初中生
最后登录1970-1-1
在线时间 小时
注册时间2020-3-3
|
/*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 "bsp_systick.h"文件
void SysTick_Handler(void)
{
TimingDelay_Decrement();
}
/*main.c*/
#include "stm32f10x.h"
#include "bsp_led.h"
#include "bsp_systick.h"
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);
}
}
|
|