STM32用SysTick來做定時(shí)器
1 硬件電路配置
這里還是借用前面LED電路我就不貼圖片。
2 時(shí)鐘說明
SysTick和HCK的時(shí)鐘頻率是一樣的庫函數(shù)代碼如下
/**
*@briefInitializeandstarttheSysTickcounteranditsinterrupt.
*
*@paramticksnumberofticksbetweentwointerrupts
*@return1=failed,0=successful
*
*Initialisethesystemticktimeranditsinterruptandstartthe
*systemticktimer/counterinfreerunningmodetogenerate
*periodicalinterrupts.
*/
static__INLINEuint32_tSysTick_Config(uint32_tticks)
{
if(ticks>SysTick_LOAD_RELOAD_Msk)return(1);/*Reloadvalueimpossible*/
SysTick->LOAD=(ticks&SysTick_LOAD_RELOAD_Msk)-1;/*setreloadregister*/
NVIC_SetPriority(SysTick_IRQn,(1<<__NVIC_PRIO_BITS)-1);/*setPriorityforCortex-M0SystemInterrupts*/
SysTick->VAL=0;/*LoadtheSysTickCounterValue*/
SysTick->CTRL=SysTick_CTRL_CLKSOURCE_Msk|
SysTick_CTRL_TICKINT_Msk|
SysTick_CTRL_ENABLE_Msk;/*EnableSysTickIRQandSysTickTimer*/
return(0);/*Functionsuccessful*/
}
由庫函數(shù)可知道沒有進(jìn)行分頻動(dòng)作所以時(shí)鐘和主時(shí)鐘一樣
配置時(shí)鐘中斷的時(shí)間間隔
{
/*SystemFrequency/10001ms中斷一次
*SystemFrequency/10000010us中斷一次
*SystemFrequency/10000001us中斷一次
*/
//if(SysTick_Config(SystemFrequency/100000))//ST3.0.0庫版本
if(SysTick_Config(SystemCoreClock/1000))//ST3.5.0庫版本
{
/*Captureerror*/
while(1);
}
//我需要不斷的開啟中斷所以這句話屏蔽起來
//SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;
}
用中斷模式所以需要在中斷文件中做定時(shí)標(biāo)志。一下這個(gè)函數(shù)在中斷文件中
*@briefThisfunctionhandlesSysTickHandler.
*@paramNone
*@retval:None
*/
voidSysTick_Handler(void)
{
TimingDelay_Decrement();
}
中斷函數(shù)在調(diào)用一個(gè)計(jì)數(shù)函數(shù),計(jì)數(shù)函數(shù)應(yīng)該放在外面一個(gè)公共文件中方便各個(gè)文件中數(shù)據(jù)傳遞。
/*
*函數(shù)名:TimingDelay_Decrement
*描述獲取節(jié)拍程序
*輸入無
*輸出無
*調(diào)用在SysTick中斷函數(shù)SysTick_Handler()調(diào)用
*/
voidTimingDelay_Decrement(void)
{
if(TimingDelay!=0x00)
{
TimingDelay--;
}
}