📄 timer.c
字号:
#include "..\inc\public2.h"
#define IR_CusCode 0x04
code BYTE Key_Table[64] =
{
00,00,00,00,00,01,01,01,
01,01,01,01,01,01,01,01,
02,02,02,02,02,02,02,02,
02,02,03,03,03,03,03,03,
03,04,04,04,04,04,04,04,
04,04,04,04,05,05,05,05,
05,05,05,05,05,06,06,06,
06,0x6,0x6,0x6,0xF,0xF,0xF,0xF,
};
data unsigned char DLYTimer;
data unsigned int OSDTimer;
data unsigned int SleepTimer;
data BYTE KeyTimer;
data BYTE KeyFlag;
data BYTE Key;
data bit IR_INT0,IR_INT=1;
data BYTE IR_RevCusCode;
data BYTE IR_RevCusCode_B;
data BYTE IR_Value = 0xFF,IR_NUM = 0xFF;
data BYTE Key_Input0,Key_Input =0x77,Key_Value = 0xFF;
data BYTE Key_AD1,Key_AD2;
data BYTE IR_RevDataCode = 0xFF;
data BYTE TMR0_CNT = 0;
data BYTE SecTimer;
data unsigned int NoSigTimer;
data BYTE IR_Snd;
data BYTE IR_Rev_CNT;
data BYTE IR_RevCode;
data BYTE IR_SndDataCode;
data unsigned int IR_TMR = 0xFFFF;
data BYTE IR_TMP = 0;
data BYTE IR_SndStatus = 0, IR_Snd_CNT = 0xFF, IR_SndCode_CNT, IR_SndBit_CNT;
data BYTE TEST = 0;
data BYTE IR_OUT = 1;
data bit IR_Pressed = 0, Key_Pressed = 0;
void IR_Send_Code();
/**--------------------------------------------------------------------------
* Name void Timer_Init(void)
*
* Description Init Timer,
* Timer2 for UART setting Baud rate=38400 in 27MHz
*
* Return
* DATE Author Description
* ===========================================================================
* 2004/06/24 Eson W.
* 2005/09/11 Microntek Modified
**/
void Timer_Init(void)
{
SCON = 0x50;
TI = 0; //Clear UART Tx and Rx
RI = 0;
//************************************************
//****** Set Timer0 /Timer1 ******
//************************************************
TMOD=0x21; //Timer0 in Mode 1, and Timer1 in Mode 2
TR0 = 0; //Stop timer 0
TH0=0xD8; //Timer0 working in Mode 1 and set 5m sec.
TL0=0xF0; //Working Cry=24MHz 1/24M * 12 = 0.5us
//10ms = 0.5 * 10000
//TH0 = (65536 - 10000) / 256 = 0xD8
//TL0 = (65536 - 10000) % 256 = 0xF0
TR0 = 1; //Enable Timer 0
// ETimer For IR Receiving and Sending
ETMOD = 0x00; // Count Up
ETCTR = 0x00; // TR2 = 0
RCAPETH = 0xFF;
RCAPETL = 0x8F;
THET = 0xFF;
TLET = 0x8F;
ETCTR |= 0x04; // TR2 = 1
ETCTR &= 0x7F; // TF2 = 0
EINT1PEN = 0x40; // Enable ETimer Interrupt
// Timer1 for BAUD RATE GENERATOR
TR1 = 0; // Stop timer 1
TH1 = 243; // 243 for Baud Rate 9600 bps
TL1 = 243; //
//
TR1 = 1;
// Start count Timer1
TF1 = 0;
PCON = 0x80; // SMOD1 = 1 for Baud Rate Generator
IT0 = 1;
IE0 = 0;
IP = 0x14; //14 // Interrupt Priority // |
// bit7:Reserved // |
// bit6:TIMER2 IRQ // |
// bit5:IIC // |
// bit4:UART 1st Priority // |
// bit3:TIMER1 IRQ // |
// bit2:External/INT1 // |
// bit1:TIMER0 IRQ // |
// bit0:Enable External/INT0 // |
IE = 0x97; //8051 Interrupt IRQ setting // |
// bit7:All IRQ setting,EA=1 // |
// bit6:TIMER2 IRQ // |
// bit5:IIC // |
// bit4:UART // |
// bit3:TIMER1 IRQ // |
// bit2:External/INT1 // |
// bit1:TIMER0 IRQ // |
// bit0:Enable External/INT0 // |
}
/**--------------------------------------------------------------------------
* Name void interrupt Timer0()
*
* Description This is a timer interrupt. It will entry when every 10ms.
* It will Dec. Interval counter when Service flag setting
*
* Flow Chart
*
* Return
*
* DATE Author Description
* ===========================================================================
* 2003-02-20 KM Ho This is first time implement
* 2005-09-11 Microntek Modified
**/
void Timer0(void) interrupt 1 using 3
{
TR0 = 0; //STOP COUNT Timer0
if ( DLYTimer ) DLYTimer --;
TMR0_CNT = (TMR0_CNT+1)&0x1;
if ( IR_TMR > 900 )
{
IR_Pressed = 0;
}
Key_Input0 = Key_Input;
/*
switch (TMR0_CNT)
{
case 0:
PADMOD4 = 0x10;
P6_2 = 0;
break;
case 1:
Key_Input = (Key_Input&0xF8) | ((P6_5&0x1)<<2)|((P6_6&0x1)<<1)|((P6_7&0x1));
PADMOD4 = 0x08;
P6_3 = 0;
break;
case 2:
Key_Input = (Key_Input&0xC7) | ((P6_5&0x1)<<5)|((P6_6&0x1)<<4)|((P6_7&0x1)<<3);
PADMOD4 = 0x04;
P6_4 = 0;
break;
case 3:
Key_Input = (Key_Input&0x3F) | ((P6_5&0x1)<<7)|((P6_6&0x1)<<6);
break;
}
*/
switch (TMR0_CNT)
{
case 0:
Key_Input = (Key_Input&0x0F) | ((P6_3&0x1)<<7)|((P6_2&0x1)<<6)|((P6_1&0x1)<<5)|((P6_0&0x1)<<4);
P1_6 = 0;
P1_7 = 1;
break;
case 1:
Key_Input = (Key_Input&0xF0) | ((P6_3&0x1)<<3)|((P6_2&0x1)<<2)|((P6_1&0x1)<<1)|(P6_0&0x1);
P1_7 = 0;
P1_6 = 1;
break;
}
/*
Key_Input = ((P6_0&0x1)<<7)|((P6_1&0x1)<<6)|((P6_2&0x1)<<5)|((P6_3&0x1)<<4)|((P6_4&0x1)<<3)|
((P6_5&0x1)<<2)|((P6_6&0x1)<<1)|(P6_7&0x1);
*/
// Key_Input = ((P6_1&0x1)<<6)|((P6_2&0x1)<<5)|((P6_3&0x1)<<4)|((P6_4&0x1)<<3)|
// ((P6_5&0x1)<<2)|((P6_6&0x1)<<1)|(P6_7&0x1);
if (( TMR0_CNT&1) == 0 )
{
if (OSDTimer) OSDTimer--;
SecTimer++;
if (SecTimer >= 50 )
{
SecTimer = 0;
if (NoSigTimer)
NoSigTimer--;
if (SleepTimer)
SleepTimer--;
}
if ( Key_Input0 == Key_Input )
{
switch (Key_Input)
{
case 0xFE:
Key_Value = MENU;
Key_Pressed = 1;
break;
case 0xFD:
Key_Value = UP;
Key_Pressed = 1;
break;
case 0xFB:
Key_Value = DOWN;
Key_Pressed = 1;
break;
case 0xF7:
Key_Value = RIGHT;
Key_Pressed = 1;
break;
case 0xEF:
Key_Value = LEFT;
Key_Pressed = 1;
break;
case 0xDF:
Key_Value = ENTER;
Key_Pressed = 1;
break;
case 0xBF:
Key_Value = ONOFF;
Key_Pressed = 1;
break;
case 0x7F:
Key_Value = CALL;
Key_Pressed = 1;
break;
default:
Key_Value = 0xFF;
Key_Pressed = 0;
break;
}
}
if ( IR_Pressed || Key_Pressed )
{
if ( ! IR_Pressed )
Key = Key_Value;
if (KeyTimer != 0xFF ) KeyTimer++;
if (KeyTimer == 0x2 ) KeyFlag = 1;
}
else
{
Key = 0xFF;
KeyTimer = 0;
KeyFlag = 0;
}
}
TH0=0xD8; //Timer0 working in Mode 1 and set 5m sec.
TL0=0xF0; //Working Cry=24MHz 1/24M * 12 = 0.5us
//10ms = 0.5 * 10000
//TH0 = (65536 - 10000) / 256 = 0xD8
//TL0 = (65536 - 10000) % 256 = 0xF0
TF0 = 0;
TR0 = 1; //Start count Timer0
}
void Etimer(void) interrupt 2 using 2
{
IR_SND = IR_OUT;
if ( IR_TMR < 0xFFFF )
IR_TMR ++;
if ( IR_TMP < 4 )
{
IR_TMP++;
ETCTR &= 0x7F; // Clr TF2
// TF1 = 0;
return;
}
IR_TMP = 0;
// The Following Sub run for every 0.5625ms
if ( IR_Snd_CNT >= 191 ) // Not in Send Status
{
if ( IR_Pressed || Key_Pressed )
{
switch ( IR_SndStatus )
{
case 0:
IR_SndStatus = 1;
break;
default : // Send Code 1st time
if ( IR_SndDataCode != Key )
IR_SndStatus = 1;
else
IR_SndStatus = 2;
break;
}
IR_SndDataCode = Key;
IR_Snd_CNT = 0;
IR_SndCode_CNT = 0;
IR_SndBit_CNT = 0;
}
else
IR_SndStatus = 0;
}
else // IR Sending Status
{
switch ( IR_SndStatus )
{
case 1: // Send Code 1st time
if ( IR_Snd_CNT >= 24 && IR_Snd_CNT < 120 )
IR_Send_Code();
else if ( IR_Snd_CNT == 0 )
IR_OUT = 0;
else if ( IR_Snd_CNT == 16 )
IR_OUT = 1;
else if ( IR_Snd_CNT == 120 )
IR_OUT = 0;
else if ( IR_Snd_CNT == 121 )
IR_OUT = 1;
break;
case 2: // Send Code Repeat
switch ( IR_Snd_CNT )
{
case 0:
IR_OUT = 0;
break;
case 16:
IR_OUT = 1;
break;
case 20:
IR_OUT = 0;
break;
case 21:
IR_OUT = 1;
break;
}
break;
}
IR_Snd_CNT++;
}
ETCTR &= 0x7F; // Clr TF2
// TF1 = 0;
}
void IR_Send_Code()
{
if ( IR_SndBit_CNT == 0 )
{
if ( IR_SndCode_CNT == 0 )
{
IR_Snd = IR_CusCode;
}
else if ( IR_SndCode_CNT == 8 )
{
IR_Snd = ~IR_CusCode;
}
else if ( IR_SndCode_CNT == 16 )
{
IR_Snd = IR_SndDataCode;
}
else if ( IR_SndCode_CNT == 24 )
{
IR_Snd = ~IR_SndDataCode;
}
if ( IR_Snd & 0x80 )
IR_SndBit_CNT = 3;
else
IR_SndBit_CNT = 1;
IR_SndCode_CNT++;
IR_OUT = 0;
}
else
{
IR_SndBit_CNT --;
if ( IR_SndBit_CNT == 0 )
IR_Snd = IR_Snd<<1;
IR_OUT = 1;
}
}
void Int0(void) interrupt 0 using 3
{
data bit IR_Input;
data bit IR_Valid;
data unsigned int IR_Duration;
IR_Duration = IR_TMR;
IR_TMR = 0;
IR_Valid = 0;
if ( IR_Duration >= 50 )
{
IR_Rev_CNT = 0;
}
else if ( IR_Duration >= 6 && IR_Duration <= 13 ) // 1.125 ms
{
IR_Input = 0;
IR_Valid = 1;
}
else if ( IR_Duration >= 16 && IR_Duration <= 23 ) // 0.5625 ms
{
IR_Input = 1;
IR_Valid = 1;
}
if (IR_Valid)
{
IR_Rev_CNT++;
IR_RevCode <<= 1;
IR_RevCode |= IR_Input;
switch ( IR_Rev_CNT )
{
case 8:
IR_RevCusCode = IR_RevCode;
break;
case 16:
IR_RevCusCode_B = IR_RevCode;
break;
case 24:
IR_RevDataCode = IR_RevCode;
break;
case 32:
if ( IR_RevCode == (~IR_RevDataCode) && IR_RevCusCode == IR_CusCode
&& IR_RevCusCode == (~IR_RevCusCode_B) )
{
IR_Pressed = 1;
Key = IR_Value = IR_RevDataCode;
switch ( IR_Value )
{
case N_0:
IR_NUM = 0 ;
break;
case N_1:
IR_NUM = 1 ;
break;
case N_2:
IR_NUM = 2 ;
break;
case N_3:
IR_NUM = 3 ;
break;
case N_4:
IR_NUM = 4 ;
break;
case N_5:
IR_NUM = 5 ;
break;
case N_6:
IR_NUM = 6 ;
break;
case N_7:
IR_NUM = 7 ;
break;
case N_8:
IR_NUM = 8 ;
break;
case N_9:
IR_NUM = 9 ;
break;
default :
IR_NUM = 0xFF;
break;
}
}
break;
}
}
IE0 = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -