cancel
Showing results for 
Search instead for 
Did you mean: 

Changing data for DAC every 1 sec

YLivn.1
Associate II

Hello,

I currently use the STM32G4 MCU Nucleo board (NUCLEO-G474RE) - I create an array of numbers (specifically - a sine wave at a certain frequency), which I transmit via the DMA, triggered by a timer (TIM2). Currently, the array is being transmitted by the DAC endlessly.

What I want is:

  1. Every 1 sec, create a new array to play.
  2. Transmit the new array for the duration of 1 sec. Meaning changing the data for the DAC every 1 sec.
  3. When reaching the "last array" - stop the transmission.

For example:

arr1 = [0,1,2,...]

transmit arr1 for 1 sec.

Create a new arr: arr2 = [3,4,5...]

transmit arr2 for 1 sec

etc.

What is the best way to do that? Please be as specific as you can regarding each step, an example will help.

Thanks for helping!

4 REPLIES 4
TDK
Guru

Set the DAC to shift out data via a circular DMA buffer, forever.

Every 1s, change the contents of that buffer.

At the end, stop the DAC.

So don't create a new array, just modify the one already in use by the DAC.

If you feel a post has answered your question, please click "Accept as Solution".

Hey,

How do I time the every 1sec to change the content of the buffer?

TDK
Guru

The value returned by HAL_GetTick() changes by 1 every 1ms. You could use that. Or you could create another timer that updates once per second and do the processing in the timer update callback.

If you feel a post has answered your question, please click "Accept as Solution".

Have the DMA TC/HT interrupts count out elapsed time, have the buffer size as some multiple that cleanly divides into the time span you're looking to cover. ie don't pick large odd numbers that don't divide into anything helpfully.

Say have a buffer holding 10 or 100ms of sample data. Perhaps use a TIM with a repetition count, and capable of triggering the DAC with a CCx rather than UPDATE signal.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..