catalogue
1. Experiment contents and steps:
3.2 SysTick reload value register:
3.3 SysTick interrupt priority:
3.4 SysTick interrupt function:
5. Program design (Standard Library)
6. Program design (HAL Library)
7.SYSTICK tick timer
0. Bloggers understand:
SysTick is a timer, but this timer belongs to Cortex-M3 core (that is, all m3 core chips have this timer), which plays a compatible role. This timer is usually used for the "heartbeat" of the operating system; It is usually used as a delay in bare metal.
1. Experiment contents and steps:
Experiment content:
The "1S SysTick" is output through the serial port every 1S clock through the SysTick tick timer
Steps:
1. Configure SysTick control and status register (select clock source, enable SysTick and enable interrupt);
2. Configure SysTick reload value register (load the value of countdown);
3. Configure the interrupt priority of SysTick;
4. Configure the interrupt function of SysTick.
2. Hardware description
None.
3. Register description
Refer to "STM32F10xxx Cortex-M3 programming manual - English version" and "Cortex-M3 authoritative guide (Chinese)"
3.1 SysTick clock and enable:
Configure SysTick control and status register (address: 0xE000_E010) (STK_CTRL)
It mainly describes the second bit of the register CLKSOURCE, 0=AHB/8; 1=AHB
In this experiment, set the systick timer and select the clock source as the kernel clock (1=AHB=72M) to enable systick and systick exception requests (i.e. interrupts).
STK_CTRL = 0x0007;
3.2 SysTick reload value register:
SysTick will start to decrease from the reload value every time. If it decreases to 0, an exception will be generated, and the value of the reload register will be loaded into the counter again to cycle. It is recommended that the interrupt value should not be too short, which will cause the system to enter the interrupt frequently and increase the burden of CPU. Here we set the reload value to 10ms.
SysTick reload register (address: 0xE000_E014, STK_LOAD)
In this experiment, SysTick is set as a 10ms timer because the clock source selected in the previous step is the kernel clock (1=AHB=72M). Therefore, when the reload value is 720000, the time to count down to 0 is 10ms.
STK_LOAD = 720000;
3.3 SysTick interrupt priority:
The interrupt priority configured for SysTick is different from the register of external interrupt priority. Strictly speaking, the priority of SysTick is exception priority. Its registers are as follows
System exception priority register array (address: 0xE000_ED18 - 0xE000_ED23, SHPRx)
In this experiment, the interrupt priority of SysTick is 15, that is, the lowest priority.
SHPR12 = 15<<4; (because the upper four bits are the valid bits of the configuration priority, it is necessary to shift four bits to the left)
3.4 SysTick interrupt function:
In startup_stm32f10x_hd.s, the interrupt function of SysTick can be found as SysTick_Handler
In the experiment, 1S needs to be timed, while the timer of SysTick is 10ms. Therefore, when 100 interrupts are entered, 1S is timed. Define a variable with an initial value of 100. When it enters an interrupt, it will be reduced by 1. When it is finally reduced to 0, it means that it has been timed for 1S, and then carry out the corresponding operation.
4. Program design (register)
Source code: here is the key part selected by Shuai. See the source code in detail.
#define SysTick_FCLK_10ms 720000 #define SET_ SYS_ IP (irqx, PP) SCB - > SHP [((uint32_t) (irqx) & 0xf) - 4] = PP < < 4 / / set exception priority u16 systick_1s=100; //SYSTICK initialization void Systick_Config(void) { //Reload value SysTick->LOAD = SysTick_FCLK_10ms; SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | //Use internal clock 72M SysTick_CTRL_TICKINT_Msk | //Open interrupt SysTick_CTRL_ENABLE_Msk; //Enable Systick SET_SYS_IP(SysTick_IRQn,15); } void SysTick_Handler() { if(systick_1s!=0) systick_1s--; } /**********************main***************************/ //Main function int main(void) { SET_NVIC_GROUP(NVIC_PriorityGroup_4); //Interrupt group 4 Systick_Config(); //Initialize SysTick timer .........(ellipsis) USART1_Config(115200); //Serial port 1 configuration 115200 while (1) { .........(ellipsis) if(systick_1s == 0) //Judge whether to 1S { systick_1s = 100; //Reassign 1S USART1_Sends((u8*)"1S SysTick\r\n"); } } }
5. Program design (Standard Library)
In core_ cm3. The SysTick configuration function already exists in the H file, as shown below
The configuration method is the same as that of our register configuration, so I won't explain it in detail.
The input parameter is the value of reload, i.e. 720000 (10ms Systick interrupt)
The interrupt function and main function are consistent with the register.
Source code: here is the key part selected by Shuai. See the source code in detail.
#define SysTick_FCLK_10ms 720000 //Main function int main(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); //Set interrupt priority group 4 .........(ellipsis) SysTick_Config(SysTick_FCLK_10ms); USART1_Config(); //Serial port 1 configuration 115200 while (1) { .........(ellipsis) if(systick_1s == 0) { systick_1s = 100; //Reassign 1S USART1_Sends((u8*)"1S SysTick\r\n"); } } } void SysTick_Handler() { if(systick_1s!=0) systick_1s--; }
6. Program design (HAL Library)
In HAL, the configuration function of SysTick is consistent with that of the standard library. In core_cm3.h file.
Source code: here is the key part selected by Shuai. See the source code in detail.
#define SysTick_FCLK_10ms 720000 u16 systick_1s=100; void SysTick_Handler() { if(systick_1s!=0) systick_1s--; } /*********************main Functions*****************************/ int main(void) { HAL_Init(); SystemClock_Config(); SysTick_Config(SysTick_FCLK_10ms); //Set SysTick timer for 10ms LED_GPIOX_Config(); //LED initialization KEY_EXTI_Config(); //Key interrupt initialization USART1_Config(); //Serial port 1 configuration 115200 printf("SysTick_Test\r\n"); while (1) { if(systick_1s == 0) //Judgment 1S { systick_1s = 100; //Reassign 1S USART1_Sends((u8*)"1S SysTick\r\n"); } } }}
7. Experimental results
The serial port assistant will output "1S SysTick" every 1S.