液晶初始化 delay_ms(50); // delay 50 ms 进入
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault excep
tion occurs */
while (1)
{
}
}
一下是delay函数
////////////////////////////////////////////////////////////
#define SUPPORT_OS 1
#if SUPPORT_OS
#include "FreeRTOS.h"
#include "task.h"
#endif
//////////////////////////////////////////////////////
oid delay_init(u8 SYSCLK)
{
#if SUPPORT_OS
u32 reload;
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
fac_us=SYSCLK;
reload=SYSCLK;
reload*=1000000/configTICK_RATE_HZ;
fac_ms=1000/configTICK_RATE_HZ;
SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk;
SysTick->LOAD=reload;
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;
#else
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
fac_us=SYSCLK/8;
fac_ms=(u16)fac_us*1000; //ÿ¸ömsDèòaμÄsystickê±Öóêy
#endif
}
void delay_us(u32 nus)
{
#if SUPPORT_OS
u32 ticks;
u32 told,tnow,tcnt=0;
u32 reload=SysTick->LOAD; //LOADμÄÖμ
ticks=nus*fac_us; //DèòaμĽúÅÄêy
told=SysTick->VAL; //¸Õ½øèëê±μļÆêyÆ÷Öμ
while(1)
{
tnow=SysTick->VAL;
if(tnow!=told)
{
if(tnow
else tcnt+=reload-tnow+told;
told=tnow;
if(tcnt>=ticks)break; //ê±¼ä3¬1y/μèóúòaÑó3ùμÄê±¼ä,ÔòíË3ö.
}
};
#else
u32 midtime;
SysTick->LOAD=nus*fac_us; //ê±¼ä¼óÔØ
SysTick->VAL=0x00; //Çå¿Õ¼ÆêyÆ÷
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //¿aê¼μ1êy
do
{
midtime=SysTick->CTRL;
}
while((midtime&0x01)&&!(midtime&(1<<16)));//μè′yê±¼äμ½′ï
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //1رռÆêyÆ÷
SysTick->VAL =0X00; //Çå¿Õ¼ÆêyÆ÷
#endif
}
/****************************************************************************
* Ãû 3Æ: void delay_xms(u16 nms)
* 1| Äü£oÑóê±nms
* èë¿ú2Îêy£oòaÑóê±μÄoáÃîêy
* ·μ»Ø2Îêy£oÎT
* Ëμ Ã÷£oSysTick->LOADÎa24λ¼Ä′æÆ÷,ËùòÔ,×î′óÑóê±Îa: nms<=0xffffff*8*1000/SYSCLK
¶Ô168Mìõ¼tÏÂ,nms<=798ms
****************************************************************************/
void delay_xms(u16 nms)
{
#if SUPPORT_OS
u32 i;
for(i=0;i
#else
u32 midtime;
SysTick->LOAD=(u32)nms*fac_ms;//ê±¼ä¼óÔØ(SysTick->LOADÎa24bit)
SysTick->VAL =0x00; //Çå¿Õ¼ÆêyÆ÷
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //¿aê¼μ1êy
do
{
midtime=SysTick->CTRL;
}
while((midtime&0x01)&&!(midtime&(1<<16)));//μè′yê±¼äμ½′ï
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //1رռÆêyÆ÷
SysTick->VAL =0X00; //Çå¿Õ¼ÆêyÆ÷
#endif
}
/****************************************************************************
* Ãû 3Æ: void delay_ms(u16 nms)
* 1| Äü£oÑóê±nms
* èë¿ú2Îêy£oòaÑóê±μÄoáÃîêy
* ·μ»Ø2Îêy£oÎT
* Ëμ Ã÷£onms:0~65535
****************************************************************************/
void delay_ms(u16 nms)
{
#if SUPPORT_OS
if(xTaskGetSchedulerState()!=taskSCHEDULER_NOT_STARTED)//Ïμí3òѾ-ÔËDD
{
if(nms>=fac_ms) //Ñóê±μÄê±¼ä′óóúOSμÄ×îéùê±¼äÖüÆú
{
vTaskDelay(nms/fac_ms); //FreeRTOSÑóê±
}
nms%=fac_ms; //OSòѾ-ÎT·¨ìá1©ÕaÃ′D¡μÄÑóê±áË,2éóÃÆÕí¨·½ê½Ñóê±
}
delay_us((u32)(nms*1000)); //ÆÕí¨·½ê½Ñóê±
#else
u8 repeat=nms/540; //ÕaàïóÃ540,êÇ¿¼ÂÇμ½Ä3D©¿í»§¿éÄü3¬Æμê1óÃ,
//±èèç3¬Æμμ½248MμÄê±oò,delay_xms×î′óÖ»ÄüÑóê±541ms×óóòáË
u16 remain=nms%540;
while(repeat)
{
delay_xms(540);
repeat--;
}
if(remain)delay_xms(remain);
#endif
}
0