STM32外设驱动---SysTick精准延时

单片机:STM32F103
库函数:V3.5
描述:利用系统内核文件core_cm3.c内部SysTick实现1ms,1us的精准延时,时钟72MHz,涉及中断服务SysTick_Handler的注册。

delay.h

#ifndef     __DELAY_H__
#define     __DELAY_H__
#include "stm32f10x.h"

extern  void DelayMs(__IO uint32_t nTime);
extern  void DelayUs(__IO uint32_t nTime);

//this function for systick handler function
extern  void TimingDelay_Decrement(void);
#endif

delay.c

#include "delay.h"

static __IO uint32_t TimingDelay;

void DelayMs(__IO uint32_t nTime)
{
    if (SysTick_Config(72000))
    { 
        /* Capture error */ 
        while (1);
    }
    TimingDelay = nTime;
    while(TimingDelay != 0);
}

void    DelayUs(__IO uint32_t nTime)
{
    if (SysTick_Config(72))
    { 
        /* Capture error */ 
        while (1);
    }
    TimingDelay = nTime;
    while(TimingDelay != 0);
}

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

中断服务stm32f10x_it.c

void SysTick_Handler(void)
{
    TimingDelay_Decrement();
}

你可能感兴趣的:(嵌入式学习交流)