Multiple Timer Interrupt Example
This example is just like the previous one except this time the code is written with my SPL functions and two timers are running simultaneously with different clock frequencies. LEDs connected to GPIOC 6 and 7 pins flash to show the update rates of these timers. An advance and a GP timer are used in the demo.
TIM3_SR = 0;
TIM8_SR = 0;
setup_GPIOC(6, (GPIO_PP_output | output_mode_medium_speed));
setup_GPIOC(7, (GPIO_PP_output | output_mode_medium_speed));
TIM3_PSC = 575;
TIM3_ARR = 62499;
TIM8_PSC = 575;
TIM8_ARR = 62499;
Demo video link: https://www.youtube.com/watch?v=jyPUu5JqZyU.
External Clock Drive/Counter Example
In the beginning I stated that timers can be driven with external clock sources. STM32 timers can be driven with three types of external sources. We can use this facility to count pulses coming from a sensor like a Hall position sensor. We can also cascade several such timers to form a high precision timer. In this example, we will see how this is done.
The following hardware setup is used.
Please refer to the code. Note GPIOA 0 pin is used as the input of the external clock source. Here I connected a button to stimulate the external clock source. Note that the AFIO block has been enabled but no pin remapping is used. I avoided pin remapping to keep things simple although pin remapping is not hard. The following code sets up external clock properties.
As you can see the external clock source will only be sensed during rising edge transitions. Its input channel enabled and selected.
The rest of the code in the timer setup simply state how much to count before invoking an interrupt and should there be any prescaling for the incoming signal. This code makes my STM32 board sense two rising edges before invoking an interrupt and toggling a LED connected to GPIOC 6 pin. Pretty simple.
TIM2_SR = 0;
setup_GPIOC(6, (GPIO_PP_output | output_mode_high_speed));
TIM2_ARR = 0x0002;
TIM2_PSC = 0x0000;
Demo video link: https://www.youtube.com/watch?v=jCVWCoJeqec.
well done sir
How to set the PWM with Center aligned mode ?
Read this app note: http://www.st.com/web/en/resource/technical/document/application_note/DM00042534.pdf
Now need the sample code to change the CMS bits different from ’00’
Great tutorials! Best I’ve found so far.
first of all i like to say a million thanks for this tutorial and i have been waiting for this
tutorial i am your biggest fan in stm32 programming for there is little information about it on the web you’ve motivated me to keep having interest in on knowing about the 32bit micros
Thanks…. People like you keeps me going…. 🙂
Pingback: STM32 Timers - Arduino collector blog