1. 程式人生 > >STM32外設驅動---SysTick精準延時

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(); }