Introduction and features of SysTick
-1- SysTick belongs to the system clock.
-2- SysTick timer is bundled in NVIC.
-3- SysTick can generate interrupts, and interrupts cannot be masked.
SysTick clock source view
It can be seen from the clock tree that the maximum ticking clock is 72MHZ/8=9MHZ
This conclusion can also be reached through the Chinese reference manual
Code writing (verified and can be copied and used directly)
SysTick source file
#include "Systick.h"
static u8 fac_us=0;
static u16 fac_ms=0;
void Sys_Tick_Init(void)
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
fac_ms=SystemCoreClock/8000;
fac_us=SystemCoreClock/8000000;
}
void Delay_Ms(u16 nms)
{
u32 temp;
//倒数值,fac_ms代表一毫秒所需节拍数,与形参乘法运行得到所需的全部节拍数
SysTick->LOAD=(u32)nms*fac_ms;
//清空计数器
SysTick->VAL =0x00;
//对控制寄存器第0位写1代表始能
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;
//这句话的意思控制寄存器是使能的且时间还未到达,就会一直卡在此处
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(1<<16)));
//对控制寄存器第0位写1代表示除能
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;
//清空计数器
SysTick->VAL =0x00;
}
void Delay_Us(u32 nms)
{
u32 temp;
//倒数值,fac_ms代表一毫秒所需节拍数,与形参乘法运行得到所需的全部节拍数
SysTick->LOAD=(u32)nms*fac_us;
//清空计数器
SysTick->VAL =0x00;
//对控制寄存器第0位写1代表始能
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;
//这句话的意思控制寄存器是使能的且时间还未到达,就会一直卡在此处
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(1<<16)));
//对控制寄存器第0位写1代表示除能
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;
//清空计数器
SysTick->VAL =0x00;
}
main function
#include "stm32f10x.h"
#include "Systick.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
int main(void)
{
Sys_Tick_Init();
LED_Init();
while(1)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
GPIO_ResetBits(GPIOE,GPIO_Pin_5);
Delay_Ms(500);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
Delay_Ms(500);
}
}
Project Acquisition
After three consecutive clicks on the avatar below