Cacks96
Cacks96

Reputation: 11

How can I write to AD5754 DAC register from Raspberry Pi? SPI interface

I'm using a Raspberry pi to try and control the DAC AD5754 from Analog Devices to produce arbitrary waveforms. I've connected the RPI to the DAC using SPI. I'm trying to write into the DAC to first power up the DACs, secondly choose all four DACs and then lastly set the output voltage range to +-10V.

I've tried writing into the 24 bit DAC registers creating 3 different functions and using spi.self.writebytes to write 8 bits at a time then combine them without any joy.

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()
  

I expect VoutA,B,C,D to produce sine wave outputs however i just seem to be getting noise

Upvotes: 1

Views: 858

Answers (0)

Related Questions