stm32利用SysTick实现精确定时

 

stm32定时器资源虽然丰富,但是在一些项目中,我们任然希望不借助传统定时器实现精确延时,这样在面对一些对定时器资源需求多的项目我们剩下资源用来做该做事情。STM32用的是ARM Cortex-Mx系列的内核,该核心具有一个滴答时钟,这个滴答时钟大意就是在配置完时钟源和使能以后能进入到中断处理函数中。是为了方便一些操作系统,诸如ucOS等系统,的移植,作为任务调用的同步节拍。以下是在stm32f103上利用SysTick来实现精确延时的步骤。

 

第一步:配置时钟源

  1. void SysTick_Configuration(void)  
  2. {  
  3.     /* Setup SysTick Timer for 1 uS interrupts */  
  4.     /* SystemCoreClock isdefined in ¡°system_stm32f10x.h¡± and equal to HCLK frequency */  
  5.     if (SysTick_Config(72))   
  6.     {  
  7.         /* Capture error */  
  8.         while (1);  
  9.     }  
  10.     NVIC_SetPriority (SysTick_IRQn, 3);                   // the highest priority */  
  11. }  
void SysTick_Configuration(void)
{
	/* Setup SysTick Timer for 1 uS interrupts */
	/* SystemCoreClock isdefined in ¡°system_stm32f10x.h¡± and equal to HCLK frequency */
	if (SysTick_Config(72)) 
	{
		/* Capture error */
		while (1);
	}
	NVIC_SetPriority (SysTick_IRQn, 3);                   // the highest priority */
}

第二步:写中断处理函数

  1. void SysTick_Handler(void)  
  2. {  
  3.     if(TimingDelay != 0)  
  4.         TimingDelay --;  
  5. }
void SysTick_Handler(void)
{
	if(TimingDelay != 0)
		TimingDelay --;
}

第三步:写延时函数

  1. volatile unsigned int TimingDelay = 0;  
  2. void Delay_mS(unsigned int n)  
  3. {  
  4.     TimingDelay = n * 1000;  
  5.     while(TimingDelay !=0);  
  6. }  
  7.   
  8. void Delay_uS(unsigned int n)  
  9. {  
  10.     TimingDelay = n;  
  11.     while(TimingDelay !=0);  
  12. }  
volatile unsigned int TimingDelay = 0;
void Delay_mS(unsigned int n)
{
	TimingDelay = n * 1000;
	while(TimingDelay !=0);
}

void Delay_uS(unsigned int n)
{
	TimingDelay = n;
	while(TimingDelay !=0);
}

完成

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值