2020-07-07 19:56:01 +08:00

253 lines
6.7 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "delay.h"
#include "sys.h"
//////////////////////////////////////////////////////////////////////////////////
//<2F><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>OS,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ucosΪ<73><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
#if SYSTEM_SUPPORT_OS
#include "includes.h" //֧<><D6A7>OSʱ<53><CAB1>ʹ<EFBFBD><CAB9>
#endif
//////////////////////////////////////////////////////////////////////////////////
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>ѧϰʹ<CFB0>ã<EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><CEBA><EFBFBD>;
//ALIENTEK STM32<33><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//ʹ<><CAB9>SysTick<63><6B><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD>ӳٽ<D3B3><D9BD>й<EFBFBD><D0B9><EFBFBD><><D6A7>OS)
//<2F><><EFBFBD><EFBFBD>delay_us,delay_ms
//<2F><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>@ALIENTEK
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳:www.openedv.com
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:2015/12/4
//<2F><EFBFBD><E6B1BE>V1.1
//<2F><>Ȩ<EFBFBD><C8A8><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؾ<EFBFBD><D8BE><EFBFBD>
//Copyright(C) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿƼ<D3BF><C6BC><EFBFBD><EFBFBD>޹<EFBFBD>˾ 2014-2024
//All rights reserved
//********************************************************************************
//<2F>޸<EFBFBD>˵<EFBFBD><CBB5>
//V1.1 20160418
//<2F>޸<EFBFBD>delay_init<69><74><EFBFBD><EFBFBD>fac_ms<6D>ļ<EFBFBD><C4BC>ʽ<E3B7BD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>delay_ms<6D>ڷ<EFBFBD>8M<38><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD>
//////////////////////////////////////////////////////////////////////////////////
static u8 fac_us=0; //us<75><73>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
static u16 fac_ms=0; //ms<6D><73>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><>os<6F><73>,<2C><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ms<6D><73>
#if SYSTEM_SUPPORT_OS //<2F><><EFBFBD><EFBFBD>SYSTEM_SUPPORT_OS<4F><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><CBB5>Ҫ֧<D2AA><D6A7>OS<4F><53>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UCOS).
//<2F><>delay_us/delay_ms<6D><73>Ҫ֧<D2AA><D6A7>OS<4F><53>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OS<4F><53><EFBFBD>صĺ궨<C4BA><EAB6A8><EFBFBD>ͺ<EFBFBD><CDBA><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD><D6A7>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><33><EFBFBD><EFBFBD><EAB6A8>:
// delay_osrunning:<3A><><EFBFBD>ڱ<EFBFBD>ʾOS<4F><53>ǰ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C>Ծ<EFBFBD><D4BE><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD>غ<EFBFBD><D8BA><EFBFBD>
//delay_ostickspersec:<3A><><EFBFBD>ڱ<EFBFBD>ʾOS<4F><EFBFBD><E8B6A8>ʱ<EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD>,delay_init<69><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>systick
// delay_osintnesting:<3A><><EFBFBD>ڱ<EFBFBD>ʾOS<4F>ж<EFBFBD>Ƕ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD>,<2C><>Ϊ<EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><E6B2BB><EFBFBD>Ե<EFBFBD><D4B5><EFBFBD>,delay_msʹ<73>øò<C3B8><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//Ȼ<><C8BB><EFBFBD><EFBFBD>3<EFBFBD><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:
// delay_osschedlock:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OS<4F><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
//delay_osschedunlock:<3A><><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD>OS<4F><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>¿<EFBFBD><C2BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// delay_ostimedly:<3A><><EFBFBD><EFBFBD>OS<4F><53>ʱ,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
//<2F><><EFBFBD><EFBFBD><EFBFBD>̽<EFBFBD><CCBD><EFBFBD>UCOSII<49><49>UCOSIII<49><49>֧<EFBFBD><D6A7>,<2C><><EFBFBD><EFBFBD>OS,<2C><><EFBFBD><EFBFBD><EFBFBD>вο<D0B2><CEBF><EFBFBD><EFBFBD><EFBFBD>ֲ
//֧<><D6A7>UCOSII
#ifdef OS_CRITICAL_METHOD //OS_CRITICAL_METHOD<4F><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><CBB5>Ҫ֧<D2AA><D6A7>UCOSII
#define delay_osrunning OSRunning //OS<4F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD>,0,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;1,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define delay_ostickspersec OS_TICKS_PER_SEC //OSʱ<53>ӽ<EFBFBD><D3BD><EFBFBD>,<2C><>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>
#define delay_osintnesting OSIntNesting //<2F>ж<EFBFBD>Ƕ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD>,<2C><><EFBFBD>ж<EFBFBD>Ƕ<EFBFBD>״<EFBFBD><D7B4><EFBFBD>
#endif
//֧<><D6A7>UCOSIII
#ifdef CPU_CFG_CRITICAL_METHOD //CPU_CFG_CRITICAL_METHOD<4F><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><CBB5>Ҫ֧<D2AA><D6A7>UCOSIII
#define delay_osrunning OSRunning //OS<4F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD>,0,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;1,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define delay_ostickspersec OSCfg_TickRate_Hz //OSʱ<53>ӽ<EFBFBD><D3BD><EFBFBD>,<2C><>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>
#define delay_osintnesting OSIntNestingCtr //<2F>ж<EFBFBD>Ƕ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD>,<2C><><EFBFBD>ж<EFBFBD>Ƕ<EFBFBD>״<EFBFBD><D7B4><EFBFBD>
#endif
//us<75><73><EFBFBD><EFBFBD>ʱʱ,<2C>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>us<75><73><EFBFBD>ӳ<EFBFBD>)
void delay_osschedlock(void)
{
#ifdef CPU_CFG_CRITICAL_METHOD //ʹ<><CAB9>UCOSIII
OS_ERR err;
OSSchedLock(&err); //UCOSIII<49>ķ<EFBFBD>ʽ,<2C><>ֹ<EFBFBD><D6B9><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>us<75><73>ʱ
#else //<2F><><EFBFBD><EFBFBD>UCOSII
OSSchedLock(); //UCOSII<49>ķ<EFBFBD>ʽ,<2C><>ֹ<EFBFBD><D6B9><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>us<75><73>ʱ
#endif
}
//us<75><73><EFBFBD><EFBFBD>ʱʱ,<2C>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void delay_osschedunlock(void)
{
#ifdef CPU_CFG_CRITICAL_METHOD //ʹ<><CAB9>UCOSIII
OS_ERR err;
OSSchedUnlock(&err); //UCOSIII<49>ķ<EFBFBD>ʽ,<2C>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>
#else //<2F><><EFBFBD><EFBFBD>UCOSII
OSSchedUnlock(); //UCOSII<49>ķ<EFBFBD>ʽ,<2C>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>
#endif
}
//<2F><><EFBFBD><EFBFBD>OS<4F>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
//ticks:<3A><>ʱ<EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD>
void delay_ostimedly(u32 ticks)
{
#ifdef CPU_CFG_CRITICAL_METHOD
OS_ERR err;
OSTimeDly(ticks,OS_OPT_TIME_PERIODIC,&err);//UCOSIII<49><49>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
#else
OSTimeDly(ticks); //UCOSII<49><49>ʱ
#endif
}
//systick<63>жϷ<D0B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ʹ<><CAB9>OSʱ<53>õ<EFBFBD>
void SysTick_Handler(void)
{
if(delay_osrunning==1) //OS<4F><53>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>,<2C><>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5>ȴ<EFBFBD><C8B4><EFBFBD>
{
OSIntEnter(); //<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
OSTimeTick(); //<2F><><EFBFBD><EFBFBD>ucos<6F><73>ʱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
OSIntExit(); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD>ж<EFBFBD>
}
}
#endif
//<2F><>ʼ<EFBFBD><CABC><EFBFBD>ӳٺ<D3B3><D9BA><EFBFBD>
//<2F><>ʹ<EFBFBD><CAB9>OS<4F><53>ʱ<EFBFBD><CAB1>,<2C>˺<EFBFBD><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>OS<4F><53>ʱ<EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD>
//SYSTICK<43><4B>ʱ<EFBFBD>ӹ̶<D3B9>ΪAHBʱ<42>ӵ<EFBFBD>1/8
//SYSCLK:ϵͳʱ<CDB3><CAB1>Ƶ<EFBFBD><C6B5>
void delay_init(u8 SYSCLK)
{
#if SYSTEM_SUPPORT_OS //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ֧<D2AA><D6A7>OS.
u32 reload;
#endif
SysTick->CTRL&=~(1<<2); //SYSTICKʹ<4B><CAB9><EFBFBD>ⲿʱ<E2B2BF><CAB1>Դ
fac_us=SYSCLK/8; //<2F><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>ʹ<EFBFBD><CAB9>OS,fac_us<75><73><EFBFBD><EFBFBD>Ҫʹ<D2AA><CAB9>
#if SYSTEM_SUPPORT_OS //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ֧<D2AA><D6A7>OS.
reload=SYSCLK/8; //ÿ<><C3BF><EFBFBD>ӵļ<D3B5><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>λΪM
reload*=1000000/delay_ostickspersec; //<2F><><EFBFBD><EFBFBD>delay_ostickspersec<65><EFBFBD><E8B6A8><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
//reloadΪ24λ<34>Ĵ<EFBFBD><C4B4><EFBFBD>,<2C><><EFBFBD><EFBFBD>ֵ:16777216,<2C><>180M<30><4D><><D4BC>0.699s<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
fac_ms=1000/delay_ostickspersec; //<2F><><EFBFBD><EFBFBD>OS<4F><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ٵ<EFBFBD>λ
SysTick->CTRL|=1<<1; //<2F><><EFBFBD><EFBFBD>SYSTICK<43>ж<EFBFBD>
SysTick->LOAD=reload; //ÿ1/delay_ostickspersec<65><63><EFBFBD>ж<EFBFBD>һ<EFBFBD><D2BB>
SysTick->CTRL|=1<<0; //<2F><><EFBFBD><EFBFBD>SYSTICK
#else
fac_ms=((u32)SYSCLK*1000)/8; //<2F><>OS<4F><53>,<2C><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>ms<6D><73>Ҫ<EFBFBD><D2AA>systickʱ<6B><CAB1><EFBFBD><EFBFBD>
#endif
}
#if SYSTEM_SUPPORT_OS //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ֧<D2AA><D6A7>OS.
//<2F><>ʱnus
//nus:Ҫ<><D2AA>ʱ<EFBFBD><CAB1>us<75><73>.
//nus:0~204522252(<28><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>2^32/fac_us@fac_us=21)
void delay_us(u32 nus)
{
u32 ticks;
u32 told,tnow,tcnt=0;
u32 reload=SysTick->LOAD; //LOAD<41><44>ֵ
ticks=nus*fac_us; //<2F><>Ҫ<EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD>
delay_osschedlock(); //<2F><>ֹOS<4F><53><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>us<75><73>ʱ
told=SysTick->VAL; //<2F>ս<EFBFBD><D5BD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ֵ
while(1)
{
tnow=SysTick->VAL;
if(tnow!=told)
{
if(tnow<told)tcnt+=told-tnow; //<2F><><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>һ<EFBFBD><D2BB>SYSTICK<43><4B>һ<EFBFBD><D2BB><EFBFBD>ݼ<EFBFBD><DDBC>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϳ<EFBFBD><CDBF><EFBFBD><EFBFBD><EFBFBD>.
else tcnt+=reload-tnow+told;
told=tnow;
if(tcnt>=ticks)break; //ʱ<><EFBFBD><E4B3AC>/<2F><><EFBFBD><EFBFBD>Ҫ<EFBFBD>ӳٵ<D3B3>ʱ<EFBFBD><CAB1>,<2C><><EFBFBD>˳<EFBFBD>.
}
};
delay_osschedunlock(); //<2F>ָ<EFBFBD>OS<4F><53><EFBFBD><EFBFBD>
}
//<2F><>ʱnms
//nms:Ҫ<><D2AA>ʱ<EFBFBD><CAB1>ms<6D><73>
//nms:0~65535
void delay_ms(u16 nms)
{
if(delay_osrunning&&delay_osintnesting==0)//<2F><><EFBFBD><EFBFBD>OS<4F>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>(<28>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><E6B2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
{
if(nms>=fac_ms) //<2F><>ʱ<EFBFBD><CAB1>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OS<4F><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
delay_ostimedly(nms/fac_ms); //OS<4F><53>ʱ
}
nms%=fac_ms; //OS<4F>Ѿ<EFBFBD><D1BE>޷<EFBFBD><DEB7><EFBFBD><E1B9A9>ôС<C3B4><D0A1><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>ʽ<EFBFBD><CABD>ʱ
}
delay_us((u32)(nms*1000)); //<2F><>ͨ<EFBFBD><CDA8>ʽ<EFBFBD><CABD>ʱ
}
#else //<2F><><EFBFBD><EFBFBD>ucosʱ
//<2F><>ʱnus
//nusΪҪ<CEAA><D2AA>ʱ<EFBFBD><CAB1>us<75><73>.
//ע<><D7A2>:nus<75><73>ֵ,<2C><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>798915us(<28><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>2^24/fac_us@fac_us=21)
void delay_us(u32 nus)
{
u32 temp;
SysTick->LOAD=nus*fac_us; //ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SysTick->VAL=0x00; //<2F><><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
SysTick->CTRL=0x01 ; //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(1<<16))); //<2F>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD><E4B5BD>
SysTick->CTRL=0x00; //<2F>رռ<D8B1><D5BC><EFBFBD><EFBFBD><EFBFBD>
SysTick->VAL =0X00; //<2F><><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
}
//<2F><>ʱnms
//ע<><D7A2>nms<6D>ķ<EFBFBD>Χ
//SysTick->LOADΪ24λ<34>Ĵ<EFBFBD><C4B4><EFBFBD>,<2C><><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱΪ:
//nms<=0xffffff*8*1000/SYSCLK
//SYSCLK<4C><4B>λΪHz,nms<6D><73>λΪms
//<2F><>180M<30><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,nms<=745ms
void delay_xms(u16 nms)
{
u32 temp;
SysTick->LOAD=(u32)nms*fac_ms; //ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(SysTick->LOADΪ24bit)
SysTick->VAL =0x00; //<2F><><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
SysTick->CTRL=0x01 ; //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(1<<16))); //<2F>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD><E4B5BD>
SysTick->CTRL=0x00; //<2F>رռ<D8B1><D5BC><EFBFBD><EFBFBD><EFBFBD>
SysTick->VAL =0X00; //<2F><><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
}
//<2F><>ʱnms
//nms:0~65535
void delay_ms(u16 nms)
{
u8 repeat=nms/540; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>540,<2C>ǿ<EFBFBD><C7BF>ǵ<EFBFBD>ijЩ<C4B3>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD>ܳ<EFBFBD>Ƶʹ<C6B5><CAB9>,
//<2F><><EFBFBD>糬Ƶ<E7B3AC><C6B5>248M<38><4D>ʱ<EFBFBD><CAB1>,delay_xms<6D><73><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>ʱ541ms<6D><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
u16 remain=nms%540;
while(repeat)
{
delay_xms(540);
repeat--;
}
if(remain)delay_xms(remain);
}
#endif