/*bsp_systick.c*/
#include "bsp_systick.h"
static uint32_t TimingDelay;//全局变量
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
void SysTick_Init(uint32_t ticks,uint32_t priority)
{
SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;//1ms定时周期 ticks=72000
NVIC_SetPriority (SysTick_IRQn, priority);//设置中断优先级 取值为0~15
SysTick->VAL = 0;//计数器清零
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | //配置AHB为时钟
SysTick_CTRL_TICKINT_Msk; //使能中断
}
void SysTick_Delay_ms(uint32_t ms)
{
uint32_t i =0;
TimingDelay = ms;//赋值全局变量
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; //使能计数器
for(i=0; i<ms; i++)
{
while(TimingDelay != 0)
;
}
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;//关计数器
}
/*stm32f10x_it.c*/ //这里不要忘记调用自己写的#include &quot;bsp_systick.h&quot;文件
void SysTick_Handler(void)
{
TimingDelay_Decrement();
}
/*main.c*/
#include &quot;stm32f10x.h&quot;
#include &quot;bsp_led.h&quot;
#include &quot;bsp_systick.h&quot;
int main()
{
LED_GPIO_Config();
SysTick_Init(72000,0);//定时周期72000/72M=1ms,中断优先级0~15
LED_G(OFF);
LED_B(OFF);
LED_R(OFF);
while(1)
{
LED_G(OFF);
SysTick_Delay_ms(100);
LED_G(ON);
SysTick_Delay_ms(100);
}
} |