Problem overwriting buffer when using DAC.write_timed() #16230
Replies: 2 comments 3 replies
-
There are three processes going on which are mutually asynchronous:
I am not surprised that the results are erratic.
In this case the two buffers contain static content. You're starting and stopping |
Beta Was this translation helpful? Give feedback.
-
The following works fine here import pyb
import math
from time import sleep_ms
nSamples = 128
samplingRate = nSamples * 8 # 1024Hz
dac = pyb.DAC(2, bits=8, buffering=False) # X6
dacTimer = pyb.Timer(6)
dacTimer.init(freq=samplingRate)
dacBuf = bytearray(nSamples)
trig = pyb.Pin('X1', pyb.Pin.OUT, value=0) # Scope trigger
def send_synchronous(tms, led):
led.on()
trig(1) # Trigger the scope
trig(0)
dac.write_timed(dacBuf, dacTimer, mode=dac.CIRCULAR) # Restart at a zero crossing
sleep_ms(tms)
led.off()
def sample(i):
return round(20 * math.sin(2 * math.pi * i / nSamples))
def populate(offset):
for i in range(nSamples) :
dacBuf[i] = offset + sample(i)
while True:
t = 1000 # rep rate 1s
populate(50)
send_synchronous(t, pyb.LED(1))
populate(100)
send_synchronous(t, pyb.LED(2)) There is a discontinuity when the waveform changes but no strange waveforms as per the original post. The discontinuity is inevitable as the code is suddenly adding or subtracting a DC value from the sine wave, but it is minimised by starting the new sinewave at a zero crossing. The key is to consider synchronisation, including the testgear. |
Beta Was this translation helpful? Give feedback.
-
Hello!
I am using micropython on an STM32 Nucleo F767ZI and I am very new to this world (so I apologize in advance if this is a trivial problem).
I wrote a simple test code that creates and outputs a sinusoid from a DAC port.
In the main loop, every 5 seconds, the sinusoid is translated up or down by a fixed quantity by overwriting a buffer.
Here is the code:
My issue is that the buffer does not seem to be correctly overwritten, as if some portion of the buffer still contain the previous sinosoid.
Edit: The previous sentence cannot actually be true. Apart from the fact that it would be a very weird behaviour, I also checked the buffer content after the overwriting by sending the data through a USART port, and the sinusoid looks good! Could it be more of a DMA/synchronization problem?
Or similarly, the buffer is overwritten, but what is continuously sent through the DMA is an "uncomplete" version of the new buffer.
The effect, as seen on a scope, is a "fragmented" sinusoid as shown in the example figures:
Some additional comments:
What am I doing wrong?
Thank you in advance for your help!
Linda
Beta Was this translation helpful? Give feedback.
All reactions