首页 > 解决方案 > 如何从 Raspberry Pi 写入 AD5754 DAC 寄存器?SPI接口

问题描述

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

我尝试写入 24 位 DAC 寄存器,创建 3 个不同的功能,并使用spi.self.writebytes一次写入 8 位,然后毫无乐趣地组合它们。

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 产生正弦波输出,但我似乎只是得到了噪音

标签: pythoncpu-registersspidac

解决方案


推荐阅读