Skip to main content
GHARI.1
Associate III
Oct 19, 2021 6:26 AM UTC
Question

Genration of sine wave using DAC of Nucleo-H743zi2 development board

Hello,

I am using DAC module of STM32H743ZI2 micro controller to generate sine wave of 8KHz frequency I am using Timer2 for this setting using APB1 bus of 150MHz and prescaler of 74 and ARR register of 1.

>>The issue I am facing is jitter in the output wave form like +/-50Hz deviation from 8KHz.

>>I want also sine wave whose frequency varies from 5KHz to 20KHz in steps of 0.1Hz with a delay of 100ms between each frequency step with low jitter is that possible

This topic has been closed for replies.

11 replies

Georgy Moshkin
Senior
Oct 19, 2021 7:42 AM UTC

If your DAC buffer is not multiple of full sine period, then you need to generate values inside HAL_DAC_ConvHalfCpltCallback and HAL_DAC_ConvCpltCallback interrupts. For high sampling rates it could be done using arm_sin_q31 or arm_sin_q15, and global phase accumulator variable (for example, named phaseVal):

uint32_t phaseVal;
// ...
 
phaseStep=round(0x100000000*freq/sampleFreq); 
 
// ... loop in callback to generate new values for DAC in circular DMA mode:
for (int i=idx1;i<idx2;i++)
arm_sin_q31((phaseVal)>>1)
phaseVal=phaseVal+phaseStep;
audioBuffer[i]=...

note that phase accumulator does not require check for overflow and incremented endlessly. It wraps around to zero by itself and provides correct phase value. For personalized solution contact me through profile

Check my STM32 Bootloader - flash your STM32 with AES-encrypted binaries right from web browser!
GHARI.1
GHARI.1Author
Associate III
Oct 19, 2021 9:47 AM UTC

Is there any example of this code. Right now I am just storing sine wave values and sending to DAC using DMA.

Georgy Moshkin
Senior
Oct 19, 2021 9:55 AM UTC

It was a short example,

float freq = ... // frequency of generated tone
float sampleFreq = ... // your sampling frequency

in HAL_DAC_ConvHalfCpltCallback you generate one half of the signal using

idx1=0;
idx2=AUDIO_BUFFER_SIZE/2;

and in HAL_DAC_ConvCpltCallback 

idx1=AUDIO_BUFFER_SIZE/2;
idx2=AUDIO_BUFFER_SIZE;

Can't post whole source, it was from commercial product. You can contact me directly by email in profile

But I am sure you can figure it out from these short snippets.

Check my STM32 Bootloader - flash your STM32 with AES-encrypted binaries right from web browser!
LCE
Principal III
Oct 19, 2021 9:39 AM UTC

Also make sure that you are not using the internal RC oscillator (HSI) as a clock source, these usually have high jitter.

Georgy Moshkin
Senior
Oct 19, 2021 9:46 AM UTC

good point, In some cases you can use external reference signal (1sec, 32khz, etc..) , and perform timer input capture to measure CPU cycles and calculate correction coefficient for a frequency.

Check my STM32 Bootloader - flash your STM32 with AES-encrypted binaries right from web browser!
GHARI.1
GHARI.1Author
Associate III
Oct 19, 2021 9:49 AM UTC

How to use timer input capture . I didn,t get you