📄 timer.c
字号:
//----------------------------------------------------------------------------------------------------
// ID Code : Timer.c No.0000
// Update Note :
//
//----------------------------------------------------------------------------------------------------
#define __TIMER__
#include "..\Header\Include.h"
//--------------------------------------------------
// Description : Check if it's time to execute events
// Input Value : None
// Output Value : None
//--------------------------------------------------
void CTimerHandler(void)
{
BYTE timereventcnt;
for(timereventcnt=0;timereventcnt<_MAX_EVENT_AMOUNT;timereventcnt++)
{
if(TimerEvent[timereventcnt].Time == 0)
{
bTimer0Ctrl = _TRUE;
TimerEvent[timereventcnt].Time = _INACTIVE_TIMER_EVENT;
bTimer0Ctrl = _FALSE;
(*TimerEvent[timereventcnt].Event)();
}
}
}
//--------------------------------------------------
// Description : Set up a timer for an event. If the event is exist,
// this function is ignored and do not change the executing time.
// Input Value : usTime --> Unit in 10ms, range in 0.01 ~ 655.3 sec
// Event --> Execute Event while timeup
// Output Value : None
//--------------------------------------------------
void CTimerActiveTimerEvent(WORD usTime, void (*Event)())
{
BYTE timereventcnt;
for(timereventcnt=0;timereventcnt<_MAX_EVENT_AMOUNT;timereventcnt++)
{
if((TimerEvent[timereventcnt].Time != _INACTIVE_TIMER_EVENT) && (TimerEvent[timereventcnt].Event == Event))
return;
}
for(timereventcnt=0;timereventcnt<_MAX_EVENT_AMOUNT;timereventcnt++)
{
if(TimerEvent[timereventcnt].Time == _INACTIVE_TIMER_EVENT)
{
bTimer0Ctrl = _TRUE;
TimerEvent[timereventcnt].Time = usTime;
bTimer0Ctrl = _FALSE;
TimerEvent[timereventcnt].Event = Event;
break;
}
}
}
//--------------------------------------------------
// Description : Reactive a timer for an event. If the event is exist,
// this function will reset the executing time and restart.
// Input Value : usTime --> Unit in 10ms, range in 0.01 ~ 655.3 sec
// Event --> Execute Event while timeup
// Output Value : None
//--------------------------------------------------
void CTimerReactiveTimerEvent(WORD usTime, void (*Event)())
{
BYTE timereventcnt;
for(timereventcnt=0;timereventcnt<_MAX_EVENT_AMOUNT;timereventcnt++)
{
if((TimerEvent[timereventcnt].Time != _INACTIVE_TIMER_EVENT) && (TimerEvent[timereventcnt].Event == Event))
{
bTimer0Ctrl = _TRUE;
TimerEvent[timereventcnt].Time = usTime;
bTimer0Ctrl = _FALSE;
return;
}
}
CTimerActiveTimerEvent(usTime, Event);
}
//--------------------------------------------------
// Description : Cancel an event
// Input Value : Event --> Event which we want to cancel
// Output Value : None
//--------------------------------------------------
void CTimerCancelTimerEvent(void (*Event)())
{
BYTE timereventcnt;
for(timereventcnt=0;timereventcnt<_MAX_EVENT_AMOUNT;timereventcnt++)
{
if(TimerEvent[timereventcnt].Event == Event)
{
bTimer0Ctrl = _TRUE;
TimerEvent[timereventcnt].Time = _INACTIVE_TIMER_EVENT;
bTimer0Ctrl = _FALSE;
}
}
}
//--------------------------------------------------
// Description : Initial timer and events. We have to run this function at firmware startup
// Input Value : None
// Output Value : None
//--------------------------------------------------
void CTimerInitialTimerEvent(void)
{
BYTE timereventcnt;
for(timereventcnt=0;timereventcnt<_MAX_EVENT_AMOUNT;timereventcnt++)
{
TimerEvent[timereventcnt].Time = _INACTIVE_TIMER_EVENT;
}
TR0 = 1;
}
//--------------------------------------------------
// Description : Decrease timer counts while 10ms Interrupt is up
// Input Value : None
// Output Value : None
//--------------------------------------------------
void CTimerDecreaseTimerCnt(void)
{
BYTE timerdeccnt;
for(timerdeccnt=0;timerdeccnt<_MAX_EVENT_AMOUNT;timerdeccnt++)
{
if((TimerEvent[timerdeccnt].Time != _INACTIVE_TIMER_EVENT) && (TimerEvent[timerdeccnt].Time != 0))
{
TimerEvent[timerdeccnt].Time--;
}
}
}
//----------------------------------------------------------------------------------------------------
// Description : Count down event process
// Input Value : pEventCnt --> Event counter
// ucWaitCnt --> Count down number (Max: 254)
// Event --> Execute Event while counter is zero
// Output Value : None
//----------------------------------------------------------------------------------------------------
void CTimerCountDownEventProc(BYTE *pEventCnt, BYTE ucWaitCnt, void (*Event)(void))
{
if(*pEventCnt == _INACTIVE_COUNTDOWN_EVENT)
{
*pEventCnt = ucWaitCnt;
}
else if(*pEventCnt == 0)
{
*pEventCnt = _INACTIVE_COUNTDOWN_EVENT;
Event();
}
else
{
*pEventCnt = *pEventCnt - 1;
}
}
//----------------------------------------------------------------------------------------------------
// Description : Polling Event Process
// Input Value : ucTimeout --> Timeout number (Max: 255 ms)
// Event --> Polling event. This event has to return _TRUE or _FALSE
// Output Value : Return _TRUE while polling success, _FALSE for timeout
//----------------------------------------------------------------------------------------------------
bit CTimerPollingEventProc(BYTE ucTimeout, bit (*Event)(void))
{
do
{
CTimerDelayXms(1);
if(Event())
{
return _TRUE;
}
}
while(--ucTimeout);
return _FALSE;
}
//--------------------------------------------------
// Description : Hold program for 0 ~ 65535 ms
// Input Value : usNum --> Delay time
// Output Value : None
//--------------------------------------------------
void CTimerDelayXms(WORD usNum)
{
if(usNum)
{
bNotifyTimer0Int = _FALSE;
while(_TRUE)
{
if(bNotifyTimer0Int)
{
bNotifyTimer0Int = _FALSE;
if(--usNum)
TR0 = _ON;
else
return;
}
}
}
}
//--------------------------------------------------
// Description : Hold until the specified event occurs
// Input Value : ucEvent --> Specified event
// Output Value : Return _TRUE while timeout
//--------------------------------------------------
bit CTimerWaitForEvent(BYTE ucEvent)
{
BYTE temp;
BYTE timeoutcnt = 30; // 30ms timeout
CScalerSetByte(_STATUS1_03, 0x00); // Clear status (status register will be cleared after write)
bNotifyTimer0Int = _FALSE;
do
{
if(bNotifyTimer0Int)
{
bNotifyTimer0Int = _FALSE;
if(--timeoutcnt)
TR0 = _ON;
}
CScalerRead(_STATUS1_03, 1, &temp, _NON_AUTOINC);
temp &= ucEvent;
}
while((temp == 0) && (timeoutcnt != 0));
return timeoutcnt ? _FALSE : _TRUE;
}
//---------------------------------------------------
// Description : Initial according variable for User Timer. Add by zhang_dc
// Input Value: None
// Output Value: None
//---------------------------------------------------
void CInitialUserTimer(void) //add by zhang_dc
{
BYTE i;
for(i=0; i<4; i++)
{
stUserTimerData.Hour[i] = 0;
stUserTimerData.Minute[i] = 0;
}
ucUserTimerCount = 0;
ucUserTimerCount2 = 0;
//ucUserTimerValid = 0;
}
//------------------------------------------------
// Description : Check valid number of User Timer. Add by zhang_dc
// Input Value: None
// Output Value: Valid number
//------------------------------------------------
void CCheckUserTimerValid(void)
{
BYTE i;
BYTE valid = _FAIL;
if(ucOsd_Item_Index1 > 0) //if still in adjust, do not enable User Timer setting.
return;
for(i=0; i<3; i++)
{
if((stUserTimerData.Hour[i]>0) || (stUserTimerData.Minute[i]>0))
{
valid++;
}
}
if(valid == _FAIL) //No timer enabled, disable user timer counting.
{
bUserTimerEn = _FALSE;
//stUserTimerData.Minute[3] = 0; //Notice: must clear time base variable.!!
//stUserTimerData.Hour[3] = 0;
}
else
{
if(!bUserTimerEn)
{
bUserTimerEn = _TRUE;
bUserTimerOut = _FALSE;
//stUserTimerData.Minute[3] = 0;
//stUserTimerData.Hour[3] = 0;
//ucUserTimerCount = 0;
//ucUserTimerCount2 = 0;
}
}
}
//------------------------------------------------
// Description : Check valid Timer timeout or not. Add by zhang_dc
// Input Value: None
// Output Value: Timeout case
//------------------------------------------------
BYTE CCheckUserTimerTimeout(void)
{
BYTE i;
for(i=0; i<3; i++)
{
if((stUserTimerData.Minute[i]==0)&&(stUserTimerData.Hour[i]==0))
{
continue;
}
if(stUserTimerData.Minute[3] == stUserTimerData.Minute[i])
{
if(stUserTimerData.Hour[3] == stUserTimerData.Hour[i])
{
//Time Out matched!!
stUserTimerData.Hour[i] = 0;
stUserTimerData.Minute[i] = 0;
return i+1; //Notice: I only enable ONE case at the same time! Other cases will be ignoaled.
}
}
}
return _FALSE; //No match
}
//---------------------------------------------------
// Description : User timer timeout process. Add by zhang_dc
// Input Value : None
// Output Value : None
//---------------------------------------------------
void CUserTimerTimeoutCaseProc(BYTE ucCase)
{
switch(ucCase)
{
case _TIMER_POWERUP:
if(!GET_POWERSTATUS()) //if current power status is power off
{
SET_POWERSWITCH(); //do power on
}
break;
case _TIMER_POWERDOWN:
if(GET_POWERSTATUS()) //if current power status is power on
{
SET_POWERSWITCH(); //do power off.
}
break;
case _TIMER_REMIND:
if(ucCurrState == _ACTIVE_STATE)
{
ucOsdEventMsg = _SHOW_TIMER_REMIND;
}
break;
default:
break;
}
}
//---------------------------------------------------
// Description : User timer handler. Add by zhang_dc
// Input Value : None
// Output Value : None
//---------------------------------------------------
void CUserTimerHandler(void)
{
BYTE i = 0;
//if(ucCurrState != _ACTIVE_STATE)
//return;
CCheckUserTimerValid();
if(!bUserTimerEn) //if do not enable user timer, do nothing.
return;
//Counting
if(bUserTimerOut)
{
bUserTimerOut = _FALSE;
ucUserTimerCount++;
if(ucUserTimerCount == 10)
{
ucUserTimerCount = 0;
ucUserTimerCount2++;
if(ucUserTimerCount2 == 60)
{
ucUserTimerCount2 = 0;
stUserTimerData.Minute[3]++;
if(stUserTimerData.Minute[3] == 60)
{
if(stUserTimerData.Hour[3]++ < _USER_TIMER_MAX)
{
stUserTimerData.Minute[3] = 0;
stUserTimerData.Hour[3]++;
}
else
{
//stUserTimerData.Minute[3] = 59;
CInitialUserTimer();
}
}
}
}
}
CUserTimerTimeoutCaseProc(CCheckUserTimerTimeout());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -