AnsweredAssumed Answered

STM32F334R8 - Setting the timers for a sine waveform generator ?

Question asked by Antoine.PM on Jun 14, 2016
Latest reply on Jun 15, 2016 by Antoine.PM
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:

01.#include "mbed.h"
02.// Audio output demo for speaker
03.// generates a 500Hz sine wave on the analog output pin
04.// 128 data points on one sine wave cycle are precomputed,
05.// scaled, stored in an array and
06.// continuously output to the Digital to Analog convertor
08.AnalogOut DAC(p18); // conflict with the DAC name, I changed to AnalogOut dacout(PA_4)
09.//global variables used by interrupt routine
10.volatile int i=0;
11.float Analog_out_data[128]; //I tried to put 256 instead of all the 128 (just to test) but it doesn't work
13.// Interrupt routine
14.// used to output next analog sample whenever a timer interrupt occurs
15.void Sample_timer_interrupt(void)
17.    // send next analog sample out to D to A
18.    DAC = Analog_out_data[i];
19.    // increment pointer and wrap around back to 0 at 128
20.    i = (i+1) & 0x07F;
22. main()
25.    // set up a timer to be used for sample rate interrupts
26.    Ticker Sample_Period;
27.    // precompute 128 sample points on one sine wave cycle
28.    // used for continuous sine wave output later
29.    for(int k=0; k<128; k++) {
30.        Analog_out_data[k]=((1.0 + sin((float(k)/128.0*6.28318530717959)))/2.0);
31.        // scale the sine wave from 0.0 to 1.0 - as needed for AnalogOut arg
32.    }
33.    // turn on timer interrupts to start sine wave output
34.    // sample rate is 500Hz with 128 samples per cycle on sine wave
35.        //So when I put 10000 instead of 500 it outputs 1.3kHz
36.    Sample_Period.attach(&Sample_timer_interrupt, 1.0/(500.0*128));
37.    // everything else needed is already being done by the interrupt routine
38.    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 this thread 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 - AN3126
Extending DAC performances- AN4566

Thank you very much for your time.