Timer.c
上传用户:xmyjxjd
上传日期:2013-05-04
资源大小:1517k
文件大小:6k
开发平台:

C/C++

  1. #define __TIMER__
  2. #include "reg52.h"
  3. #include "HeaderLcd_main.h"
  4. #include "HeaderLcd_auto.h"
  5. /////////////////////////////////////////////////////////////
  6. // Interrupt Service Routine for Timer0 (Fired every 20ms) //
  7. /////////////////////////////////////////////////////////////
  8. void IntProc_Timer0(void) interrupt 1
  9. {
  10. #if (IRPOLLING)
  11.     static unsigned char    ucIR_Seq    = 0xff;
  12.     static unsigned char    ucTimerCnt  = 0x00;
  13.     TL0         = TIME0_COUNTER_LBYTE;  // Reload Timer0 low-byte
  14.     TH0         = TIME0_COUNTER_HBYTE;  // Reload Timer0 high-byte
  15.     if (ucIR_State)
  16.     {
  17.         if (bIR_GPIO)
  18.         {
  19.             ucIR_Seq    = (ucIR_Seq << 1) | 0x01;
  20.             if ((0xff == ucIR_Seq) && (ucIR_State & 0xfe))
  21.             {
  22.                 ucIR_State  = 0;    // Command timeout; Back to idle state
  23.                 
  24.                 ((unsigned int *)ucIR_Cmd)[0]   = 0;
  25.             }
  26.         }
  27.         else
  28.         {
  29.             if (1 < ucIR_State)
  30.             {
  31.                 if (ucIR_Seq & 0x01)
  32.                 {
  33.                     ((unsigned int *)ucIR_Cmd)[0]   = (((unsigned int *)ucIR_Cmd)[0] << 1) 
  34.                                                     | (0x07 != (ucIR_Seq & 0x07) ? 1 : 0);
  35.                     if (23 == ++ucIR_State && CUSTOM_CODE != ((unsigned int *)ucIR_Cmd)[0])
  36.                     {
  37.                         ucIR_State  = 0;    // Customer code error; Back to idle state
  38.                         ((unsigned int *)ucIR_Cmd)[0]   = 0;
  39.                     }
  40.                     else if (39 == ucIR_State)
  41.                     {
  42.                         ucIR_State  = 0;    // Command complete; Back to idle state
  43.                         ucIR_Cmd[1] = 8;    // Set command timer to 8 (7*20 = 140ms)
  44.                     }
  45.                 }
  46.                 else if (0x00 == (ucIR_Seq & 0xfe))
  47.                 {
  48.                     ucIR_State  = 0;    // Command crashed; Back to idle state
  49.     
  50.                     ((unsigned int *)ucIR_Cmd)[0]   = 0;
  51.                 }
  52.             }
  53.             else                        // ucIR_State is 1
  54.             {
  55.                 if (0xff == ucIR_Seq)
  56.                 {
  57.                     ucIR_State  = 0x07; // Start to decode new command and clear old command
  58.     
  59.                     ((unsigned int *)ucIR_Cmd)[0]   = 0;
  60.                 }
  61.                 else
  62.                 {
  63.                     ucIR_State  = 0;    // Fire repeat command and reset to idle
  64.                     ucIR_Cmd[1] = ucIR_Cmd[0] ? 8 : 0;
  65.                 }
  66.             }
  67.             
  68.             ucIR_Seq    = ucIR_Seq << 1;
  69.         }
  70.     }
  71.     else
  72.     {
  73.         ucIR_Seq    = (ucIR_Seq << 1) | bIR_GPIO;
  74.         if (0x07 == ucIR_Seq)   ucIR_State  = 1;    // Leader code detected
  75.     }
  76.     if (54 <= ++ucTimerCnt)        // 0.375ms*54 = 20.25ms passed
  77.     {
  78.         ucTimerCnt          = 0;
  79.         bNotify_Timer0_Int  = 1;    // Post Timer0's timeout message
  80.         if (usOSD_Timer)    usOSD_Timer -= 1;   // For OSD Timeout
  81.         // Clear command after 144ms if not repeat code
  82.         if (0 == ucIR_State)
  83.         {
  84.             if (ucIR_Cmd[1] && 0 == --ucIR_Cmd[1])      ucIR_Cmd[0] = 0;
  85.         }
  86.     }
  87. #else
  88. // mega #if(MCU_TYPE == MCU_WINBOND) //anson 050519
  89.     TL0         = TIME0_COUNTER_LBYTE;  // Reload Timer0 low-byte
  90.     TH0         = TIME0_COUNTER_HBYTE;  // Reload Timer0 high-byte
  91.     bNotify_Timer0_Int  = 1;    // Post Timer0's timeout message
  92.     if (usOSD_Timer)    usOSD_Timer -= 1;   // For OSD Timeout
  93.  /*   mega //anson 050519
  94. #else
  95.  TL0 = TIME1_COUNTER_LBYTE;  // Reload Timer1 low-byte
  96.      TH0 = TIME1_COUNTER_HBYTE;  // Reload Timer1 hifh-byte
  97.  TR0 = 1;                    // Stop Timer1
  98.      bNotify_Timer1_Int  = 1;    // Post Timer1's timeout message  
  99.      ucTimerCounter++;
  100.      if(ucTimerCounter>20)
  101.      {
  102.      ucTimerCounter=0;
  103.      bNotify_Timer0_Int  = 1;    // Post Timer0's timeout message
  104.         if (usOSD_Timer)    usOSD_Timer -= 1;   // For OSD Timeout
  105.      }
  106. #endif
  107. */
  108. #endif
  109. }
  110. ///////////////////////////////////////////////////////////////////////
  111. // Interrupt Service Routine for Timer1 (Fired in 1ms after restart) //
  112. ///////////////////////////////////////////////////////////////////////
  113. //mega #if(MCU_TYPE == MCU_WINBOND) //Use the standard MCU //anson 050519
  114. void IntProc_Timer1(void) interrupt 3
  115. {   
  116.     TR1 = 0;                    // Stop Timer1
  117.     TL1 = TIME1_COUNTER_LBYTE;  // Reload Timer1 low-byte
  118.     TH1 = (bAutoInProgress == 1) ? TIME1_COUNTER_HHBYTE : TIME1_COUNTER_HBYTE;  // Reload Timer1 hifh-byte
  119.     bNotify_Timer1_Int  = 1;    // Post Timer1's timeout message    
  120. }
  121. /////////////////////////////////////////////////////////////////////////////////
  122. // Function Routines
  123. /////////////////////////////////////////////////////////////////////////////////
  124. void Delay_Xms(unsigned char x)     // For delay 0 ~ 255 ms
  125. {
  126.     if (x)
  127.     {
  128.         bNotify_Timer1_Int  = 0;    // Clear timer1's timeout message
  129. //        TL1 = TIME1_COUNTER_LBYTE;  // Reload Timer1 low-byte
  130. //        TH1 = TIME1_COUNTER_HBYTE;  // Reload Timer1 hifh-byte
  131.         TR1 = 1;                    // Start timer1
  132.         
  133.         while (1)
  134.         {
  135.             if (bNotify_Timer1_Int)
  136.             {
  137.                 bNotify_Timer1_Int  = 0;    // Clear timer1's timeout message
  138.                 if (--x)    TR1 = 1;        // Restart timer1 if not timeout
  139.                 else        return;         // Timeout for X ms.
  140.             }
  141.         }
  142.     }
  143. }
  144. /*mega  //anson 050519
  145. #else //Use just one timer
  146. /////////////////////////////////////////////////////////////////////////////////
  147. // Function Routines
  148. /////////////////////////////////////////////////////////////////////////////////
  149. void Delay_Xms(unsigned char x)     // For delay 0 ~ 255 ms
  150. {
  151. //    unsigned char i;    
  152.     if (x)
  153.     {
  154.         bNotify_Timer1_Int  = 0;    // Clear timer1's timeout message
  155.         //TR1 = 1;                    // Start timer1
  156.         while (1)
  157.         {
  158.             if (bNotify_Timer1_Int)
  159.             {
  160.                 bNotify_Timer1_Int  = 0;    // Clear timer1's timeout message
  161.                 if (--x)    x=x;//TR1 = 1;        // Restart timer1 if not timeout
  162.                 else        return;         // Timeout for X ms.
  163.             }
  164.         }
  165.     }
  166. }
  167. #endif
  168. */