Timers

You might also like

Download as pdf
Download as pdf
You are on page 1of 14
FAQs All community Search all content STMicroelectronics Community © Knowledge base © STM32 MCUs How to generate a one second interrupt using an ST... Options How to generate a one second interrupt using an STM32 Timer ST AME Support NF ST Employee o on 2021-10-21 01:09 AM How do | generate a one second i using an STM32 Timer? iterrupt 1. Introduction It is very common that applications require a periodic interrupt that is used as a time-base for triggering tasks, adding delays, keeping track of elapsed time, etc. While this article shows how to configure an ‘STM32 Timer to generate an interrupt every second, itis easy to change some parameters for other periodic rates. 2. Pre-requisite Hardware * Nucleo-Go70RB yoeqpee4 * Micro USB cable used to power the Nucleo board from a host machine and to load the code into ‘the STM32, Software © STM32CubelDE 3. Theory In this article we will use a general STM32 timer in order to generate an interrupt every second. We could have used the Systick or the RTC (Real Time Clock), but in this article we will use a simple timer, timer 3 (TIM), in an STM32G0 Microcontroller. TIM3 is one of many timers embedded in the STM32. Microcontrollers. eet ede rao) Connectivity oe TRL LULg yy ror emu Wien Ee Analog Control Encryption TIMB contains many components as shown in the following block diagram. While the timer supports a number of functions and features, only a subset is needed to generate a periodic one second interrupt. The following components of the timer block are used and configured for this example: ‘© Input clock (clock source for the timer): This clock, called Internal clock (CK_INT), comes from the RCC (Reset and Clock Control) block and is prescaled from the peripheral clock (APB) which is prescaled from the AHB clock. ‘* Prescaler input clock, CK_PSC, comes from the Trigger controller and is the same as the input clock ‘© PSC prescaler divides its input clock, CK_PSC, and outputs it as CK_CNT ‘© CK_CNTis the input clack to the CNT counter that is used to increment it + Auto-reload register (ARR) is loaded with the value that the counter increments to. When the value is reached, an update interrupt is generated, the counter is cleared and restarts counting, again. The value loaded into the ARR determines the periodic rate of the timer interrupts. engin eT eae ue ETR_ [Palani sateaion F600 | TRGO. aie i SaeER keen aoe e ~ nee ese om exese FE |. mena Tea Beans ier Brea os Ta a [ea Nt rc sie ta rua cat T2811 es ra9 ]oc2 | nats. an 5 See ca ea fe pa oe scout —TH01 [=| rll Sy oe eT ana, | |B) Mp [Tneee])!4 act APS [GauseiConpare srg JOCAREF Pps oct Jct ormnccHe In this part we will review the various calculations necessary to configure TIM3 to generate an interrupt every second, First of all TIM3 input clock (TIMBCLK) is set to APB1 clock (PCLK1), ‘APB1 prescaler will be set to 1. ‘So TIM3CLK = PCLK1 ‘And PCLK1 = HCLK => TIMBCLK = HCLK = System Core Clock In this example we will run the STM3260 at its maximum speed which is 64 MHz So SystemCoreClock is set to 64 MHz. ‘To get TIM3 counter clock at 10 KHz, the Prescaler is computed as following: Prescaler = (TIM3CLK / TIM3 counter clock) - 1 Prescaler = (SystemCoreClock /10 KHz) ~ 1 = (64 MHz / 10 KH2)~ 6400 ~ 6399 The TIM3 ARR (Auto-Reload Register) value which is the Period is equal to 10000 - 1, Update rate = TIM3 counter clock / (Period + 1) = Hz, This results in an interrupt every 1 second. When the counter value reaches the auto-reload register value, the TIM update interrupt is generated and, in the handler routine, pin PAS (connected to LED4 on board NUCLEO-GO7ORB) is toggled. Now that we have calculated the necessary configuration values, let’s walk through the steps to generate the code to for the timer to trigger an interrupt every second. 4. Steps 4. Open STM32CubelDE 2. Create a new project using the NUCLEO-GO70RB board Board Fits: eB mo Featu.. LargePict.. Docs &Rerour... [3] Datach... Ch Commerc Fed [iucuso-covore Boards List tom Vendor Type MCUIMPU Series other Petpherl 3. Give a name to the project Wistm22 Project o Setup STM32 project C/sers/nicols filor/STM32CubelDE/workspace 160 [RISE Options Targetad Language Oc Oc Targeted Binary Type © Executable O Static ibrary Taugetad Project Type @stm32cube © Empty 4. Initialize all peripherals with their default settings |[S Board Project Options: ers ese ceetttter C= = 5. Set the System Clock frequency ‘We are going to set HCLK to the maximum of 64 MHz: a ° Resolve Clock Issues ‘ Setting the APB Presecaler to 1 results in the APB Peripheral Clock (PCLK) set to 64 MHz also. G4_]toPower ats) Ga HCL to ate bus, coe, 4 |roccones System timer (ts) 4 |Fcux cones cook tuts sP8 peripheral locks gat 4 _|APB me clocks sts 6. Select TIM3 Note that while you can use any timer, TIM3 is used in this example. You will find TIM3 in the Pinout & Configuration Tab of the STM32CubelDE under “Timers"” Pinout & Configuration Se Software Q v| Categories | A->Z System Core? Analog > RTC 7. Select internal clock as clock source for TIM3: In the TIM3 Mode and Configuration, select Internal Clock as Clock Source. TIM3 Mode and Configuration SlaveMode Disable 7 Trigger Source Disable Clock Source _ Internal Clock Channell Disable Channel2 Disable ¥ Channels Disable - 8. Configure TIM3 In the Parameter Settings we will input the values we calculated earlier in the Theory section. ‘We will set 6399 for Prescaler ‘And use 10000 for the Counter Period @ DMA Settings Pee ome © User Constant: \Configure the below parameters : © oO @ » Counter Settings Prescaler (PSC - 16 bit...6399 Counter Mode Counter Period (AutoR... 10000 Internal Clock Division ... auto-reload preload Disable 9. Enable TIM3 global interrupts TIM3 Mode and Configuration j Reset Configuration @ NVIC Settings © Parameter Settings NVIC Interrupt Table ITIM3 global interrupt © DMA Settings See cus) 10. Generate Code Save the project, that will also generate the code. 11. Add code to toggle the LED In main.c, add the following code in the main function (this code is used to Start the Timer TIM3 in Interrupt mode}: /* USER CODE BEGIN 2 */ if (HAL_TIM_Base_Start_IT(&htim3) != HAL_OK) { /* Starting Error */ Error_Handler(); + /* USER CODE END 2 */ In stm32g0xx_it.c, add the following code in TIM3_IRQHandler (Timer 3 Interrupt Service Request) which will toggle the LED: /* USER CODE BEGIN TIM3_IRQn 1 */ HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN Pin); /* USER CODE END TIN3_IRQn 2 */ 12, Build the project, enter debug mode and run the code Build Project e startu Build Working Set > After the project is built, enter a debug session and then click on the Resume icon to run the code. ~@@ Run veero-vinie % Debug a Run History > © RunAs > Run Configurations. Debug Histany Tg CONTIGUTATONS— Breakpoint Types > Toggle Breakpoint Ctrl+Shift+B Toggle Line Breakpoint Toggle Watchpoint Toggle Method Breakpoint Skip All Breakpoints CtrltAlteB Remove All Breakpoints Extemal Tools a factor Navigate Search Pr ortex-M C++ Application} ‘While the code is running, the green LED is toggled every second as configured. 5, Links STM32G070 Datasheet Arm® Cortex®.MOs 32-bit MCU, 128 KB Flash, 36 KB RAM, 4x USART, timers, ADC, DAC, comm, W/Fs, .7- 3.6V (st.com) STM32G070 Reference Manual SIM32G0x0 advanced Arm*-based 32-bit MCUs - Reference manual STM32CubelDE ‘STM32CubelDE - Integrated Development Environment for STM32 - STMicroelectronics NUCLEO-GO7ORB: NUCLEO-GO70RB - STIMI32 Nucleo-64 development board with STM32G070RB MCU, supports Arduino and ‘ST-morpho connectivity - STMicroelectronics COMMENTS fo) jovial_canopy Y) “Associate Il Great and clear post. Thank you! wQ Associate III 2022-01-24 11:19 PM 2022-05-02 05:51 PM Thanks. | did HAL_TIM_Base_Start() instead of HAL_TIM_Base_Start_IT() until | saw this post. Version history Last update: 2021-10-21 01:09 AM Updated by: ST AME Support NF ky te cugmented About STMicroelectronics Who we are Investor relations Sustainability Innovation & technology Careers Blog Compliance, ethics & privacy Ethies and compliance ST ethics hotline Privacy portal Follow us o Lo Connect with us Contact ST offices Find sales offices & distributors Community Newsroom Events & trainings Top Browse Shortcuts Sitemap All rights reserved © 2024 STMicroelectronics Terms of use Sales terms & conditions Trademarks Privacy portal Manage cookies

You might also like