stm32利用SysTick实现精确定时

来源:互联网 发布:.sql注释 编辑:程序博客网 时间:2024/04/30 09:07

 

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

 

第一步:配置时钟源

[cpp] view plaincopyprint?
  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. }  

第二步:写中断处理函数

[cpp] view plaincopyprint?
  1. void SysTick_Handler(void)  
  2. {  
  3.     if(TimingDelay != 0)  
  4.         TimingDelay --;  
  5. }

第三步:写延时函数

[cpp] view plaincopyprint?
  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. }  

完成

0 0
原创粉丝点击