Skip to content
Snippets Groups Projects
Select Git revision
  • a384a5313013d8ffda4db0e509cf798a080b3526
  • wip-bootstrap default
  • dualcore
  • ch3/leds
  • ch3/time
  • master
6 results

pyb.DAC.rst

Blame
  • user avatar
    Paul Sokolovsky authored
    Class designator will be used as is in indexes, so must match actual class
    name.
    a384a531
    History
    pyb.DAC.rst 3.18 KiB

    class DAC -- digital to analog conversion

    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)
    
    dac = DAC(1, bits=12)   # use 12 bit resolution
    dac.write(4095)         # output maximum value, 3.3V

    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)):
        buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf)))
    
    # output the sine-wave at 400Hz
    dac = DAC(1)
    dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)

    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)
    dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)

    Constructors

    Construct a new DAC object.

    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.

    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.

    Methods