Problem overwriting buffer when using DAC.write_timed() #16230
Replies: 4 comments 9 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.
-
@LinderaG To address the issue of your waveform when running my code. How it should work (and how it does work on the Pyboard) is:
Your scope trace shows a frequency of 79.72Hz which is ten times too high. The offset is being applied and removed four times per cycle, a frequency of 159.44Hz. I'm afraid I'm stumped. I can't see how the code I presented can produce the frequencies you are observing. Unfortunately I don't have a Nucleo board so I can't replicate your test conditions exactly, but it seems that somehow the timing is wildly out. Re DMA I haven't studied the source but feel free to do so. I'm sorry I can't help further with this, but I'm out of ideas. |
Beta Was this translation helpful? Give feedback.
-
Hello, I have an update for this topic. Not sure if the problem was with the Nucleo board or in the STM32F7. Thank you again peterhinch for your help. Best, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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