2016-06-07 22:57:41 +01:00
|
|
|
.. currentmodule:: pyb
|
2014-11-04 18:25:20 +00:00
|
|
|
.. _pyb.DAC:
|
|
|
|
|
2014-10-31 22:21:37 +00:00
|
|
|
class DAC -- digital to analog conversion
|
|
|
|
=========================================
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
|
|
The DAC is used to output analog values (a specific voltage) on pin X5 or pin X6.
|
|
|
|
The voltage will be between 0 and 3.3V.
|
|
|
|
|
|
|
|
*This module will undergo changes to the API.*
|
|
|
|
|
|
|
|
Example usage::
|
|
|
|
|
|
|
|
from pyb import DAC
|
|
|
|
|
|
|
|
dac = DAC(1) # create DAC 1 on pin X5
|
|
|
|
dac.write(128) # write a value to the DAC (makes X5 1.65V)
|
|
|
|
|
2015-10-13 14:33:04 +01:00
|
|
|
dac = DAC(1, bits=12) # use 12 bit resolution
|
|
|
|
dac.write(4095) # output maximum value, 3.3V
|
|
|
|
|
2014-10-31 01:37:19 +00:00
|
|
|
To output a continuous sine-wave::
|
|
|
|
|
|
|
|
import math
|
|
|
|
from pyb import DAC
|
|
|
|
|
|
|
|
# create a buffer containing a sine-wave
|
|
|
|
buf = bytearray(100)
|
|
|
|
for i in range(len(buf)):
|
2015-10-13 14:44:00 +01:00
|
|
|
buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf)))
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
|
|
# output the sine-wave at 400Hz
|
|
|
|
dac = DAC(1)
|
2015-10-13 14:44:00 +01:00
|
|
|
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2015-10-13 14:33:04 +01:00
|
|
|
To output a continuous sine-wave at 12-bit resolution::
|
|
|
|
|
|
|
|
import math
|
|
|
|
from array import array
|
|
|
|
from pyb import DAC
|
|
|
|
|
|
|
|
# create a buffer containing a sine-wave, using half-word samples
|
|
|
|
buf = array('H', 2048 + int(2047 * math.sin(2 * math.pi * i / 128)) for i in range(128))
|
|
|
|
|
|
|
|
# output the sine-wave at 400Hz
|
|
|
|
dac = DAC(1, bits=12)
|
2015-10-13 14:44:00 +01:00
|
|
|
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
|
|
Constructors
|
|
|
|
------------
|
|
|
|
|
2020-07-11 07:53:26 +01:00
|
|
|
.. class:: pyb.DAC(port, bits=8, *, buffering=None)
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
|
|
Construct a new DAC object.
|
2015-10-13 14:33:04 +01:00
|
|
|
|
2014-10-31 01:37:19 +00:00
|
|
|
``port`` can be a pin object, or an integer (1 or 2).
|
|
|
|
DAC(1) is on pin X5 and DAC(2) is on pin X6.
|
|
|
|
|
2015-10-13 14:33:04 +01:00
|
|
|
``bits`` is an integer specifying the resolution, and can be 8 or 12.
|
|
|
|
The maximum value for the write and write_timed methods will be
|
|
|
|
2\*\*``bits``-1.
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2018-04-10 13:25:55 +01:00
|
|
|
The *buffering* parameter selects the behaviour of the DAC op-amp output
|
|
|
|
buffer, whose purpose is to reduce the output impedance. It can be
|
|
|
|
``None`` to select the default (buffering enabled for :meth:`DAC.noise`,
|
|
|
|
:meth:`DAC.triangle` and :meth:`DAC.write_timed`, and disabled for
|
|
|
|
:meth:`DAC.write`), ``False`` to disable buffering completely, or ``True``
|
|
|
|
to enable output buffering.
|
|
|
|
|
|
|
|
When buffering is enabled the DAC pin can drive loads down to 5KΩ.
|
|
|
|
Otherwise it has an output impedance of 15KΩ maximum: consequently
|
|
|
|
to achieve a 1% accuracy without buffering requires the applied load
|
|
|
|
to be less than 1.5MΩ. Using the buffer incurs a penalty in accuracy,
|
|
|
|
especially near the extremes of range.
|
|
|
|
|
2014-10-31 01:37:19 +00:00
|
|
|
Methods
|
|
|
|
-------
|
|
|
|
|
2020-07-11 07:53:26 +01:00
|
|
|
.. method:: DAC.init(bits=8, *, buffering=None)
|
2015-10-13 14:33:04 +01:00
|
|
|
|
2018-04-10 13:25:55 +01:00
|
|
|
Reinitialise the DAC. *bits* can be 8 or 12. *buffering* can be
|
2018-07-18 06:52:48 +01:00
|
|
|
``None``, ``False`` or ``True``; see above constructor for the meaning
|
2018-04-10 13:25:55 +01:00
|
|
|
of this parameter.
|
2015-10-13 14:33:04 +01:00
|
|
|
|
2016-06-08 14:21:28 +01:00
|
|
|
.. method:: DAC.deinit()
|
2016-05-26 07:39:04 +01:00
|
|
|
|
|
|
|
De-initialise the DAC making its pin available for other uses.
|
|
|
|
|
2016-06-08 14:21:28 +01:00
|
|
|
.. method:: DAC.noise(freq)
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
|
|
Generate a pseudo-random noise signal. A new random sample is written
|
|
|
|
to the DAC output at the given frequency.
|
|
|
|
|
2016-06-08 14:21:28 +01:00
|
|
|
.. method:: DAC.triangle(freq)
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2019-09-03 04:05:10 +01:00
|
|
|
Generate a triangle wave. The value on the DAC output changes at the given
|
|
|
|
frequency and ramps through the full 12-bit range (up and down). Therefore
|
|
|
|
the frequency of the repeating triangle wave itself is 8192 times smaller.
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2016-06-08 14:21:28 +01:00
|
|
|
.. method:: DAC.write(value)
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2015-10-13 14:33:04 +01:00
|
|
|
Direct access to the DAC output. The minimum value is 0. The maximum
|
|
|
|
value is 2\*\*``bits``-1, where ``bits`` is set when creating the DAC
|
|
|
|
object or by using the ``init`` method.
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2020-07-11 07:53:26 +01:00
|
|
|
.. method:: DAC.write_timed(data, freq, *, mode=DAC.NORMAL)
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
|
|
Initiates a burst of RAM to DAC using a DMA transfer.
|
2015-10-13 14:33:04 +01:00
|
|
|
The input data is treated as an array of bytes in 8-bit mode, and
|
|
|
|
an array of unsigned half-words (array typecode 'H') in 12-bit mode.
|
|
|
|
|
2015-07-21 23:37:45 +01:00
|
|
|
``freq`` can be an integer specifying the frequency to write the DAC
|
|
|
|
samples at, using Timer(6). Or it can be an already-initialised
|
|
|
|
Timer object which is used to trigger the DAC sample. Valid timers
|
|
|
|
are 2, 4, 5, 6, 7 and 8.
|
|
|
|
|
2014-10-31 01:37:19 +00:00
|
|
|
``mode`` can be ``DAC.NORMAL`` or ``DAC.CIRCULAR``.
|
2015-07-21 23:37:45 +01:00
|
|
|
|
|
|
|
Example using both DACs at the same time::
|
|
|
|
|
|
|
|
dac1 = DAC(1)
|
|
|
|
dac2 = DAC(2)
|
|
|
|
dac1.write_timed(buf1, pyb.Timer(6, freq=100), mode=DAC.CIRCULAR)
|
|
|
|
dac2.write_timed(buf2, pyb.Timer(7, freq=200), mode=DAC.CIRCULAR)
|
2021-09-21 20:58:19 +01:00
|
|
|
|
|
|
|
Constants
|
|
|
|
---------
|
|
|
|
|
|
|
|
.. data:: DAC.NORMAL
|
|
|
|
|
|
|
|
NORMAL mode does a single transmission of the waveform in the data buffer,
|
|
|
|
|
|
|
|
.. data:: DAC.CIRCULAR
|
|
|
|
|
|
|
|
CIRCULAR mode does a transmission of the waveform in the data buffer, and wraps around
|
|
|
|
to the start of the data buffer every time it reaches the end of the table.
|