紅外通信—紅外遙控器NEC解碼程序
/**
***********************************************************************************************
* @file main.c
* @author xr
* @date 2014年3月31日10:26:47
* @version V1.2.3
* @brief 紅外通信 NEC協(xié)議進(jìn)行紅外遙控器解碼 顯示用戶碼和鍵碼到數(shù)碼管上
* @note單片機(jī)STC89C52RC MCU晶振11.0592MHZ
***********************************************************************************************
*/
#include
//紅外輸出數(shù)據(jù)口
sbit IRD = P3^3;//外部中斷引腳
bit irflag = 0;
unsigned char ircode[4];//接收解碼得到的數(shù)據(jù)
unsigned char code LedTable[] = {
0xC0, //"0"
0xF9, //"1"
0xA4, //"2"
0xB0, //"3"
0x99, //"4"
0x92, //"5"
0x82, //"6"
0xF8, //"7"
0x80, //"8"
0x90, //"9"
0x88, //"A"
0x83, //"B"
0xC6, //"C"
0xA1, //"D"
0x86, //"E"
0x8E //"F"
};
unsigned char LedBuff[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};//數(shù)碼管顯示緩沖區(qū)
unsigned char thr0, tlr0;
void ConfigTimer0(unsigned int xms);
void ConfigTimer1();
void main()
{
ConfigTimer0(1); //定時(shí)1ms
ConfigTimer1();
while (1)
{
if (irflag)//接收到紅外數(shù)據(jù),則刷新顯示
{
LedBuff[5] = LedTable[ircode[0] >> 4];//取用戶碼的高4位字節(jié)
LedBuff[4] = LedTable[ircode[0] & 0x0F];//取用戶碼的低四位字節(jié)
LedBuff[1] = LedTable[ircode[2] >> 4];//取鍵碼的高四位
LedBuff[0] = LedTable[ircode[2] & 0x0F];//取鍵碼的低四位
}
}
}
/**
* @brief 定時(shí)器T0配置
* @param 定時(shí)時(shí)間xms
* @retval 無
*/
void ConfigTimer0(unsigned int xms)
{
unsigned int tmp;
tmp = 65536-xms*11059200/12/1000;
thr0 = (unsigned char)(tmp >> 8);
tlr0 = (unsigned char)(tmp & 0x00FF);
TMOD &= 0xF0;
TMOD |= 0x01;//T0方式1
TH0 = thr0;
TL0 = tlr0;
TR0 = 1;
EA = 1;
ET0 = 1;
//設(shè)置定時(shí)器T0的中斷優(yōu)先級(jí)高于外部中斷的優(yōu)先級(jí)
//IP中斷優(yōu)先級(jí)寄存器 PT2 PS PT1 PX1 PT0 PX0
PT0 = 1;//提高T0的優(yōu)先級(jí),優(yōu)先進(jìn)行數(shù)碼管刷新,消除數(shù)碼管顯示抖動(dòng)
}
/**
* @brief 數(shù)碼管刷新
* @param 無
* @retval 無
*/
void refresh()
{
static unsigned char j = 0;
P0 = 0xFF;//消隱
P1 = (0x08 | j);//000 0 1 ADDR2 ADDR1 ADDR0
P0 = LedBuff[j++];
if (j >= 6)
j = 0;
}
/**
* @brief T0中斷服務(wù)
* @param 無
* @retval 無
*/
void Timer0_ISP() interrupt 1
{
TH0 = thr0;
TL0 = tlr0;
refresh();//數(shù)碼管刷新
}
評(píng)論