python - 如何从 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 产生正弦波输出,但我似乎只是得到了噪音
解决方案
推荐阅读
- mysql - 主键不能包含超过 767 或 1000 个字符作为主键
- java - 使用锁和条件暂停/恢复执行时出现 IllegalMonitorStateException
- bash - 我可以在哪里存储当前 Unix 用户的变量和值,以便我可以在 SSH 和脚本中使用它们?
- html - Bootstrap 4:与响应式导航栏元素斗争
- swift - 2 个 ObservableObjects 之间的数据通信
- java - 如何在无服务器 Java 中保存/检索图像和文本
- django - NoReverseMatch 在 /alumni/2/alumni_cv/ 反向为 'alumni_profile' 与参数 '('',)' 未找到
- javascript - 嵌套循环返回最后 2 个值
- linux - 在 grep 和 sed 中正确使用正则表达式开关
- sql - 得到一个不同的行以及它的值