pyb.DAC.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. .. currentmodule:: pyb
  2. .. _pyb.DAC:
  3. class DAC -- digital to analog conversion
  4. =========================================
  5. The DAC is used to output analog values (a specific voltage) on pin X5 or pin X6.
  6. The voltage will be between 0 and 3.3V.
  7. *This module will undergo changes to the API.*
  8. Example usage::
  9. from pyb import DAC
  10. dac = DAC(1) # create DAC 1 on pin X5
  11. dac.write(128) # write a value to the DAC (makes X5 1.65V)
  12. dac = DAC(1, bits=12) # use 12 bit resolution
  13. dac.write(4095) # output maximum value, 3.3V
  14. To output a continuous sine-wave::
  15. import math
  16. from pyb import DAC
  17. # create a buffer containing a sine-wave
  18. buf = bytearray(100)
  19. for i in range(len(buf)):
  20. buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf)))
  21. # output the sine-wave at 400Hz
  22. dac = DAC(1)
  23. dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
  24. To output a continuous sine-wave at 12-bit resolution::
  25. import math
  26. from array import array
  27. from pyb import DAC
  28. # create a buffer containing a sine-wave, using half-word samples
  29. buf = array('H', 2048 + int(2047 * math.sin(2 * math.pi * i / 128)) for i in range(128))
  30. # output the sine-wave at 400Hz
  31. dac = DAC(1, bits=12)
  32. dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
  33. Constructors
  34. ------------
  35. .. class:: pyb.DAC(port, bits=8, \*, buffering=None)
  36. Construct a new DAC object.
  37. ``port`` can be a pin object, or an integer (1 or 2).
  38. DAC(1) is on pin X5 and DAC(2) is on pin X6.
  39. ``bits`` is an integer specifying the resolution, and can be 8 or 12.
  40. The maximum value for the write and write_timed methods will be
  41. 2\*\*``bits``-1.
  42. The *buffering* parameter selects the behaviour of the DAC op-amp output
  43. buffer, whose purpose is to reduce the output impedance. It can be
  44. ``None`` to select the default (buffering enabled for :meth:`DAC.noise`,
  45. :meth:`DAC.triangle` and :meth:`DAC.write_timed`, and disabled for
  46. :meth:`DAC.write`), ``False`` to disable buffering completely, or ``True``
  47. to enable output buffering.
  48. When buffering is enabled the DAC pin can drive loads down to 5KΩ.
  49. Otherwise it has an output impedance of 15KΩ maximum: consequently
  50. to achieve a 1% accuracy without buffering requires the applied load
  51. to be less than 1.5MΩ. Using the buffer incurs a penalty in accuracy,
  52. especially near the extremes of range.
  53. Methods
  54. -------
  55. .. method:: DAC.init(bits=8, \*, buffering=None)
  56. Reinitialise the DAC. *bits* can be 8 or 12. *buffering* can be
  57. ``None``, ``False`` or ``True``; see above constructor for the meaning
  58. of this parameter.
  59. .. method:: DAC.deinit()
  60. De-initialise the DAC making its pin available for other uses.
  61. .. method:: DAC.noise(freq)
  62. Generate a pseudo-random noise signal. A new random sample is written
  63. to the DAC output at the given frequency.
  64. .. method:: DAC.triangle(freq)
  65. Generate a triangle wave. The value on the DAC output changes at
  66. the given frequency, and the frequency of the repeating triangle wave
  67. itself is 2048 times smaller.
  68. .. method:: DAC.write(value)
  69. Direct access to the DAC output. The minimum value is 0. The maximum
  70. value is 2\*\*``bits``-1, where ``bits`` is set when creating the DAC
  71. object or by using the ``init`` method.
  72. .. method:: DAC.write_timed(data, freq, \*, mode=DAC.NORMAL)
  73. Initiates a burst of RAM to DAC using a DMA transfer.
  74. The input data is treated as an array of bytes in 8-bit mode, and
  75. an array of unsigned half-words (array typecode 'H') in 12-bit mode.
  76. ``freq`` can be an integer specifying the frequency to write the DAC
  77. samples at, using Timer(6). Or it can be an already-initialised
  78. Timer object which is used to trigger the DAC sample. Valid timers
  79. are 2, 4, 5, 6, 7 and 8.
  80. ``mode`` can be ``DAC.NORMAL`` or ``DAC.CIRCULAR``.
  81. Example using both DACs at the same time::
  82. dac1 = DAC(1)
  83. dac2 = DAC(2)
  84. dac1.write_timed(buf1, pyb.Timer(6, freq=100), mode=DAC.CIRCULAR)
  85. dac2.write_timed(buf2, pyb.Timer(7, freq=200), mode=DAC.CIRCULAR)