如何从Raspberry Pi写入AD5754 DAC寄存器? SPI接口

时间:2019-04-02 20:36:19

标签: python cpu-registers spi dac

我正在使用Raspberry pi尝试控制模拟设备中的DAC AD5754,以产生任意波形。我已经使用SPI将RPI连接到DAC。我正在尝试写入DAC,以首先为DAC上电,其次选择所有四个DAC,然后最后将输出电压范围设置为+ -10V。

我尝试写入24位DAC寄存器中,以创建3种不同的功能,并使用spi.self.writebytes一次写入8位,然后将它们组合起来没有任何乐趣。

''''

python

import spidev
from time import sleep
import math

DEBUG = False
spi_max_speed = 4 * 1000000 # 4 MHz
V_Ref = 3300 # 3V3 in mV
Resolution = 2**16 # 16 bits for the AD5754
CE = 1 # CE0 or CE1, select SPI device on bus

#setup and open an SPI channel
spi = spidev.SpiDev()
spi.open(0,CE)
spi.max_speed_hz = spi_max_speed

LDAQ = 7 # Can use any pin, here we use GPIO07
GPIO.setmode(GPIO.BOARD)'
GPIO.setup(LDAQ, GPIO.OUT)
GPIO.output(LDAQ,GPIO.LOW)

def powerUp(self)
    lowbyte = self.spi.writebytes([0xb00010000])
    midbyte = self.spi.writebytes([0xb00000000])
    highbyte = self.spi.writebytes([0xb00001111])

self.spi.xfer(lowbyte,midbyte,highbyte)

def selectDAC(self)
    lowbyte1 = self.spi.writebytes([0xb00000100])
    midbyte1 = self.spi.writebytes([0xb00000000])
    highbyte1 = self.spi.writebytes([0xb0000000])

self.spi.xfer(lowbyte1,midbyte1,highbyte1)

def selectDAC(self)
    lowbyte2 = self.spi.writebytes([0xb00000100])
    midbyte2 = self.spi.writebytes([0xb00000000])
    highbyte2 = self.spi.writebytes([0xb0000000])

self.spi.xfer(lowbyte2,midbyte2,highbyte2)

def DACvoltage(DACvalue,DACvoltage)
    DACVoltage = (-9.99969482421875,9.99969482421875);
    DACValue = (DACVoltage/10) * 32768;


try:
    while(True):
        for angle in range(1, 360):
            # generate a sine wave
            DACvalue = math.sin(math.radians(angle))
            # results in values between -1 and +1
            # add 1 to make all values positive (so between 0 and 2)
            # scale it to get only positive numbers between 0 and 256
            # Use only integer numbers
            DACvalue = int((DACvalue + 1 ) * 1024 / 8)

            if DEBUG : print "Output value is {0:24b}".format(DACvalue)

            setOutput(DACvalue)

            if DEBUG :
                sleep(0.0)
            else :
                # if you use a DMM to track the output, leave the sleep in.
                # if you use a scope, set the sleep to 0.0
                sleep(0.05)


except KeyboardInterrupt:
    print "Closing SPI channel"
    spi.close()

def main():
    'pass'

if __name__ == '__main__':
    'main() '

我希望VoutA,B,C,D会产生正弦波输出,但是我似乎只是在听到噪音

0 个答案:

没有答案