Skip to content

Instantly share code, notes, and snippets.

@microbit-carlos
Last active July 19, 2017 14:17
Show Gist options
  • Save microbit-carlos/d3110c69ee30ee2c92feca1397105cb0 to your computer and use it in GitHub Desktop.
Save microbit-carlos/d3110c69ee30ee2c92feca1397105cb0 to your computer and use it in GitHub Desktop.
micro:bit MicroPython ADC config test
from microbit import pin0, pin1
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
print("ADC0: {}".format(pin0.read_analog()))
print("ADC config should be 0x106A: {:#010x}".format(reg_read(NRF_ADC_CONFIG)))
# Edit the ref voltage config, set to 0 bits 5 and 6
reg_bit_clear(NRF_ADC_CONFIG, 0x60)
print("ADC config should be 0x100A: {:#010x}".format(reg_read(NRF_ADC_CONFIG)))
# Read ADC 0 again, check the ADC peripheral config
print("ADC 0: {}".format(pin0.read_analog()))
print("ADC config should be 0x106A, but it's 0x100A: {:#010x}".format(reg_read(NRF_ADC_CONFIG)))
# Read ADC 1, check config
print("ADC 1: {}".format(pin1.read_analog()))
print("ADC config should be 0x86A: {:#010x}".format(reg_read(NRF_ADC_CONFIG)))
# Edit ref voltage again
reg_bit_clear(NRF_ADC_CONFIG, 0x60)
print("ADC config should be 0x80A: {:#010x}".format(reg_read(NRF_ADC_CONFIG)))
# Read ADC 1 again, check the ADC peripheral
print("ADC 1: {}".format(pin1.read_analog()))
print("ADC config should be 0x86A, but it's 0x80A: {:#010x}".format(reg_read(NRF_ADC_CONFIG)))
# Read the analog pins 0 and 1 again, check their config
print("ADC 0: {}".format(pin0.read_analog()))
print("ADC config should be 0x106A, but it's 0x100A: {:#010x}".format(reg_read(NRF_ADC_CONFIG)))
print("ADC 1: {}".format(pin1.read_analog()))
print("ADC config should be 0x86A, but it's 0x80A: {:#010x}".format(reg_read(NRF_ADC_CONFIG)))
ADC config should be 0x18: 0x00000018
ADC0: 239
ADC config should be 0x106A: 0x0000106a
ADC config should be 0x100A: 0x0000100a
ADC 0: 212
ADC config should be 0x106A, but it's 0x100A: 0x0000100a
ADC 1: 238
ADC config should be 0x86A: 0x0000086a
ADC config should be 0x80A: 0x0000080a
ADC 1: 210
ADC config should be 0x86A, but it's 0x80A: 0x0000080a
ADC 0: 213
ADC config should be 0x106A, but it's 0x100A: 0x0000100a
ADC 1: 211
ADC config should be 0x86A, but it's 0x80A: 0x0000080a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment