The MAX freq of sine wave depends on the trigger frequency and the number of samples. Let's say the HCLK is 150 MHz, and so is the timer frequency If you keep the prescalar 0, and use ARR as 10-1, this means the trigger frequency from the timer will be 15 MHz Now if you use 15 samples in the sine wave, you can generate the sine wave of 1 MHz 150 samples = 100 KHz 1500 samples = 10KHz And so on
nice done. You can use the symetry of sinus function to save some memory. Values in second half are equal to negative value of first half. (sin (x+180) = -sin(x). Values in first and second and values in third and fourth quorter are symetrical around x=90 and x=270 degrees. sin (x+90) = sin(90-x)
Thank you very much! I got it working! Chose to go with a uint16_t array and using Half Word's instead of Word's for Data Width at 8:41, since the DAC has 12-bit or 8-bit modes and I wanted to save memory. I needed to cast my uint16_t array to uint32_t to make the compiler stop throwing a warning. I'd though the function would expect a uint16_t array instead of a uint32_t array once you change the 8:41 Data Width parameter, but apparently not. Thanks for the "-1" tip at 9:20 and Content Assist Template Proposal (Ctrl+Space) demos throughout (eg, at 12:03);
Regarding the timer setting and frequency expected, my TIM6 clock speed (APB1 bus) shows 64Mhz. So, for a Timer event to trigger the dma every 8khz, I set the prescaler to 0, and TIM_ARR = (64M/8k)-1. However, with the DMA pointing to a 2 sample buffer (0 and 0xFFF), what I see on the scope is a square wave at 4khz with a 50% duty cycle!! Considering the Stm32h7's has a maximum DAC sampling rate of 18.18Msps, what I *expected* was that every 8khz the timer would cause 2 samples to get transferred from the memory buffer to the DAC peripheral, and therefore the scope should show it briefly at 0V, but most of the time it would be idle at 3.3V while it waits for the next timer event... yet this is not the case, why?? Instead it's acting as if only one sample is transferred every timer event, despite: HAL_DAC_Start_DMA ( &hdac1 , DAC_CHANNEL_1, pBuffer , *LENGTH_2* , DAC_ALIGN_12B_R);
The frequency of the square wave will also depend on the number of samples that you are talking. In your case, 8KHz /2 = 4KHz. Why is it 3.3v ? Well that depends on what values you are feeding to the DAC
I have an stm32G474. I tried this example and I can see that the DOR register is getting new values but the DAC doesnt output any values. Any reason why this can happen?
I did everything like in your tutorial. However, I'm getting a DAC/DMA underrun error immediatelly after I start the tranfer. Do you know what might be the issue? Thanks!
Hi I am working with STM32 H7 board. I want to get sine wave in two channels. I am can able to write date to two channels as a sine wave but the sine wave is not so smooth because i need sine wave of around some 10kHz max. But the sampling time is around some 50khz and it is varying. Can you help me with this?
Hi Sir, Thanks for video I followed the same as mentioned . i am using stm32f407vg discovery board . but i am getting DMA underrun error condition occurred for DAC channel2 (the currently selected trigger is driving DAC channel2 conversion at a frequency higher than the DMA service capability rate). i reduced the timer counter frequency to 500KHz but still facing same error.. can you guide ur suggestion
thats nice. i am trying to play tone with 22.05KHz sampling frequency. tone data is stored in internal flash. i successfully got sine wave of desired value but noit luck for tone playing. can you make video on this topic? thanks
Many Thanks for the video. I would like to know if it is possible to count number of senoidal waves generate with a counter variable and after a certain number of sinusoidal waves I stop create the waves for a period and recreate again. For example: 58 pulses of senoidal wave and 2 pulses of zero volts. This will be used in a car signal injection for test.
Yes it's possible if u are able to keep the timer frequency at 100 MHz and than use 100 samples, or 50 MHz and 50 samples. Remember that decreasing the number of samples will increase inaccuracy in the sine wave
@@ControllersTech thank you very much, i tried (CPU=180MHz, Timer=CPU/2=90MHz, ARR=9-1) = (10Mhz-100ns), (10MHz/(Sample=10))=1MHz sine wave and good quality, how can i increase power of that sine wave i need minimum 50mA output
Thank you for your video, however, your Sine wave has negative cycles too, it is from -1.77V to 1.36V. But the result I receive is from 0 to 3.3V. Can you please help?
Hi, how are you? im rly struggling to output data to internal 12b DAC from usb audio class, can you make tutorial for this? it looks like everything is there, but i dont know what data put to timer 6 to trigger DAC change as STM manual say.
@@ControllersTech Im making mini tesla coil, based on NiklasFauth project, but i want to change from midi to normal audio. I reprogramed everithing but usb audio to pwm (want to use imput data from DAC to make PWM audio) all based on STM32F072.
I am literally using an F446RE like you and copy pasted your code and settings in Cube... i can't generate anything... nothing on the oscilloscope no mater what... maybe some help? if i try to operate the DAC in a standard value like the first example everything is working fine... *Edit: i created a new STM32 Project and did only the sine wave part and it worked just fine... WTF?! is CubeIDE bugged or something?
never use the code as it is. Always generate a new one. I delete few things while uploading the code. This is why there is a video, and there is the process.
For me I can generate 100 Hz but 1kHz doesn't work :(. Voltage on pin drops to around 300mV... I have DS1054z scope..., and stm32f746zg. Using TIM6 with DMA. Edit: I was only able to generate a 1kHz frequency if I used 10 samples instead of 100. Where is my limit? What should I look for on datasheet? Or is it my scope? I doubt it
Well this depends on the trigger frequency and the number of samples. Let's say the HCLK is 150 MHz, and so is the timer 1 freq If you keep the prescalar 0, and use ARR as 10-1, this means the trigger frequency from the time will be 15 MHz Now if you use 15 samples, you can generate the sine wave of 1 MHz 150 samples = 100 KHz And so on
@@ControllersTech Still does not work. For example, I have TIM6 at 16MHz. prescalar 0, ARR 10-1, so 1.6MHz. Then 160 samples gives 10kHz. On the oscilloscope, still 350mV reading, flat line no sine wave.
Don't know what's going wrong on your end. Have you enabled the update event in the timer ? Today I have tested all possible frequencies i could and the response was always as per the calculation.
I'm having a similar problem using the STM32F4o7G-DISC1 board. I can only generate up to 105kHz. The HCLK is 168MHz and the external crystal is 8MHz, and if I put ARR to 1 and Prescalar to 0, I only get 105kHz. I also tried changing the sample number (ns) in the equation, but it just changes the shape of the wave generated and maintains the same frequency. Any solutions?
The MAX freq of sine wave depends on the trigger frequency and the number of samples.
Let's say the HCLK is 150 MHz, and so is the timer frequency
If you keep the prescalar 0, and use ARR as 10-1, this means the trigger frequency from the timer will be 15 MHz
Now if you use 15 samples in the sine wave, you can generate the sine wave of 1 MHz
150 samples = 100 KHz
1500 samples = 10KHz
And so on
Finally. Information on WHY you write the code this way instead of simply what the code is. Well done.
nice done.
You can use the symetry of sinus function to save some memory. Values in second half are equal to negative value of first half. (sin (x+180) = -sin(x).
Values in first and second and values in third and fourth quorter are symetrical around x=90 and x=270 degrees. sin (x+90) = sin(90-x)
Thank you! I really needed to know about the HAL_DAC_Start existence.
Thanks so much for the knowledge, I managed to get the stm32 to generate a sine wave with a frequency value = 60hz
Thank you very much! I got it working!
Chose to go with a uint16_t array and using Half Word's instead of Word's for Data Width at 8:41, since the DAC has 12-bit or 8-bit modes and I wanted to save memory. I needed to cast my uint16_t array to uint32_t to make the compiler stop throwing a warning. I'd though the function would expect a uint16_t array instead of a uint32_t array once you change the 8:41 Data Width parameter, but apparently not.
Thanks for the "-1" tip at 9:20 and Content Assist Template Proposal (Ctrl+Space) demos throughout (eg, at 12:03);
No need to enable Tim2 global interrupt,
thank you for the good explanation
Regarding the timer setting and frequency expected, my TIM6 clock speed (APB1 bus) shows 64Mhz. So, for a Timer event to trigger the dma every 8khz, I set the prescaler to 0, and TIM_ARR = (64M/8k)-1.
However, with the DMA pointing to a 2 sample buffer (0 and 0xFFF), what I see on the scope is a square wave at 4khz with a 50% duty cycle!! Considering the Stm32h7's has a maximum DAC sampling rate of 18.18Msps, what I *expected* was that every 8khz the timer would cause 2 samples to get transferred from the memory buffer to the DAC peripheral, and therefore the scope should show it briefly at 0V, but most of the time it would be idle at 3.3V while it waits for the next timer event... yet this is not the case, why?? Instead it's acting as if only one sample is transferred every timer event, despite:
HAL_DAC_Start_DMA ( &hdac1 , DAC_CHANNEL_1, pBuffer , *LENGTH_2* , DAC_ALIGN_12B_R);
The frequency of the square wave will also depend on the number of samples that you are talking. In your case, 8KHz /2 = 4KHz.
Why is it 3.3v ? Well that depends on what values you are feeding to the DAC
I have an stm32G474. I tried this example and I can see that the DOR register is getting new values but the DAC doesnt output any values. Any reason why this can happen?
I have an external DAC connected by I2S. Do I need a timer to get interrupt callback? Or will the I2S fire DMA interrupt if it wants data?
I did everything like in your tutorial.
However, I'm getting a DAC/DMA underrun error immediatelly after I start the tranfer. Do you know what might be the issue? Thanks!
hii Nice tutorial, Good explanation, thank you very much. It is exactly what i want in my project
Good explanation, thank you very much. It is exactly what i want :))
how to you remove the dc component ? are you using capacitor in the output ?
Hi I am working with STM32 H7 board. I want to get sine wave in two channels. I am can able to write date to two channels as a sine wave but the sine wave is not so smooth because i need sine wave of around some 10kHz max. But the sampling time is around some 50khz and it is varying. Can you help me with this?
hello , using this method , ho I can generate the two sinewave on DAC1 & DAC2 Simultaneously ?
hi. can it be made like RF generator say till 10 mhz or so, and with encoder changing frequency with 1khz steps?
hi any ideas about maximum frequency of a smooth sinewave using this dac unit?
Hello? How did you manage to pop up content assist in CubeIDE? Is there any way to do so without hitting ctrl+space? Thanks in advance.
I use ctrl+space. I don't know if there is any other way
indexer enable
Nice tutorial.
Do you know how can I show the wave on labview?
Hi Sir,
Thanks for video
I followed the same as mentioned . i am using stm32f407vg discovery board . but i am getting
DMA underrun error condition occurred for DAC channel2 (the currently selected trigger is
driving DAC channel2 conversion at a frequency higher than the DMA service capability rate). i reduced the timer counter frequency to 500KHz but still facing same error..
can you guide ur suggestion
i need to generate sine wave with 5hz frequency for 1ma current at dac V-I board through spi communication ... could you please help me on this
thats nice. i am trying to play tone with 22.05KHz sampling frequency. tone data is stored in internal flash. i successfully got sine wave of desired value but noit luck for tone playing. can you make video on this topic? thanks
Many Thanks for the video. I would like to know if it is possible to count number of senoidal waves generate with a counter variable and after a certain number of sinusoidal waves I stop create the waves for a period and recreate again. For example: 58 pulses of senoidal wave and 2 pulses of zero volts. This will be used in a car signal injection for test.
Yeah you can.. just put a variable inside the function, and increment it after the sine wave has been generated..
how to generate noise sinewave and low pass filter to filter out the harmonics and getting pure sine wave by stm cubemx
Hi!!! I bought STM32L4-RB-P protoboard but in STM32Cube i don't have DAC option. Does this mean this nucleo board doesn't have that option?
Yup it doesn't support
@@ControllersTech Thanks. Can u recommend me a nucleo board for getting and FM signal ADC then demodulate it and then DAC?
I thought about nucleo-l476rg. Is this one good enough?
What was max frequency you could generate sine wave?
Actually I haven't tested it. It depends on how many samples you are taking..
is it possible 1mhz sine wave
Yes it's possible if u are able to keep the timer frequency at 100 MHz and than use 100 samples, or 50 MHz and 50 samples. Remember that decreasing the number of samples will increase inaccuracy in the sine wave
@@ControllersTech thank you very much, i tried (CPU=180MHz, Timer=CPU/2=90MHz, ARR=9-1) = (10Mhz-100ns), (10MHz/(Sample=10))=1MHz sine wave and good quality, how can i increase power of that sine wave i need minimum 50mA output
I don't know about controlling current in this prospective..
awesome as ever
Hello, i'm using F1 Blue pill Series, how to use pin as DAC? thank you
Bluepill doesn't support DAC. You can use some external IC, but this tutorial won't work for it.
Is it possible to dynamically change the sine wave pattern(its frequency, as modulated signal) using DMA?
Yeah why not. Just change the frequency and amplitude in the sine function
@@ControllersTech Thank you !
Thanks for this project. Please do complete sinewave hbridge inverter with lcd and menu keypad to set all parameters.using stm32f
Thank you for your video, however, your Sine wave has negative cycles too, it is from -1.77V to 1.36V. But the result I receive is from 0 to 3.3V. Can you please help?
maybe your scope is set for the DC.. Check that..
Thanks for the video, very helpful
hii how are u?thank you its great..how can i generate a wave with Begin and end optional voltage and frequency that user choose it?
Just change the sinx to "a + sin(bx)". I hope you remember the graph of this function from your school days.. just kidding
Hhh funny..dont worry i remember but i wannt contro it with software and stm...bro why you didnt it? I can make it for you like iot
How we will control amplitude of sine wave ?
Use math
hi how are you?that great ..how can i generate a wave but concurrent that have negative and positive voltage?
Look at my nextion video on waveform. I have used the sine wave there too. Maybe that code can help you
Hi, how are you? im rly struggling to output data to internal 12b DAC from usb audio class, can you make tutorial for this?
it looks like everything is there, but i dont know what data put to timer 6 to trigger DAC change as STM manual say.
I am working on it. Let's hope everything works out..
@@ControllersTech this is rly great :3
@@ControllersTech Im making mini tesla coil, based on NiklasFauth project, but i want to change from midi to normal audio. I reprogramed everithing but usb audio to pwm (want to use imput data from DAC to make PWM audio) all based on STM32F072.
Actually i made it work, but with i2s and usb msc.
@@ControllersTech nice work!! :) can you share code please ? :) what to change to make it work with internal dac ?
how to change amplitude?
Check my latest video on nextion display. I have used sine wave there too, along with variation in amplitude and frequency
Nice tutorial :)
I am literally using an F446RE like you and copy pasted your code and settings in Cube... i can't generate anything... nothing on the oscilloscope no mater what... maybe some help? if i try to operate the DAC in a standard value like the first example everything is working fine... *Edit: i created a new STM32 Project and did only the sine wave part and it worked just fine... WTF?! is CubeIDE bugged or something?
never use the code as it is. Always generate a new one. I delete few things while uploading the code. This is why there is a video, and there is the process.
how can i make an oscilloscope using stm. Thanks
I wish it would be that simple
Thanks for the tutorial
very nice tutorial, thx
For me I can generate 100 Hz but 1kHz doesn't work :(. Voltage on pin drops to around 300mV... I have DS1054z scope..., and stm32f746zg. Using TIM6 with DMA.
Edit: I was only able to generate a 1kHz frequency if I used 10 samples instead of 100. Where is my limit? What should I look for on datasheet? Or is it my scope? I doubt it
Well this depends on the trigger frequency and the number of samples.
Let's say the HCLK is 150 MHz, and so is the timer 1 freq
If you keep the prescalar 0, and use ARR as 10-1, this means the trigger frequency from the time will be 15 MHz
Now if you use 15 samples, you can generate the sine wave of 1 MHz
150 samples = 100 KHz
And so on
@@ControllersTech Still does not work. For example, I have TIM6 at 16MHz. prescalar 0, ARR 10-1, so 1.6MHz. Then 160 samples gives 10kHz. On the oscilloscope, still 350mV reading, flat line no sine wave.
Don't know what's going wrong on your end. Have you enabled the update event in the timer ?
Today I have tested all possible frequencies i could and the response was always as per the calculation.
Try using hsi clock
I'm having a similar problem using the STM32F4o7G-DISC1 board. I can only generate up to 105kHz. The HCLK is 168MHz and the external crystal is 8MHz, and if I put ARR to 1 and Prescalar to 0, I only get 105kHz. I also tried changing the sample number (ns) in the equation, but it just changes the shape of the wave generated and maintains the same frequency. Any solutions?
I only have one sine wave when I reset the code. After that, I have a fix voltage. How can I fix it ? How can I have a infinite signal ?
What? What you mean reset the code?
Infinite signal ?
Did you set the DMA to circular mode ?
good video. but the background music is very distracting.
how i can generate a triangle wave using the triangle generator??
thanks for the video, i'm allways share it with my partners
There is a section in the document linked in the description about triangular waveform..
STM library comes with examples. Find corresponding examples like Examples\DAC\DAC_SignalsGeneration in Cube repository.
Thank you
NIce, thanks!