📄 ir.c
字号:
/******************************************************************************
Copyright (c) 2005 MStar Semiconductor, Inc.
All rights reserved.
[Module Name]: IR.c
[Date]: 01-Jun-2005
[Comment]:
Remote control recive functions.
[Reversion History]:
*******************************************************************************/
#define _IR_C_
/********************
* INCLUDE FILES *
*********************/
#include "Project.h"
#include "TypeDef.h"
#include "Global.h"
#include "Mcu.h"
#include "Mcu_reg.h"
#include "Misc.h"
#include "Debug.h"
#include "IR.h"
#if IR_ENABLE
/**********************
* FUNCTION PROTOTYPES *
***********************/
#if 0
void sysIRTimer2Interrupt(void) interrupt 5
{
ET2 = 0; // disable timer2 interrupt
TR2 = 0; // stop timer2
if (!g_uwIrCycleTimer || g_bIrRepeatStatus)
g_bIrExecute = 0;
TR2 = 1; // start time12
}
////////////////////////////////////////////////////////////////////////////////////////
void sysIRInterrupt1ISR(void) interrupt 2
{
TR2 = 0; // stop timer2
g_uwIrPeriodCnt = (((WORD)TH2) * 0x100) + TL2;
TH2 = HIBYTE(IR_TIMER_COUNTER);
TL2 = LOBYTE(IR_TIMER_COUNTER);
TF2 = 0; // clear timer2 overflow flag
ET2 = 1; // enable timer2 interrupt
TR2 = 1; // start timer2
// --------------------------------------------------------------
if (g_bIrExecute == 0) // 1st tigger
{
g_bIrExecute = 1;
g_ucIrBitsCnt = 0;
g_uwIrCycleTimer = IR_CYCLE_TIME;
return;
}
// --------------------------------------------------------------
g_uwIrPeriodCnt -= IR_TIMER_COUNTER; // difference of triggers
if (g_ucIrBitsCnt == 0) // check lead header
{
// check repeat lead code
if (g_bIrRepeatStatus)
{
if (g_uwIrPeriodCnt > (IR_REPAEAT_LEAD_CNT-IR_TOLERANCE_CNT) && g_uwIrPeriodCnt < (IR_REPAEAT_LEAD_CNT+IR_TOLERANCE_CNT))
{
if (g_bIrCheckRepeat)
{
g_bIrRepeat = 1;
g_bIrDetect = 1;
g_uwIrTimer = IR_DELAY_TIME;
}
}
g_bIrExecute = 0;
g_uwIrCycleTimer = IR_CYCLE_TIME;
return;
}
// check genral lead code
if (g_uwIrPeriodCnt > (IR_1ST_LEAD_CNT-IR_TOLERANCE_CNT) && g_uwIrPeriodCnt < (IR_1ST_LEAD_CNT+IR_TOLERANCE_CNT))
{
g_bIrError = 0;
g_bIrRepeat = 0;
g_ucDataCode = 0x00;
g_ucIrDataFifo = 0x00;
}
else // fail lead code
{
g_bIrExecute = 0;
return;
}
}
// --------------------------------------------------------------
else // receive byte code
{
// receive bit
g_ucIrDataFifo >>= 1;
if (g_uwIrPeriodCnt > (IR_LOGIC0_CNT-IR_TOLERANCE_CNT) && g_uwIrPeriodCnt < (IR_LOGIC0_CNT+IR_TOLERANCE_CNT))
g_ucIrDataFifo &= ~_BIT7; // logic 0
else if (g_uwIrPeriodCnt > (IR_LOGIC1_CNT-IR_TOLERANCE_CNT) && g_uwIrPeriodCnt < (IR_LOGIC1_CNT+IR_TOLERANCE_CNT))
g_ucIrDataFifo |= _BIT7; // logic 1
else // mismatch
g_bIrError = 1;
// check bits counter
switch(g_ucIrBitsCnt)
{
case 8: // 1st byte
if (g_ucIrDataFifo != IR_HEADER_CODE0)
g_bIrError = 1;
break;
case 16: // 2nd byte
if (g_ucIrDataFifo != IR_HEADER_CODE1)
g_bIrError = 1;
break;
case 24: // 3rd byte
g_ucDataCode = g_ucIrDataFifo;
break;
case 32: // 4th byte
if (g_ucDataCode != ~g_ucIrDataFifo)
g_bIrError = 1;
if (!g_bIrError)
{
g_bIrRepeatStatus = 1;
g_ucIrCode = g_ucDataCode;
g_uwIrCycleTimer = IR_CYCLE_TIME;
g_bIrDetect = 1;
g_bIrCommand = 1;
g_bIrTime = 0; // IR 1st time repeat
g_bIrCheckRepeat = 0;
g_uwIrTimer = IR_DELAY_TIME;
}
g_bIrExecute = 0;
return;
} // switch
} // if (g_ucIrBitsCnt == 0)
g_ucIrBitsCnt++; // next bit
}
#endif
////////////////////////////////////////////////////////////////////////////////////////
void sysIRDetectTimer0( void )
{
if (g_uwIrCycleTimer)
g_uwIrCycleTimer--;
else
{
g_bIrExecute = 0;
g_bIrRepeatStatus = 0;
}
//--- check IR repeat command delay time ---
if ( g_bIrCommand ) // check IR command start
{
g_uwIrTimer--;
if ( g_uwIrTimer == 0 ) // time out
{
if ( g_bIrTime ) // for 1st IR command
{
// IR 1st command executed then delay
g_bIrTime = 0; // clear 1st IR command status
g_uwIrTimer = IR_REPEAT_END_TIME;
g_bIrCheckRepeat = 1; // ready to repeat
}
else // IR repeat end or IR decode too late
{
// IR repeat command delay timer
g_bIrCommand = 0;
g_ucIrCode = 0xff;
g_bIrCheckRepeat = 0;
}
} // if time out
} // if IR command
//--- IR press 0-9 key ending timer for TUNER Channel---
/*
if ( g_bIrNumKeyStart || g_bIr100Key ) // IR key start
{
g_wIrNumKeyTimer--;
if ( g_wIrNumKeyTimer == 0 ) // time out
{
if ( _testbit_( g_bIr100Key ) )
g_ucIrNumKey = ( g_ucIrNumKey * 10 ) + 100;
g_bIrNumKeyStart = 0;
g_bIrKeyNumEnd = 1;
g_wIrNumKeyTimer = 200;
}
} // if IR key start*/
}
////////////////////////////////////////////////////////////////////////////////////////
void sysIRDecodeRepaeat(WORD wRepeatTime)
{
if ( g_bIrRepeat ) // repeat command
{
g_bIrRepeat = 0;
g_bIrTime = 1; // start TIMER0 to repeat IR command
g_uwIrTimer = wRepeatTime; // reset repeat timer
}
else // first command before repeat
{
g_bIrTime = 1; // start TIMER0 to repeat IR command
g_uwIrTimer = IR_REPEAT_START_TIME; // set timer to check repeat
}
}
////////////////////////////////////////////////////////////////////////////////////////
void sysIRDecodeEnd( void )
{
g_bIrCommand = 0; // end IR command
// clear repeat flag
g_bIrCheckRepeat = 0;
g_bIrRepeat = 0;
g_uwIrTimer = IR_DELAY_TIME; // any value
}
////////////////////////////////////////////////////////////////////////////////////////
void sysIRInitial(void)
{
IR_PIN = 1;
g_bIrCommand = 0; // end IR command
g_bIrExecute = 0; // clear busy flag
g_bIrCheckRepeat = 0; // clear repeat flag
g_bIrRepeat = 0;
g_bIrDetect = 0;
g_uwIrTimer = 0;
g_uwIrCycleTimer = 0;
g_ucIrCode = 0xFF; // set IR code to 0xFF (No Key)
T2CON = 0x00;
T2MOD = 0x00;
IT1 = 1; // set INT1 level trigger
EX1 = 1; //enable MCU INT1
}
#endif // IR_ENABLE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -