Skip to content

Commit 8f11d0b

Browse files
T Sdpgeorge
authored andcommitted
docs/library/pyb.ADC.rst: Document new features for ADCAll.
1 parent d6cf5c6 commit 8f11d0b

1 file changed

Lines changed: 46 additions & 28 deletions

File tree

docs/library/pyb.ADC.rst

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ class ADC -- analog to digital conversion
1010

1111
import pyb
1212
13-
adc = pyb.ADC(pin) # create an analog object from a pin
14-
val = adc.read() # read an analog value
13+
adc = pyb.ADC(pin) # create an analog object from a pin
14+
val = adc.read() # read an analog value
1515
16-
adc = pyb.ADCAll(resolution) # create an ADCAll object
17-
val = adc.read_channel(channel) # read the given channel
18-
val = adc.read_core_temp() # read MCU temperature
19-
val = adc.read_core_vbat() # read MCU VBAT
20-
val = adc.read_core_vref() # read MCU VREF
16+
adc = pyb.ADCAll(resolution) # create an ADCAll object
17+
adc = pyb.ADCAll(resolution, mask) # create an ADCAll object for selected analog channels
18+
val = adc.read_channel(channel) # read the given channel
19+
val = adc.read_core_temp() # read MCU temperature
20+
val = adc.read_core_vbat() # read MCU VBAT
21+
val = adc.read_core_vref() # read MCU VREF
22+
val = adc.read_vref() # read MCU supply voltage
2123

2224
2325
Constructors
@@ -81,27 +83,42 @@ The ADCAll Object
8183

8284
.. only:: port_pyboard
8385

84-
Instantiating this changes all ADC pins to analog inputs. The raw MCU temperature,
86+
Instantiating this changes all masked ADC pins to analog inputs. The preprocessed MCU temperature,
8587
VREF and VBAT data can be accessed on ADC channels 16, 17 and 18 respectively.
86-
Appropriate scaling will need to be applied. The temperature sensor on the chip
87-
has poor absolute accuracy and is suitable only for detecting temperature changes.
88-
89-
The ``ADCAll`` ``read_core_vbat()`` and ``read_core_vref()`` methods read
90-
the backup battery voltage and the (1.21V nominal) reference voltage using the
91-
3.3V supply as a reference. Assuming the ``ADCAll`` object has been Instantiated with
92-
``adc = pyb.ADCAll(12)`` the 3.3V supply voltage may be calculated:
93-
94-
``v33 = 3.3 * 1.21 / adc.read_core_vref()``
95-
96-
If the 3.3V supply is correct the value of ``adc.read_core_vbat()`` will be
97-
valid. If the supply voltage can drop below 3.3V, for example in in battery
98-
powered systems with a discharging battery, the regulator will fail to preserve
99-
the 3.3V supply resulting in an incorrect reading. To produce a value which will
100-
remain valid under these circumstances use the following:
101-
102-
``vback = adc.read_core_vbat() * 1.21 / adc.read_core_vref()``
103-
104-
It is possible to access these values without incurring the side effects of ``ADCAll``::
88+
Appropriate scaling is handled according to reference voltage used (usually 3.3V).
89+
The temperature sensor on the chip is factory calibrated and allows to read the die temperature
90+
to +/- 1 degree centigrade. Although this sounds pretty accurate, don't forget that the MCU's internal
91+
temperature is measured. Depending on processing loads and I/O subsystems active the die temperature
92+
may easily be tens of degrees above ambient temperature. On the other hand a pyboard woken up after a
93+
long standby period will show correct ambient temperature within limits mentioned above.
94+
95+
The ``ADCAll`` ``read_core_vbat()``, ``read_vref()`` and ``read_core_vref()`` methods read
96+
the backup battery voltage, reference voltage and the (1.21V nominal) reference voltage using the
97+
actual supply as a reference. All results are floating point numbers giving direct voltage values.
98+
99+
``read_core_vbat()`` returns the voltage of the backup battery. This voltage is also adjusted according
100+
to the actual supply voltage. To avoid analog input overload the battery voltage is measured
101+
via a voltage divider and scaled according to the divider value. To prevent excessive loads
102+
to the backup battery, the voltage divider is only active during ADC conversion.
103+
104+
``read_vref()`` is evaluated by measuring the internal voltage reference and backscale it using
105+
factory calibration value of the internal voltage reference. In most cases the reading would be close
106+
to 3.3V. If the pyboard is operated from a battery, the supply voltage may drop to values below 3.3V.
107+
The pyboard will still operate fine as long as the operating conditions are met. With proper settings
108+
of MCU clock, flash access speed and programming mode it is possible to run the pyboard down to
109+
2 V and still get useful ADC conversion.
110+
111+
It is very important to make sure analog input voltages never exceed actual supply voltage.
112+
113+
Other analog input channels (0..15) will return unscaled integer values according to the selected
114+
precision.
115+
116+
To avoid unwanted activation of analog inputs (channel 0..15) a second prarmeter can be specified.
117+
This parameter is a binary pattern where each requested analog input has the corresponding bit set.
118+
The default value is 0xffffffff which means all analog inputs are active. If just the internal
119+
channels (16..18) are required, the mask value should be 0x70000.
120+
121+
It is possible to access channle 16..18 values without incurring the side effects of ``ADCAll``::
105122
106123
def adcread(chan): # 16 temp 17 vbat 18 vref
107124
assert chan >= 16 and chan <= 18, 'Invalid ADC channel'
@@ -140,4 +157,5 @@ The ADCAll Object
140157
def temperature():
141158
return 25 + 400 * (3.3 * adcread(16) / 4096 - 0.76)
142159

143-
160+
Note that this example is only valid for the F405 MCU and all values are not corrected by Vref and
161+
factory calibration data.

0 commit comments

Comments
 (0)