STM32F10x Study Notes SysTick Timer

Publisher:数字冲浪Latest update time:2016-12-20 Source: eefocusKeywords:STM32F10x Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The SysTick timer is integrated into the NVIC. Therefore, any microcontroller with a Cortex-M3 core has it. This study note uses the SysTick timer to implement the revolving lantern function.


The SysTick timer is very simple, with only four registers. The meaning of these four registers is very clear in the book "Cortex-M3 Definitive Guide", so I won't repeat it here. Let's just talk about what's special about SysTick on STM32. According to the CMSIS standard, the register names used when accessing these four registers in C language are as follows:

SysTick->CTRL
SysTick->LOAD
SysTick->VAL
SysTick->CALIB

 

The value of SysTick->CALIB is fixed to 9000, so a 1ms time base is generated only when the system tick clock is set to 9MHz (the maximum value of HCLK/8).


STM32 provides 2 clock sources:

0: AHB/8

1: Processor clock (AHB)


Therefore, SysTick->CTRL = 7 means using the processor clock as the clock source, enabling SysTick, and enabling the SysTick interrupt. When SysTick->CTRL = 3, the frequency is reduced to 1/8 of the original. 

There are four LEDs on my development board, corresponding to PD2, PD3, PD4 and PD7 of GPIO port D.


The following is an example program, which still sets the registers directly first.


  1. #include "stm32f10x.h"  

  2.   

  3. #define RCC_GPIO_LED RCC_APB2Periph_GPIOD  

  4. #define GPIO_LED_PORT GPIOD      

  5. #define GPIO_LED1 GPIO_Pin_2      

  6. #define GPIO_LED2 GPIO_Pin_3      

  7. #define GPIO_LED3 GPIO_Pin_4      

  8. #define GPIO_LED4 GPIO_Pin_7  

  9. #define GPIO_LED_ALL GPIO_LED1 |GPIO_LED2 |GPIO_LED3 |GPIO_LED4   

  10.   

  11.   

  12. void LED_Spark(void)  

  13. {  

  14.     static int state = 0;  

  15.     switch (state)  

  16.     {  

  17.     case 0:  

  18.         GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);  

  19.         GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED1);  

  20.         state++;  

  21.         break;  

  22.     case 1:  

  23.         GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);  

  24.         GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED2);  

  25.         state++;  

  26.         break;  

  27.     case 2:  

  28.         GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);  

  29.         GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED3);  

  30.         state++;  

  31.         break;  

  32.     case 3:  

  33.         GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);  

  34.         GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED4);  

  35.         state = 0;  

  36.         break;  

  37.     default:  

  38.         state = 0;  

  39.         break;  

  40.     }  

  41. }  

  42. int main(void)  

  43. {  

  44.     SystemInit();  

  45.     RCC->APB2ENR |= 0x00000020;  

  46.     GPIOD->CRL = 0x24422244; //PD2 PD3 PD4 PD7 Set to Output mode    

  47.       

  48.     SysTick->LOAD = 24000000/200;  

  49.     SysTick->CTRL = 3;       

  50.   

  51.     for(;;)  

  52.     {  

  53.     }     

  54. }  

  55.   

  56. /** 

  57.  * @brief This function handles SysTick Handler. 

  58.  * @param None 

  59.  * @retval None 

  60.  */  

  61. void SysTick_Handler(void)  

  62. {  

  63.     static int count = 0;  

  64.     count++;  

  65.     if (count == 100)   

  66.     {  

  67.         LED_Spark();  

  68.         count = 0;  

  69.     }  

  70. }  


Then there are examples of using the functions provided by the STM32 firmware library.


  1. #include "stm32f10x.h"  

  2.   

  3. #define RCC_GPIO_LED RCC_APB2Periph_GPIOD  

  4. #define GPIO_LED_PORT GPIOD      

  5. #define GPIO_LED1 GPIO_Pin_2      

  6. #define GPIO_LED2 GPIO_Pin_3      

  7. #define GPIO_LED3 GPIO_Pin_4      

  8. #define GPIO_LED4 GPIO_Pin_7  

  9. #define GPIO_LED_ALL GPIO_LED1 |GPIO_LED2 |GPIO_LED3 |GPIO_LED4   

  10.   

  11. void LED_Spark(void)  

  12. {  

  13.     static int state = 0;  

  14.     switch (state)  

  15.     {  

  16.     case 0:  

  17.         GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);  

  18.         GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED1);  

  19.         state++;  

  20.         break;  

  21.     case 1:  

  22.         GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);  

  23.         GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED2);  

  24.         state++;  

  25.         break;  

  26.     case 2:  

  27.         GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);  

  28.         GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED3);  

  29.         state++;  

  30.         break;  

  31.     case 3:  

  32.         GPIO_SetBits(GPIO_LED_PORT, GPIO_LED_ALL);  

  33.         GPIO_ResetBits(GPIO_LED_PORT, GPIO_LED4);  

  34.         state = 0;  

  35.         break;  

  36.     default:  

  37.         state = 0;  

  38.         break;  

  39.     }  

  40. }  

  41. int main(void)  

  42. {  

  43.     GPIO_InitTypeDef GPIO_InitStructure;  

  44.     SystemInit();  

  45.     SysTick_Config(SystemCoreClock/100);  

  46.   

  47.     /* Enable GPIOB, GPIOC and AFIO clock */  

  48.     RCC_APB2PeriphClockCmd(RCC_GPIO_LED, ENABLE); //RCC_APB2Periph_AFIO  

  49.       

  50.     /* LEDs pins configuration */  

  51.     GPIO_InitStructure.GPIO_Pin = GPIO_LED_ALL;  

  52.     GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_2MHz;  

  53.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  

  54.     GPIO_Init(GPIO_LED_PORT, &GPIO_InitStructure);  

  55.   

  56.     for(;;)  

  57.     {  

  58.   

  59.     }     

  60. }  

  61.   

  62. /** 

  63.  * @brief This function handles SysTick Handler. 

  64.  * @param None 

  65.  * @retval None 

  66.  */  

  67. void SysTick_Handler(void)  

  68. {  

  69.     static int count = 0;  

  70.     count++;  

  71.     if (count == 100)   

  72.     {  

  73.         LED_Spark();  

  74.         count = 0;  

  75.     }  

  76. }  


 

It should be noted that if the SysTick_Config function is used to set the SysTick interrupt frequency, the clock source cannot be specified manually. The clock source used at this time is the core frequency.

SystemCoreClock is a global variable, and its value is the operating frequency of the kernel, but the prerequisite is to call the SystemInit() function to set the kernel frequency. If the kernel frequency is set by byte writing registers, the value of SystemCoreClock may not be correct.


Keywords:STM32F10x Reference address:STM32F10x Study Notes SysTick Timer

Previous article:STM32 implements DAC output related settings
Next article:STM32 general timer---basic timing learning

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号