pyb.ADC.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. .. currentmodule:: pyb
  2. .. _pyb.ADC:
  3. class ADC -- analog to digital conversion
  4. =========================================
  5. Usage::
  6. import pyb
  7. adc = pyb.ADC(pin) # create an analog object from a pin
  8. val = adc.read() # read an analog value
  9. adc = pyb.ADCAll(resolution) # create an ADCAll object
  10. adc = pyb.ADCAll(resolution, mask) # create an ADCAll object for selected analog channels
  11. val = adc.read_channel(channel) # read the given channel
  12. val = adc.read_core_temp() # read MCU temperature
  13. val = adc.read_core_vbat() # read MCU VBAT
  14. val = adc.read_core_vref() # read MCU VREF
  15. val = adc.read_vref() # read MCU supply voltage
  16. Constructors
  17. ------------
  18. .. class:: pyb.ADC(pin)
  19. Create an ADC object associated with the given pin.
  20. This allows you to then read analog values on that pin.
  21. Methods
  22. -------
  23. .. method:: ADC.read()
  24. Read the value on the analog pin and return it. The returned value
  25. will be between 0 and 4095.
  26. .. method:: ADC.read_timed(buf, timer)
  27. Read analog values into ``buf`` at a rate set by the ``timer`` object.
  28. ``buf`` can be bytearray or array.array for example. The ADC values have
  29. 12-bit resolution and are stored directly into ``buf`` if its element size is
  30. 16 bits or greater. If ``buf`` has only 8-bit elements (eg a bytearray) then
  31. the sample resolution will be reduced to 8 bits.
  32. ``timer`` should be a Timer object, and a sample is read each time the timer
  33. triggers. The timer must already be initialised and running at the desired
  34. sampling frequency.
  35. To support previous behaviour of this function, ``timer`` can also be an
  36. integer which specifies the frequency (in Hz) to sample at. In this case
  37. Timer(6) will be automatically configured to run at the given frequency.
  38. Example using a Timer object (preferred way)::
  39. adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19
  40. tim = pyb.Timer(6, freq=10) # create a timer running at 10Hz
  41. buf = bytearray(100) # creat a buffer to store the samples
  42. adc.read_timed(buf, tim) # sample 100 values, taking 10s
  43. Example using an integer for the frequency::
  44. adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19
  45. buf = bytearray(100) # create a buffer of 100 bytes
  46. adc.read_timed(buf, 10) # read analog values into buf at 10Hz
  47. # this will take 10 seconds to finish
  48. for val in buf: # loop over all values
  49. print(val) # print the value out
  50. This function does not allocate any heap memory. It has blocking behaviour:
  51. it does not return to the calling program until the buffer is full.
  52. .. method:: ADC.read_timed_multi((adcx, adcy, ...), (bufx, bufy, ...), timer)
  53. This is a static method. It can be used to extract relative timing or
  54. phase data from multiple ADC's.
  55. It reads analog values from multiple ADC's into buffers at a rate set by
  56. the *timer* object. Each time the timer triggers a sample is rapidly
  57. read from each ADC in turn.
  58. ADC and buffer instances are passed in tuples with each ADC having an
  59. associated buffer. All buffers must be of the same type and length and
  60. the number of buffers must equal the number of ADC's.
  61. Buffers can be ``bytearray`` or ``array.array`` for example. The ADC values
  62. have 12-bit resolution and are stored directly into the buffer if its element
  63. size is 16 bits or greater. If buffers have only 8-bit elements (eg a
  64. ``bytearray``) then the sample resolution will be reduced to 8 bits.
  65. *timer* must be a Timer object. The timer must already be initialised
  66. and running at the desired sampling frequency.
  67. Example reading 3 ADC's::
  68. adc0 = pyb.ADC(pyb.Pin.board.X1) # Create ADC's
  69. adc1 = pyb.ADC(pyb.Pin.board.X2)
  70. adc2 = pyb.ADC(pyb.Pin.board.X3)
  71. tim = pyb.Timer(8, freq=100) # Create timer
  72. rx0 = array.array('H', (0 for i in range(100))) # ADC buffers of
  73. rx1 = array.array('H', (0 for i in range(100))) # 100 16-bit words
  74. rx2 = array.array('H', (0 for i in range(100)))
  75. # read analog values into buffers at 100Hz (takes one second)
  76. pyb.ADC.read_timed_multi((adc0, adc1, adc2), (rx0, rx1, rx2), tim)
  77. for n in range(len(rx0)):
  78. print(rx0[n], rx1[n], rx2[n])
  79. This function does not allocate any heap memory. It has blocking behaviour:
  80. it does not return to the calling program until the buffers are full.
  81. The function returns ``True`` if all samples were acquired with correct
  82. timing. At high sample rates the time taken to acquire a set of samples
  83. can exceed the timer period. In this case the function returns ``False``,
  84. indicating a loss of precision in the sample interval. In extreme cases
  85. samples may be missed.
  86. The maximum rate depends on factors including the data width and the
  87. number of ADC's being read. In testing two ADC's were sampled at a timer
  88. rate of 210kHz without overrun. Samples were missed at 215kHz. For three
  89. ADC's the limit is around 140kHz, and for four it is around 110kHz.
  90. At high sample rates disabling interrupts for the duration can reduce the
  91. risk of sporadic data loss.
  92. The ADCAll Object
  93. -----------------
  94. Instantiating this changes all masked ADC pins to analog inputs. The preprocessed MCU temperature,
  95. VREF and VBAT data can be accessed on ADC channels 16, 17 and 18 respectively.
  96. Appropriate scaling is handled according to reference voltage used (usually 3.3V).
  97. The temperature sensor on the chip is factory calibrated and allows to read the die temperature
  98. to +/- 1 degree centigrade. Although this sounds pretty accurate, don't forget that the MCU's internal
  99. temperature is measured. Depending on processing loads and I/O subsystems active the die temperature
  100. may easily be tens of degrees above ambient temperature. On the other hand a pyboard woken up after a
  101. long standby period will show correct ambient temperature within limits mentioned above.
  102. The ``ADCAll`` ``read_core_vbat()``, ``read_vref()`` and ``read_core_vref()`` methods read
  103. the backup battery voltage, reference voltage and the (1.21V nominal) reference voltage using the
  104. actual supply as a reference. All results are floating point numbers giving direct voltage values.
  105. ``read_core_vbat()`` returns the voltage of the backup battery. This voltage is also adjusted according
  106. to the actual supply voltage. To avoid analog input overload the battery voltage is measured
  107. via a voltage divider and scaled according to the divider value. To prevent excessive loads
  108. to the backup battery, the voltage divider is only active during ADC conversion.
  109. ``read_vref()`` is evaluated by measuring the internal voltage reference and backscale it using
  110. factory calibration value of the internal voltage reference. In most cases the reading would be close
  111. to 3.3V. If the pyboard is operated from a battery, the supply voltage may drop to values below 3.3V.
  112. The pyboard will still operate fine as long as the operating conditions are met. With proper settings
  113. of MCU clock, flash access speed and programming mode it is possible to run the pyboard down to
  114. 2 V and still get useful ADC conversion.
  115. It is very important to make sure analog input voltages never exceed actual supply voltage.
  116. Other analog input channels (0..15) will return unscaled integer values according to the selected
  117. precision.
  118. To avoid unwanted activation of analog inputs (channel 0..15) a second parameter can be specified.
  119. This parameter is a binary pattern where each requested analog input has the corresponding bit set.
  120. The default value is 0xffffffff which means all analog inputs are active. If just the internal
  121. channels (16..18) are required, the mask value should be 0x70000.
  122. Example::
  123. adcall = pyb.ADCAll(12, 0x70000) # 12 bit resolution, internal channels
  124. temp = adcall.read_core_temp()