STM32 之 SysTick

基于中断方式延时效果

1.SysTick配置

void SysTick_Init(void)
{
/* SystemCoreClock / 1000 1ms
* SystemCoreClock / 100000    10us
* SystemCoreClock / 1000000 1us
*/
while(SysTick_Config( SystemCoreClock / 1000));    //
}

2.中断配置

void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

    /* Configure one bit for preemption priority */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);           //
      
    /* Enable the EXTI0_IRQn Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = SysTick_IRQn;        //
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;        //
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;            //
    NVIC_Init(&NVIC_InitStructure); 
}

3.中断函数入口

void SysTick_Handler(void)
{
  if (TimingDelay != 0x00)
  { 
    TimingDelay--;
  }
  
  
}

4. 延时函数使用

void Delay(u32 nTime)
{ 
  TimingDelay = nTime;

  while(TimingDelay != 0);
}

5.整体调用

int main(void)
{        
     SysTick_Init();
     LED_GPIO_Config();
     NVIC_Configuration();    
    while (1)
    {
         Delay(1000);//        
     /*  ~~~~~~~~~*/    
    }
}
原文地址:https://www.cnblogs.com/yeshuimaowei/p/5157163.html