1
\$\begingroup\$

I am trying to output a signal with dac124s085 from python code on a raspberry pi 3b, running Raspbian Stretch (kernel 4.14.30-v7+ #1102).

I am sure the spi master driver is enabled - dtparam=spi=on isn't commented out in /boot/config.txt

I am using 20 cm dupont cables between rpi and dac. The pinout is as follows (dac124s085 pinout on page 3 | raspberry pi 3 pinout)

MOSI pin 19 on rpi connected to pin 8 DIN on dac
MISO pin 21 on rpi not used by dac
SCLK pin 23 on rpi connected to pin 10 SCLK on dac
CE0  pin 24 on rpi not used by dac
GND  pin 25 on rpi connected to pin 6 GND on dac
CE1  pin 26 on rpi connected to pin 9 SYNC on dac

dac circuit schematic

pin 7 VREFIN on dac is connected to 5V pin 2 on rpi (decoupled to ground)
pin 1 VA on dac is also connected to 5V decoupled to ground
pin 2-5 VOUTA-D on dac is connected to two stereojacks via adc on each line

whole schematic on github

I am using the py-spidev library (https://pypi.org/project/spidev/). Loopback test on spidev0.0 and spidev0.1:

./spidev_test -D /dev/spidev0.0
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

FF FF FF FF FF FF
40 00 00 00 00 95
FF FF FF FF FF FF
FF FF FF FF FF FF
FF FF FF FF FF FF
DE AD BE EF BA AD
F0 0D

The code I am using:

import spidev
import math

class Dac:

    def __init__(self, device):
        self.spi = spidev.SpiDev()
        self.spi.open(0, device)
        self.spi.max_speed_hz = 1000000

    def write(self, ch, data):
        wdata = []
        wdata.append((ch << 6) | ((int(data) & 0xF00) >> 8))
        wdata.append(int(data) & 0xFF)
        self.spi.xfer(wdata)

    def writeAll(self, ch0, ch1, ch2, ch3):
        self.write(0, ch0)
        self.write(1, ch1)
        self.write(2, ch2)
        self.write(3, ch3)

if __name__ == '__main__':

    dac = Dac(1)

    nsamples = 32
    a = [0] * nsamples
    for n in range(nsamples):
        a[n] = int(2048 + 512*math.sin(2*math.pi*n/256))

    while (True):
        for sample in a:
            dac.write(0, sample)

(dac124s085.py script at github)

How can I troubleshoot this problem? The script should output a sine wave but does not. It actually works if I grab the cables between the circuit and the rpi. Maybe the conductivity of my body does something with the clock speed or pulls any pin high/low?

Is the script correct? I must confess I do not really understand it.

Logic analyzer is showing this when I run sudo python3 dac124s085.py on rpi:

spi logic screenshot

Is this right with reference to timing diagram?

Serial Timing Diagram for dac124s085 | figure 2 in datasheet

Edit: When I try loop.py the dac actually output a constant signal (without me holding the cables or anything)

from adc124s021 import Adc
from dac124s085 import Dac

adc = Adc(0)
dac = Dac(1)

while True:
    ch0, ch1, ch2, ch3 = adc.readAll()
    print(ch0, ch1, ch2, ch3)
    dac.writeAll(ch0, ch1, ch2, ch3)

But dac out hooked to a speaker I mostly get a noise sound - except from when I am nearby the circuit with dac, rpi and wires (not touching) I will upload a csv file with the datapoints.

Edit 2:

The problem is fixed by adding self.spi.mode = 3 under def __init__ in dac124s085.py

I guess I should have a serious read over www.allaboutcircuits.com/technical-articles/spi-serial-peripheral-interface/

\$\endgroup\$
11
  • \$\begingroup\$ "... if I grab the cables ..." is not a good technical description of what you are doing. I suggest you edit to give more precise details about the grab and the cable. \$\endgroup\$ – Transistor May 11 '18 at 19:33
  • \$\begingroup\$ @Transistor "I caressed the red wire with my index finger, it made a soft innocent sound", something along these lines? \$\endgroup\$ – Harry Svensson May 11 '18 at 19:37
  • \$\begingroup\$ The circuit and spi0 on raspberry pi is connected as the pinout at pinout.xyz/pinout/spi# with dupont cables. I grabbed the cables with my hand on the insulated part of the cables. I am sorry about the poor description. Ha ha! Good one @HarrySvensson \$\endgroup\$ – coffeeandcigarettes May 11 '18 at 19:40
  • \$\begingroup\$ The loopback test on raspberrypi.org/documentation/hardware/raspberrypi/spi is producing the same results as stated there. \$\endgroup\$ – coffeeandcigarettes May 11 '18 at 19:44
  • \$\begingroup\$ Grabbing the cables adds CM shunt nF to gnd indicating high CM noise influence on unbalanced lines . It needs shields with high CM capacitance to gnd. \$\endgroup\$ – Tony Stewart Sunnyskyguy EE75 May 12 '18 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.