2016-03-07 02:18 AM
Good morning to everyone.
I am trying to figure out how to toggle periodically a digital output pin with a given frequency. The system clock is configure the internal oscillator, so it should be 16 MHz.I set a timer with a prescaler of 101, so, if I understand correctly, it should count 1 every 100 cock tick.Now, for example, I am trying to toggle a pin evry 20 ms. If I understand correctly, 16 MHz, should be 16000000 clock cycles per second. If I count 1 every 100, the timer should be 160000 every second. 20 ms is 1/50 seconds, so 1600000/50 = 3200.In my code, I execute various type of operation in an infinite cycle. When I have to evaluate if 20 ms are passed, I verify the STM counter. If 3200 counts are passed from before, I change the output.The control implemented is something like this: ULONG ulNow = STM.CNT.R; ulElapsedTime = ulNow - ulWaveTimer; if (ulElapsedTime >= 3200) /* Time passed. Change output and reset timers. */The fact is that It seems that from one check and the others pass too many clock cycles, so the frequency obtained is not what I desire, but less. Is there a more smart way to implement an output that change with a desired frequency?There is some fixed relation between the C code I write and the number of machine instructions that I obtain after compilation? Thank you, Ivan #clock-cycles-timer2016-04-07 08:25 AM
Hello Ivan ,
Sorry , for my late answer.Have you solved your issue ?Which device do you use ?About STM, if you use EL, The smart way to implement an output at the desired frequency is to change the CMP registerCMP set to 3200 in hexaCompare value for channel n. If the STM_CCRn[CEN] bit is set and the STM_CMPn register matchesthe STM_CNT register, a channel interrupt request is generated and the STM_CIRn[CIF] bit is set.and handle the associated vector number 30 0x01E0 16 Match on channel 0 STM_0 STM_131 0x01F0 16 Match on channel 1 STM_0 STM_132 0x0200 16 Match on channel 2 STM_0 STM_133 0x0210 16 Match on channel 3 STM_0 STM_1 Best regards Erwan2016-05-03 08:36 AM
Hello Erwan,
In the end I solved calling my function multiple times in a main cycle. The problem was that I had some complex calculations done by the micro and forced waits caused by the management of a display.Having multiple calls ensures that I catch when the timer matches a certain value and so change the output pin value accordingly.I also discovered that I can use the FlexPWM to generate waves with desired characteristics.Thanks for the answer. I will try your solution and decide what is best for the application on which I am working.2018-03-07 10:07 AM
hallo ivan
i recomendet to you to use the MCO master clock output
https://stm32f4-discovery.net/2014/10/library-40-output-clocks-stm32f4/
https://stackoverflow.com/questions/46139283/stm32-output-mco-configuration
2018-03-07 11:36 AM
The OP is using an SPC5 not an STM32
2018-03-08 01:42 AM
Yes , it is SPC5 Family
All the documents are located here :
http://www.st.com/en/automotive-microcontrollers/spc56el70l5.html
Best regards
Erwab