⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer.c

📁 realtek LCD monitor, TV开发源代码
💻 C
字号:
//**********************************************************************************************************
//  The  Software  is  proprietary,  confidential,  and  valuable to Realtek Semiconductor
//  Corporation  ("Realtek").  All  rights, including but not limited  to  copyrights,
//  patents,  trademarks, trade secrets, mask work rights, and other similar rights and interests,
//  are reserved to Realtek. Without  prior  written  consent  from  Realtek,  copying, reproduction,
//  modification,  distribution,  or  otherwise  is strictly prohibited. The Software  shall  be
//  kept  strictly  in  confidence,  and  shall  not be  disclosed to or otherwise accessed by
//  any third party. @ <2003> - <2008>   The Software is provided "AS IS" without any warranty of any kind,
//  express, implied, statutory or otherwise.
//**********************************************************************************************************
//----------------------------------------------------------------------------------------------------
// ID Code      : Timer.c No.0000
// Update Note  :
//----------------------------------------------------------------------------------------------------

#define __TIMER__

#include "Common\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;
            }
        }
    }
}
#if(_NEW_ADC == _TRUE)
// V010 Patch Note (1) : Adjust ADC Clock Modify
//----------------------------------------------------------------------------------------------------
// Description  : Polling Flag Process
// Input Value  : ucTimeout --> Timeout number (Max: 255 ms)
//                ucPage    --> Current Page (Common Area use _PAGE_COMMON)
//                ucRegister--> Current Register
//                ucBit     --> Polling Bit
//                bSuccess  --> 1 or 0 when finished for Polling Bit
// Output Value : Return _TRUE while polling success, _FALSE for timeout
//----------------------------------------------------------------------------------------------------
bit CTimerPollingFlagProc(BYTE ucTimeout, BYTE ucPage, BYTE ucRegister, BYTE ucBit, bit bSuccess)
{
    if(ucPage != _PAGE_COMMON)
    {
        CScalerPageSelect(ucPage);
    }

    do
    {
        CTimerDelayXms(1);

        if(!((bit)CScalerGetBit(ucRegister, ucBit) ^ bSuccess))
        {
            return _TRUE;
        }
    }
    while(--ucTimeout);

    return _FALSE;
}
#endif
//--------------------------------------------------
// 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;
}

//==================================================================================//
void CTimerEventTimerCount(void)
{
   DWORD Hour;
   BYTE  Min;
   BYTE  Duty;
//============= Following are back light timer and total timer  ==================//
#define _DUTY_TIMER_SAVE_FREQUENCY_FACTORY   10
#define _DUTY_TIMER_SAVE_FREQUENCY_USER      20

   if(!GET_FACTORY_MODE())
      Duty=_DUTY_TIMER_SAVE_FREQUENCY_USER;
   else
      Duty=_DUTY_TIMER_SAVE_FREQUENCY_FACTORY;
   if((++ucDutyTimerTemp)>=Duty)
   {
      ucDutyTimerTemp = 0;
      CEepromLoadDutyTimerData();
      Hour=((DWORD)pData[3]<<9);
      Hour= Hour | ((DWORD)pData[4] << 1);
      Hour= Hour | ((DWORD)pData[5] >> 7);
      Min=  pData[5] & 0x3F;
      if(GET_PANELPOWERSTATUS()==_ON)
      {
         if((Hour<99999))//Max are 99999 hours and 59 min
         {
            Min=Min+Duty;
            if(Min>=60)
            {
               Min=0;
               Hour++;
            }
            pData[3]=(BYTE)(Hour>>9);
            pData[4]=(BYTE)(Hour>>1);
            pData[5]=(BYTE)(Hour<<7) | Min;
         }
      }

      Hour=((DWORD)pData[0]<<9);
      Hour= Hour | ((DWORD)pData[1] << 1);
      Hour= Hour | ((DWORD)pData[2] >> 7);
      Min=  pData[2] & 0x3F;
      if((Hour<99999))   //Max are 99999 hours and 59 min
      {
           Min=Min+Duty;
           if(Min>=60)
           {
              Min=0;
              Hour++;
           }
        pData[0]=(BYTE)(Hour>>9);
        pData[1]=(BYTE)(Hour>>1);
        pData[2]=(BYTE)(Hour<<7) | Min;
        CEepromSaveDutyTimerData();
      }
   }
   CTimerEnaleTimerCount();
#undef _DUTY_TIMER_SAVE_FREQUENCY_FACTORY
#undef _DUTY_TIMER_SAVE_FREQUENCY_USER
}
void CTimerEnaleTimerCount(void)
{
   CTimerActiveTimerEvent(SEC(60),CTimerEventTimerCount);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -