1.5wuling_zhuanjietou/SYSTEM/delay/delay.c

183 lines
4.7 KiB
C
Raw Normal View History

2024-10-17 01:06:51 +00:00
#include "delay.h"
#include "sys.h"
//////////////////////////////////////////////////////////////////////////////////
//<2F><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>ucos,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>.
#if SYSTEM_SUPPORT_UCOS
#include "includes.h" //ucos ʹ<><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>
//<2F><><EFBFBD><EFBFBD>delay_us,delay_ms
//<2F><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>@ALIENTEK
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳:www.openedv.com
//<2F>޸<EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD>:2012/9/2
//<2F><EFBFBD><E6B1BE>V1.5
//<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>˾ 2009-2019
//All rights reserved
//********************************************************************************
//V1.2<EFBFBD>޸<EFBFBD>˵<EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>е<EFBFBD><D0B5>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
//<2F><>ֹ<EFBFBD><D6B9>ʱ<EFBFBD><CAB1>׼ȷ,<2C><><EFBFBD><EFBFBD>do while<6C>ṹ!
//V1.3<EFBFBD>޸<EFBFBD>˵<EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD>UCOSII<49><49>ʱ<EFBFBD><CAB1>֧<EFBFBD><D6A7>.
//<2F><><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>ucosII,delay_init<69><74><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>SYSTICK<43><4B>ֵ,ʹ֮<CAB9><D6AE>ucos<6F><73>TICKS_PER_SEC<45><43>Ӧ.
//delay_ms<6D><73>delay_usҲ<73><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ucos<6F>ĸ<EFBFBD><C4B8><EFBFBD>.
//delay_us<75><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ucos<6F><73>ʹ<EFBFBD><CAB9>,<2C><><EFBFBD><EFBFBD>׼ȷ<D7BC>Ⱥܸ<C8BA>,<2C><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>û<EFBFBD><C3BB>ռ<EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD>Ķ<EFBFBD>ʱ<EFBFBD><CAB1>.
//delay_ms<6D><73>ucos<6F><73>,<2C><><EFBFBD>Ե<EFBFBD><D4B5><EFBFBD>OSTimeDly<6C><79><EFBFBD><EFBFBD>,<2C><>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>ucosʱ,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>delay_usʵ<73><CAB5>,<2C>Ӷ<EFBFBD>׼ȷ<D7BC><C8B7>ʱ
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ucos֮<73><D6AE>delay_ms<6D><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ij<EFBFBD><C4B3><EFBFBD>,ѡ<><D1A1>OSTimeDlyʵ<79>ֻ<EFBFBD><D6BB><EFBFBD>delay_usʵ<73><CAB5>.
//V1.4<EFBFBD>޸<EFBFBD>˵<EFBFBD><EFBFBD> 20110929
//<2F>޸<EFBFBD><DEB8><EFBFBD>ʹ<EFBFBD><CAB9>ucos,<2C><><EFBFBD><EFBFBD>ucosδ<73><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>,delay_ms<6D><73><EFBFBD>ж<EFBFBD><D0B6>޷<EFBFBD><DEB7><EFBFBD>Ӧ<EFBFBD><D3A6>bug.
//V1.5<EFBFBD>޸<EFBFBD>˵<EFBFBD><EFBFBD> 20120902
//<2F><>delay_us<75><73><EFBFBD><EFBFBD>ucos<6F><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>ucos<6F><73><EFBFBD><EFBFBD>delay_us<75><73>ִ<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>ܵ<EFBFBD><DCB5>µ<EFBFBD><C2B5><EFBFBD>ʱ<EFBFBD><CAB1>׼<EFBFBD><D7BC>
//////////////////////////////////////////////////////////////////////////////////
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>
#ifdef OS_CRITICAL_METHOD //<2F><><EFBFBD><EFBFBD>OS_CRITICAL_METHOD<4F><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><CBB5>ʹ<EFBFBD><CAB9>ucosII<49><49>.
//systick<63>жϷ<D0B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ʹ<><CAB9>ucosʱ<73>õ<EFBFBD>
void SysTick_Handler(void)
{
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>ucos<6F><73>ʱ<EFBFBD><CAB1>,<2C>˺<EFBFBD><CBBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ucos<6F><73>ʱ<EFBFBD>ӽ<EFBFBD><D3BD><EFBFBD>
//SYSTICK<43><4B>ʱ<EFBFBD>ӹ̶<D3B9>ΪHCLKʱ<4B>ӵ<EFBFBD>1/8
//SYSCLK:ϵͳʱ<CDB3><CAB1>
void delay_init()
{
#ifdef OS_CRITICAL_METHOD //<2F><><EFBFBD><EFBFBD>OS_CRITICAL_METHOD<4F><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><CBB5>ʹ<EFBFBD><CAB9>ucosII<49><49>.
u32 reload;
#endif
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //ѡ<><D1A1><EFBFBD>ⲿʱ<E2B2BF><CAB1> HCLK/8
fac_us=SystemCoreClock/8000000; //Ϊϵͳʱ<CDB3>ӵ<EFBFBD>1/8
#ifdef OS_CRITICAL_METHOD //<2F><><EFBFBD><EFBFBD>OS_CRITICAL_METHOD<4F><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><CBB5>ʹ<EFBFBD><CAB9>ucosII<49><49>.
reload=SystemCoreClock/8000000; //ÿ<><C3BF><EFBFBD>ӵļ<D3B5><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>λΪK
reload*=1000000/OS_TICKS_PER_SEC;//<2F><><EFBFBD><EFBFBD>OS_TICKS_PER_SEC<45><EFBFBD><E8B6A8><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
//reloadΪ24λ<34>Ĵ<EFBFBD><C4B4><EFBFBD>,<2C><><EFBFBD><EFBFBD>ֵ:16777216,<2C><>72M<32><4D><><D4BC>1.86s<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
fac_ms=1000/OS_TICKS_PER_SEC;//<2F><><EFBFBD><EFBFBD>ucos<6F><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ٵ<EFBFBD>λ
SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk; //<2F><><EFBFBD><EFBFBD>SYSTICK<43>ж<EFBFBD>
SysTick->LOAD=reload; //ÿ1/OS_TICKS_PER_SEC<45><43><EFBFBD>ж<EFBFBD>һ<EFBFBD><D2BB>
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk; //<2F><><EFBFBD><EFBFBD>SYSTICK
#else
fac_ms=(u16)fac_us*1000;//<2F><>ucos<6F><73>,<2C><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>ms<6D><73>Ҫ<EFBFBD><D2AA>systickʱ<6B><CAB1><EFBFBD><EFBFBD>
#endif
}
#ifdef OS_CRITICAL_METHOD //ʹ<><CAB9><EFBFBD><EFBFBD>ucos
//<2F><>ʱnus
//nusΪҪ<CEAA><D2AA>ʱ<EFBFBD><CAB1>us<75><73>.
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>
tcnt=0;
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>.
}
};
}
//<2F><>ʱnms
//nms:Ҫ<><D2AA>ʱ<EFBFBD><CAB1>ms<6D><73>
void delay_ms(u16 nms)
{
if(OSRunning==TRUE)//<2F><><EFBFBD><EFBFBD>os<6F>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
if(nms>=fac_ms)//<2F><>ʱ<EFBFBD><CAB1>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ucos<6F><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
OSTimeDly(nms/fac_ms);//ucos<6F><73>ʱ
}
nms%=fac_ms; //ucos<6F>Ѿ<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>ʱ,<2C><>ʱucos<6F>޷<EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
}
#else//<2F><><EFBFBD><EFBFBD>ucosʱ
//<2F><>ʱnus
//nusΪҪ<CEAA><D2AA>ʱ<EFBFBD><CAB1>us<75><73>.
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|=SysTick_CTRL_ENABLE_Msk ; //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
do
{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16)));//<2F>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD><E4B5BD>
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //<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><>72M<32><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,nms<=1864
void delay_ms(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|=SysTick_CTRL_ENABLE_Msk ; //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
do
{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16)));//<2F>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD><E4B5BD>
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //<2F>رռ<D8B1><D5BC><EFBFBD><EFBFBD><EFBFBD>
SysTick->VAL =0X00; //<2F><><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
}
#endif