Skip to content

Instantly share code, notes, and snippets.

@0mars
Created July 13, 2020 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0mars/7e648158402a57aef05297203ca664be to your computer and use it in GitHub Desktop.
Save 0mars/7e648158402a57aef05297203ca664be to your computer and use it in GitHub Desktop.
# LICENSE MIT
import utime as time
from gates import GateIn
from leds import LED
print('RUN: main.py')
from machine import Pin, SPI
class MCP4822:
"""
"""
GAIN_BYTE = 13
CHANNEL_SELECT_BYTE = 15
TURN_CHANNEL_ON_BYTE = 12
def __init__(self, chip_select:int, ldac: int, sck: int, mosi: int, miso:int):
self.spi = SPI(1, 10000000, sck=Pin(sck, Pin.OUT), mosi=Pin(mosi, Pin.OUT), miso=Pin(miso), polarity=0, phase=0, firstbit=SPI.MSB)
self.chip_select = Pin(chip_select)
self.ldac = Pin(ldac)
self.init()
def init(self):
self.chip_select.init(self.chip_select.OUT, value=1)
self.ldac.init(self.ldac.OUT, value=0)
time.sleep_ms(120)
def _write(self, data: int):
self.ldac.value(1)
self.chip_select.value(0)
self.spi.write(bytearray(bin(data)))
self.chip_select.value(1)
self.ldac.value(0)
def set_voltage(self, voltage: int):
command = 0 << 15 # channel A
command = command | (1 << 12) # turn on
command = command | (0 << 13) # set gain to high
value = voltage >> (12 - 12) # second 12 is bit resolution (12-bit_resolution), set voltage
command = command | value
print("command: {}".format(command))
print("bin: {}".format(bin(command)))
dac._write(command)
dac = MCP4822(2, 4, 14, 13, 12)
dac.set_voltage(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment