📄 ir.c
字号:
//---------------------------------------------------------------
//
// Copyright (c) 2006-2007 E-compass electronic Co.,Ltd.
// All Right Reserved.
//
// $Author walnutcy
// $Email walnutcy@gmail.com
// $Version v1.0
// $FileName
// $Since
// $Log
//
// DESCRIPTION
//
// History:
// <author> <time> <version > <desc>
//---------------------------------------------------------------
#include "..\includes.h"
#include "IRTable.h"
////////////// global varible ///////////////////////////////////////
IU8 gu8IRBuf[4];
IU8 gu8IRData;
U8 gu8IRState;
S8 lIrBitCnt;
U16 lIrOldCnt ;
S8 lIrFirst;
U16 lIrSubTime;// the counter between the drop impulses
U16 lIrNewCnt;
#define IR_USER_CODE_0 0x10
#define IR_USER_CODE_1 0x67
////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
static U8 IR_FindIR(U8 cmd) REENTRANT
{
U8 i = 0;
while ((cmd != gIRTable[i])&&(40 > i))
{
i++;
}
if (40 <= i)
{
return 0;
}
return (i + 1);
}
/////////////////////////////////////////////////////////////////////////////
U8 IR_GetRevData(U8* buf) REENTRANT// get received data
{
U8 tmp = gu8IRState;
if(gu8IRState)
{
gu8IRState = IR_INVALID;
*(buf+2) = tmp;
tmp = IR_FindIR(gu8IRData);
if (0 != tmp)
{
*(buf+0) = tmp;
*(buf+1) = ~tmp;
return 3;
}
}
return (0);
}
void IR_Init() REENTRANT //initialize system interrupt
{
gu8IRState = IR_INVALID;//IR_VALID;//
gu8IRData = 0xFF;
lIrBitCnt = 0;
lIrOldCnt = 0;
lIrFirst = 1;
CCON = 0x00;
CL = 0x00;
CH = 0x00;
CMOD = 0x00;// enable interrupt
CCAPM0 = 0x49; // pwm 0 : 16-bit timer(1ms)
CCAPM1 = 0x11; // pwm 1 : falling edge trigger; IR
CCAP0L = SYS_TIME_TICK & 0xff;
CCAP0H = SYS_TIME_TICK >> 8;
CR = 1; // start PCA timer
return;
}
#if 0
// get the timer value of the triggering by the falling edge of the IR from the remoter
static U16 IR_GetCurrTime()
{
U16 l_count ;
l_count = CCAP1H;
l_count = (l_count << 8) + CCAP1L;
return l_count;
}
#endif
void IR_Decode() REENTRANT
{
U8 temp;//used as a temp bit value for the received data
// lIrNewCnt = IR_GetCurrTime();//the counter value
lIrNewCnt = CCAP1H;
lIrNewCnt = (lIrNewCnt<<8) + CCAP1L;
lIrSubTime = lIrNewCnt - lIrOldCnt;
lIrOldCnt = lIrNewCnt;
if (lIrFirst)
{
lIrFirst = 0;
return;
}
// deal with the lIrSubTime
if ( (NUM0_MIN < lIrSubTime)&&(lIrSubTime < NUM0_MAX) )// 0
{
temp = 0;
}
else if ( (NUM1_MIN < lIrSubTime)&&(lIrSubTime < NUM1_MAX) )// 1
{
temp = 1;
}
else if ( (LEAD_MIN < lIrSubTime)&&(lIrSubTime < LEAD_MAX) )// leader code
{
gu8IRBuf[0] = 0;
gu8IRBuf[1] = 0;
gu8IRBuf[2] = 0;
gu8IRBuf[3] = 0;
gu8IRData = 0xFF;
lIrBitCnt = 0;
return;
}
else if ( (KEEP_MIN < lIrSubTime)&&(lIrSubTime < KEEP_MAX) )// keep depressed
{
gu8IRState = IR_KEEP_DEPRESSED;
OsSendMsg(TASK_IR);
return;
}
else// interference
{
// do nothing
return;
}
/* deal with the temp value */
if (31 > lIrBitCnt)
{
gu8IRBuf[lIrBitCnt >> 3] |= temp << (7 - (lIrBitCnt & 0x07));
}
else if ( 31 == lIrBitCnt)// received the last bit
{
gu8IRBuf[3] |= temp;
// the relation of the invert of data code and the data code
// \data ^ data == 0xff (??)
temp = ~ gu8IRBuf[3];
if ( gu8IRBuf[2] == temp )
{
if ((IR_USER_CODE_0 == gu8IRBuf[0])&&
(IR_USER_CODE_1 == gu8IRBuf[1]))
{
gu8IRState = IR_VALID;
gu8IRData = gu8IRBuf[2];
OsSendMsg(TASK_IR);
}
else
{ gu8IRData = 0xFF;
}
}
lIrFirst = 1;
}
lIrBitCnt++;
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -