Skip to content

Commit 85d3b61

Browse files
peterhinchdpgeorge
authored andcommitted
docs: Update details on using ADCAll object for vref/vbat channels.
1 parent 70f32f0 commit 85d3b61

1 file changed

Lines changed: 63 additions & 3 deletions

File tree

docs/library/pyb.ADC.rst

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ADC -- analog to digital conversion
1818
val = adc.read_core_vbat() # read MCU VBAT
1919
val = adc.read_core_vref() # read MCU VREF
2020

21+
2122
Constructors
2223
------------
2324

@@ -77,6 +78,65 @@ Methods
7778
The ADCAll Object
7879
-----------------
7980

80-
Instantiating this changes all ADC pins to analog inputs. It is possible to read the
81-
MCU temperature, VREF and VBAT without using ADCAll. The raw data can be accessed on
82-
ADC channels 16, 17 and 18 respectively. However appropriate scaling will need to be applied.
81+
.. only:: port_pyboard
82+
83+
Instantiating this changes all ADC pins to analog inputs. The raw MCU temperature,
84+
VREF and VBAT data can be accessed on ADC channels 16, 17 and 18 respectively.
85+
Appropriate scaling will need to be applied. The temperature sensor on the chip
86+
has poor absolute accuracy and is suitable only for detecting temperature changes.
87+
88+
The ``ADCAll`` ``read_core_vbat()`` and ``read_core_vref()`` methods read
89+
the backup battery voltage and the (1.21V nominal) reference voltage using the
90+
3.3V supply as a reference. Assuming the ``ADCAll`` object has been Instantiated with
91+
``adc = pyb.ADCAll(12)`` the 3.3V supply voltage may be calculated:
92+
93+
``v33 = 3.3 * 1.21 / adc.read_core_vref()``
94+
95+
If the 3.3V supply is correct the value of ``adc.read_core_vbat()`` will be
96+
valid. If the supply voltage can drop below 3.3V, for example in in battery
97+
powered systems with a discharging battery, the regulator will fail to preserve
98+
the 3.3V supply resulting in an incorrect reading. To produce a value which will
99+
remain valid under these circumstances use the following:
100+
101+
``vback = adc.read_core_vbat() * 1.21 / adc.read_core_vref()``
102+
103+
It is possible to access these values without incurring the side effects of ``ADCAll``::
104+
105+
def adcread(chan): # 16 temp 17 vbat 18 vref
106+
assert chan >= 16 and chan <= 18, 'Invalid ADC channel'
107+
start = pyb.millis()
108+
timeout = 100
109+
stm.mem32[stm.RCC + stm.RCC_APB2ENR] |= 0x100 # enable ADC1 clock.0x4100
110+
stm.mem32[stm.ADC1 + stm.ADC_CR2] = 1 # Turn on ADC
111+
stm.mem32[stm.ADC1 + stm.ADC_CR1] = 0 # 12 bit
112+
if chan == 17:
113+
stm.mem32[stm.ADC1 + stm.ADC_SMPR1] = 0x200000 # 15 cycles
114+
stm.mem32[stm.ADC + 4] = 1 << 23
115+
elif chan == 18:
116+
stm.mem32[stm.ADC1 + stm.ADC_SMPR1] = 0x1000000
117+
stm.mem32[stm.ADC + 4] = 0xc00000
118+
else:
119+
stm.mem32[stm.ADC1 + stm.ADC_SMPR1] = 0x40000
120+
stm.mem32[stm.ADC + 4] = 1 << 23
121+
stm.mem32[stm.ADC1 + stm.ADC_SQR3] = chan
122+
stm.mem32[stm.ADC1 + stm.ADC_CR2] = 1 | (1 << 30) | (1 << 10) # start conversion
123+
while not stm.mem32[stm.ADC1 + stm.ADC_SR] & 2: # wait for EOC
124+
if pyb.elapsed_millis(start) > timeout:
125+
raise OSError('ADC timout')
126+
data = stm.mem32[stm.ADC1 + stm.ADC_DR] # clear down EOC
127+
stm.mem32[stm.ADC1 + stm.ADC_CR2] = 0 # Turn off ADC
128+
return data
129+
130+
def v33():
131+
return 4096 * 1.21 / adcread(17)
132+
133+
def vbat():
134+
return 1.21 * 2 * adcread(18) / adcread(17) # 2:1 divider on Vbat channel
135+
136+
def vref():
137+
return 3.3 * adcread(17) / 4096
138+
139+
def temperature():
140+
return 25 + 400 * (3.3 * adcread(16) / 4096 - 0.76)
141+
142+

0 commit comments

Comments
 (0)