I'm trying to sync a DAC waveform write with an ADC read. Both the adc and the dac are clocked to the same timer. I've noticed, using the code below, that there is an offset between the readout and the DAC buffer. I guess this is to be expected, as there is no trigger to synchronise the start of both the ADC and DAC. Any ideas on how best to implement a trigger like this?
Code: Select all
import math
import pyb
tim = pyb.Timer(6, freq=1000) # create a timer running at 1000Hz
adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19
dac = pyb.DAC(1)
# create a buffer containing a sine-wave
buf = bytearray(500)
for i in range(len(buf)):
buf[i] = 128 + int(127 * math.sin(4 * math.pi * i / len(buf)))
# output the sine-wave at 400Hz
dac.write_timed(buf, tim, mode=DAC.CIRCULAR)
# Setup the read buffer
abuf = bytearray(500) # create a buffer to store the samples
adc.read_timed(abuf, tim) # sample 100 values, taking 10s
for enu,i in enumerate(buf):
print(i, abuf[enu])