typedef enum{
RESET = 0,
SET
}TimerTaskFlagStatus_t;
uint32 Timer_Counter[Timer_Task_Num]; //定义定时器计数值
TimerTaskFlagStatus_t Timer_MASK[Timer_Task_Num];
/****************************************************************************
* NAME:void SysTick_Configuration(void)
* Function:
* note: 内容参考 Cortex -M4 programming manual
****************************************************************************/
void SysTick_Configuration(void)
{
SysTick->LOAD = 21000; // 1ms
SysTick->VAL = 0; /* Load the SysTick Counter Value */
SysTick->CTRL = SysTick_CTRL_TICKINT_Msk|SysTick_CTRL_ENABLE_Msk;
}
/****************************************************************************
* NAME:void Timer_ParameterSet(uint8 i,uint16 cnt)
* Function: 1ms时基定时器
* Input1 : T0类型定时器标号
* Input2 : 定时时间
* note:
****************************************************************************/
void Timer_ParameterSet(uint8 i,uint16 cnt)
{
if(i>Timer_Task_Num)//定时器范围溢出
return;
if(cnt==0)
return;
if((Timer_Counter[i]==0)&&(Timer_MASK[i] == RESET))
Timer_Counter[i] = cnt;
}
/****************************************************************************
* NAME:void T0MaskCheck(void)
* Function: 检查T0定时器标志位
* Input1 : T0类型定时器标号
* Input2 : 定时时间
* note:
****************************************************************************/
void TimerMaskCheck(void)
{
for(int i = 0;i < Timer_Task_Num;i++)
{
if(Timer_MASK[i] == SET)
switch(i)
{
case 0 :
Timer_MASK[0] = RESET;
Timer_ParameterSet(0,100);
//dosomething
break;
case 1 :
Timer_MASK[1] = RESET;
//dosomething
break;
case 2 :
Timer_MASK[2] = RESET;
//dosomething
break;
case 3 :
Timer_MASK[3] = RESET;
//dosomething
break;
default :
break;
}
}
}
void SysTick_Handler(void)
{
for(int i=0;i < Timer_Task_Num;i++)
{
if(Timer_Counter[i]!=0)
{
Timer_Counter[i]--;
if(Timer_Counter[i]==0)
{
Timer_MASK[i] = SET;
}
}
}
}
int main()
{
T0_ParameterSet(0,5);
T0_ParameterSet(1,10);
T0_ParameterSet(2,100);
T0_ParameterSet(3,5000);
while(1)
TimerMaskCheck();
}
MCU裸机软定时器例子
于 2020-09-02 17:34:18 首次发布