from machine import Pin, SoftSPI
import time
def WDac(H,L):
#txbuf 需要将16进制转换为10进制
spi = SoftSPI(baudrate=51000, polarity=0, phase=0, mosi=Pin(4), miso=Pin(16), sck=Pin(2))
spi.init(baudrate=125000) # set the baudrate
p16 = Pin(17, Pin.OUT) # create output pin on GPIO16
p16.value(0) # set pin to on/high
time.sleep(1)
p16.value(0) # set pin to on/high
#开启POWER EN0
p16.value(0) # set pin to on/high
txbuf =bytearray([0,178,255])
spi.write(txbuf) # write to MOSI and read from MISO into the buffer
p16.value(1) # set pin to on/high
time.sleep(0.001)
#开启POWER EN1
p16.value(0) # set pin to on/high
txbuf =bytearray([0,179,255])
spi.write(txbuf) # write to MOSI and read from MISO into the buffer
p16.value(1) # set pin to on/high
time.sleep(0.001)
#开启POWER EN2
p16.value(0) # set pin to on/high
txbuf =bytearray([0,180,255])
spi.write(txbuf) # write to MOSI and read from MISO into the buffer
p16.value(1) # set pin to on/high
time.sleep(0.001)
#开启POWER EN3
p16.value(0) # set pin to on/high
txbuf =bytearray([0,181,255])
spi.write(txbuf) # write to MOSI and read from MISO into the buffer
p16.value(1) # set pin to on/high
time.sleep(0.001)
#所有DAC Range 0~10
#p16.value(0) # set pin to on/high
#txbuf =bytearray([128,21,102])
#spi.write(txbuf) # write to MOSI and read from MISO into the buffer
#p16.value(1) # set pin to on/high
#time.sleep(0.001)
#所有DAC高8位写 0x80
p16.value(0) # set pin to on/high
txbuf =bytearray([0,126,H])
spi.write(txbuf) # write to MOSI and read from MISO into the buffer
p16.value(1) # set pin to on/high
time.sleep(0.001)
#所有DAC低8位写 0x00
p16.value(0) # set pin to on/high
txbuf =bytearray([0,127,L])
spi.write(txbuf) # write to MOSI and read from MISO into the buffer
p16.value(1) # set pin to on/high
time.sleep(0.001)
p16.value(0) # set pin to on/high
#更新寄存器
txbuf =bytearray([0,15,1])
spi.write(txbuf) # write to MOSI and read from MISO into the buffer
p16.value(1) # set pin to on/high
#reg addr 需要转换成10进制
def GetReg(RegAddr):
rxbuf = bytearray(1) # create a buffer
spi = SoftSPI(baudrate=51000, polarity=0, phase=0, mosi=Pin(4), miso=Pin(16), sck=Pin(2))
spi.init(baudrate=125000) # set the baudrate
p16 = Pin(17, Pin.OUT) # create output pin on GPIO16
p16.value(0) # set pin to on/high
txbuf =bytearray([128,RegAddr])
spi.write(txbuf)
time.sleep(0.0005)
spi.readinto(rxbuf) # write to MOSI and read from MISO into the buffer
p16.value(1)
return rxbuf