cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F334R8 - Setting the timers for a sine waveform generator ?

antoine
Associate II
Posted on June 14, 2016 at 17:32

Hi everyone,

I'm quite new in the world of stm32 and microcontrollers in general. Here is the thing: I want my f334 to generate a sine waveform which is going out by the DAC. I want it to adapt the frequency of the sine depending of a voltage it receives as an input, but basically I try to have 170kHz as output. I mainly use mbed, and very recently tried Keil. I found several examples, and I tried to use it as a start, but I can't go far. Here is the code I use, from github:

https://gist.github.com/donghee/9748245


#include ''mbed.h''

// Audio output demo for speaker

// generates a 500Hz sine wave on the analog output pin

// 128 data points on one sine wave cycle are precomputed,

// scaled, stored in an array and

// continuously output to the Digital to Analog convertor


AnalogOut DAC(p18); 
// conflict with the DAC name, I changed to AnalogOut dacout(PA_4)

//global variables used by interrupt routine

volatile 
int
i=0;

float
Analog_out_data[128]; 
//I tried to put 256 instead of all the 128 (just to test) but it doesn't work


// Interrupt routine

// used to output next analog sample whenever a timer interrupt occurs

void
Sample_timer_interrupt(
void
)

{

// send next analog sample out to D to A

DAC = Analog_out_data[i];

// increment pointer and wrap around back to 0 at 128

i = (i+1) & 0x07F;

}


int
main()

{

// set up a timer to be used for sample rate interrupts

Ticker Sample_Period;

// precompute 128 sample points on one sine wave cycle 

// used for continuous sine wave output later

for
(
int
k=0; k<128; k++) {

Analog_out_data[k]=((1.0 + sin((
float
(k)/0*6.28318530717959)))/2.0);

// scale the sine wave from 0.0 to 1.0 - as needed for AnalogOut arg 

}

// turn on timer interrupts to start sine wave output

// sample rate is 500Hz with 128 samples per cycle on sine wave

//So when I put 10000 instead of 500 it outputs 1.3kHz

Sample_Period.attach(&Sample_timer_interrupt, 1.0/(500.0*128));

// everything else needed is already being done by the interrupt routine

while
(1) {}

}

The problem is, it works for this frequency of 500Hz, but when I try higher frequencies (like 1kHz, 5kHz, 10kHz etc.) it can't go higher than 1.3kHz. I have read many, many forums, datasheets, codes, to try to understand by myself how to do it, but I failed. I used AN3126 because it seems to be exactly what I'm looking for, but when I look at the setting the frequency part, it just says to configure TIMER6, or TIM6_TRG, more or less. I have found

/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Timer%20value%20calculation&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=6536

where someone had to use timers and all, but I don't know where i should put the whole initialization part (this is a minor detail, though, I think the main.cpp is fine), and more, I can't find something like TIM_TimeBaseStructure in my libraries, for example. My big problem is that mbed is very clean and this is cool, but I don't know what lies for real in the mbed libraries. So I tried to export my project into Keil, and what I see is that many libraries are HAL (like

stm32f3xx_hal_tim.h

or

stm32f3xx_hal_dac.h

for example), which is something else that disturbs me. Also, I may have to use the integrated PLL, but I put it aside at first because I thought the input and output clock were too high for what I need, but now I'm starting to think I was wrong. But, meh, I can't manage a PLL either.... I hope some of you could help me, in any way, because I really need it. I'm stuck on this for two or three weeks (yes I'm bad) and I'm running out of time. Sometimes lurking is not enough. What I understand is that the DAC is bounded to timer6 (and timer7), and APB1 plays a part in that. I don't ask for the solution, of course, only for method, advice, anything. This is something I really want to get into, but it looks like I can't do it on my own. If you need any technical information or else, I will gladly provide them; I just didn't know what could be useful. Here are the two files I used (or try to use): Audio and waveform generation using the DAC -

http://www.st.com/content/ccc/resource/technical/document/application_note/05/fb/41/91/39/02/4d/1e/CD00259pdf/files/CD00259pdf/jcr:content/translations/en.CD00259pdf

Extending DAC performances-

http://www.st.com/content/ccc/resource/technical/document/application_note/6f/35/61/e9/8a/28/48/8c/DM00129pdf/files/DM00129pdf/jcr:content/translations/en.DM00129pdf

Thank you very much for your time. #trigger #sine-waveform #stm32f334 #timer
3 REPLIES 3
Posted on June 14, 2016 at 18:03

Can't help you with mbed or HAL..

The DAC typically has an output rate of up to 1Msps, you're not going to be able to do a 128 point 170 KHz (21.76 MHz) with it. You're also not going to be able to use interrupts beyond a few hundred KHz, so this will put a ceiling on that method too. To get higher rates you'd need to use DMA to service the DAC via a timer. The DAC doesn't support floats, it uses 12-bit integers, you'll have to preformat the pattern buffer (wave table) to fit the hardware expectations.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 14, 2016 at 18:16

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/sawtooth&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=97]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2Fsawtooth&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=97

Dual DMA DAC example half way down here, search ''Dual DAC 6.25 KHz Sine''

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Help%20with%20simple%20ADC%20on%20STM32F4&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
antoine
Associate II
Posted on June 15, 2016 at 10:33

Hello clive, thank you for your quick answer.

I'm going to study carefully the two links you gave me, take your advice in account in my thought process, and I will keep you in touch very soon.

Thanks again :)

Edit: I almost forgot, but it's in fact a major problem I have:

where are the libraries for my device?

I guess they're all grouped in a TARGET_NUCLEO folder generated when I select my device, but I can't find them.

I apologize for some questions that may seem very basic, but the wonderful thing is that i can't find the answers on the internet. Like blinky.cpp for my F334, I have no idea where I can have it.