Last active
June 28, 2022 18:43
-
-
Save microbit-carlos/baebc389bccc4366abf3701acc7fc61c to your computer and use it in GitHub Desktop.
micro:bit MicroPython change ADC reference voltage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from microbit import pin0 | |
NRF_ADC_BASE = 0x40007000 | |
NRF_ADC_CONFIG = NRF_ADC_BASE + 0x504 | |
@micropython.asm_thumb | |
def reg_read(r0): | |
ldr(r0, [r0, 0]) | |
@micropython.asm_thumb | |
def reg_bit_clear(r0, r1): | |
ldr(r2, [r0, 0]) | |
bic(r2, r1) | |
str(r2, [r0, 0]) | |
# Check ADC Config register default | |
print("ADC config should be 0x18: {:#010x}".format(reg_read(NRF_ADC_CONFIG))) | |
# mbed configured: 10bit, input 1/3, ref 1/3 vdd, enable ADC in AIN4-P0.03-pin0 | |
pin0.read_analog() | |
print("ADC config should be 0x106A: {:#010x}".format(reg_read(NRF_ADC_CONFIG))) | |
# Set ref voltage to internal ref 1.2V (band gap), set bits 5-6 to 0x00 | |
reg_bit_clear(NRF_ADC_CONFIG, 0x60) | |
print("ADC config should be 0x100A: {:#010x}".format(reg_read(NRF_ADC_CONFIG))) |
I have the same problem as davek61 - i.e. "When I use it to monitor the battery voltage I always get 1023 no matter what the battery voltage is". I don't understand why.
I am using a V1 microbit and I have tested with other code e.g. https://meanderingpi.com/2017/08/05/microbit-analog-inputs/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, When I use the above code on my microbit I read all of the correct ADC CONFIG register values. However the program does not give me a pin0.read.analog value that is dependant upon the actual battery voltage. When I use it to monitor the battery voltage I always get 1023 no matter what the battery voltage is. It is as if the internal reference has not been selected. Do you have any suggestions as to what I might be doing wrong? Many Thanks, David