|
從論壇看到的一位朋友發(fā)的紅外解碼程序
請(qǐng)問(wèn)大家這個(gè)程序?yàn)槭裁床慌袛嘤脩舸a 只要數(shù)據(jù)碼對(duì)就能正常
紅色字體的用戶碼 我的這個(gè)遙控器實(shí)際用戶碼是01FE
單片機(jī)源程序如下:
/************* 用戶系統(tǒng)配置 **************/
#define MAIN_Fosc 12000000L //定義主時(shí)鐘, 模擬串口和紅外接收會(huì)自動(dòng)適應(yīng)。5~36MHZ
#define D_TIMER0 125 //選擇定時(shí)器時(shí)間, us, 紅外接收要求在60us~250us之間
#define User_code 0xB649 //定義紅外接收用戶碼
/************* 以下宏定義用戶請(qǐng)勿修改 **************/
#include "reg51.H"
#define uchar unsigned char
#define uint unsigned int
#define freq_base (MAIN_Fosc / 1200)
#define Timer0_Reload (65536 - (D_TIMER0 * freq_base / 10000))
/************* 本地常量聲明 **************/
/************* 本地變量聲明 **************/
sbit P_IR_RX = P3^2; //定義紅外接收輸入端口
sbit userLed1=P3^0;
sbit userLed2=P3^1;
sbit userLed3=P3^3;
sbit userLed4=P3^4;
sbit userLed5=P3^5;
bit P_IR_RX_temp; //Last sample
bit B_IR_Sync; //已收到同步標(biāo)志
uchar IR_SampleCnt; //采樣計(jì)數(shù)
uchar IR_BitCnt; //編碼位數(shù)
uchar IR_UserH; //用戶碼(地址)高字節(jié)
uchar IR_UserL; //用戶碼(地址)低字節(jié)
uchar IR_data; //數(shù)據(jù)原碼
uchar IR_DataShit; //數(shù)據(jù)反碼
bit B_IrUserErr; //User code error flag
bit B_IR_Press; //Key press flag,include repeat key.
uchar IR_code; //IR code 紅外鍵碼
unsigned int Count1ms=0; //一毫秒計(jì)數(shù)器
unsigned char T1RH=0xFC; //定時(shí)器時(shí)長(zhǎng) 高位值
unsigned char T1RL=0x67; //定時(shí)器時(shí)長(zhǎng) 低位值
/************* 本地函數(shù)聲明 **************/
void Tx1Send(uchar dat);
uchar HEX2ASCII(uchar dat);
void InitTimer(void);
void PrintString(unsigned char code *puts);
/************* 外部函數(shù)和變量聲明 *****************/
/* 配置并啟動(dòng)T0,ms-T0定時(shí)時(shí)間 */
void ConfigTimer1(unsigned int ms)
{
unsigned long tmp; //臨時(shí)變量
// tmp = 11059200 / 12; //定時(shí)器計(jì)數(shù)頻率
tmp = MAIN_Fosc / 12; //定時(shí)器計(jì)數(shù)頻率
tmp = (tmp * ms) / 1000; //計(jì)算所需的計(jì)數(shù)值
tmp = 65536 - tmp; //計(jì)算定時(shí)器重載值
tmp = tmp + 18; //補(bǔ)償中斷響應(yīng)延時(shí)造成的誤差
T1RH = (unsigned char)(tmp>>8); //定時(shí)器重載值拆分為高低字節(jié)
T1RL = (unsigned char)tmp;
TMOD &= 0x0F; //清零T0的控制位
TMOD |= 0x10; //配置T0為模式1
TH1= T1RH; //加載T0重載值
TL1 = T1RL;
ET1 = 1; //使能T0中斷
TR1 = 1; //啟動(dòng)T0
}
/* T0中斷服務(wù)函數(shù) */
void InterruptTimer1() interrupt 3
{
static unsigned char ti5=0;
TH1 = T1RH; //重新加載重載值
TL1 = T1RL;
ti5++; //計(jì)數(shù)標(biāo)志自加1
if(ti5==20) //判斷是否到1s
{
ti5=0; //將靜態(tài)變量清0
// FlashLed=~FlashLed; //LED位求反
}
}
/********************* 主函數(shù) *************************/
void main(void)
{
userLed1=0;
userLed2=0;
userLed3=0;
userLed4=0;
userLed5=0;
EA=1; //允許總中斷
ConfigTimer1(25);
InitTimer(); //初始化Timer
while(1)
{
if(B_IR_Press) //有IR鍵按下
{
switch(IR_code)
{
case 0x01:
userLed1=~userLed1;
break;
case 0x00:
userLed2=~userLed2;
break;
case 0x04:
userLed3=~userLed3;
break;
case 0x08:
userLed4=~userLed4;
break;
case 0x43:
userLed5=~userLed5;
break;
}
B_IR_Press = 0; //清除IR鍵按下標(biāo)志
}
}
}
//*******************************************************************
//*********************** IR Remote Module **************************
//*********************** IR Remote Module **************************
//this programme is used for Receive IR Remote (HT6121).
//data format: Synchro,AddressH,AddressL,data,/data, (total 32 bit).
//send a frame(85ms), pause 23ms, send synchro of another frame, pause 94ms
//data rate: 108ms/Frame
//Synchro:low=9ms,high=4.5/2.25ms,low=0.5626ms
//Bit0:high=0.5626ms,low=0.5626ms
//Bit1:high=1.6879ms,low=0.5626ms
//frame space = 23 ms or 96 ms
/******************** 紅外采樣時(shí)間宏定義, 用戶不要隨意修改 *******************/
#if ((D_TIMER0 <= 250) && (D_TIMER0 >= 60))
#define D_IR_sample D_TIMER0 //定義采樣時(shí)間,在60us~250us之間
#endif
#define D_IR_SYNC_MAX (15000/D_IR_sample) //SYNC max time
#define D_IR_SYNC_MIN (9700 /D_IR_sample) //SYNC min time
#define D_IR_SYNC_DIVIDE (12375/D_IR_sample) //decide data 0 or 1
#define D_IR_DATA_MAX (3000 /D_IR_sample) //data max time
#define D_IR_DATA_MIN (600 /D_IR_sample) //data min time
#define D_IR_DATA_DIVIDE (1687 /D_IR_sample) //decide data 0 or 1
#define D_IR_BIT_NUMBER 32 //bit number
//*******************************************************************************************
//**************************** IR RECEIVE MODULE ********************************************
void IR_RX_HT6121(void)
{
uchar SampleTime;
IR_SampleCnt++; //Sample + 1
F0 = P_IR_RX_temp; //Save Last sample status
P_IR_RX_temp = P_IR_RX; //Read current status
if(F0 && !P_IR_RX_temp) //Last sample is high,and current sample is low, so is fall edge
{
SampleTime = IR_SampleCnt; //get the sample time
IR_SampleCnt = 0; //Clear the sample counter
if(SampleTime > D_IR_SYNC_MAX) B_IR_Sync = 0; //large the Maxim SYNC time, then error
else if(SampleTime >= D_IR_SYNC_MIN) //SYNC
{
if(SampleTime >= D_IR_SYNC_DIVIDE)
{
B_IR_Sync = 1; //has received SYNC
IR_BitCnt = D_IR_BIT_NUMBER; //Load bit number
}
}
else if(B_IR_Sync) //has received SYNC
{
if(SampleTime > D_IR_DATA_MAX)
B_IR_Sync=0; //data samlpe time to large
else
{
IR_DataShit >>= 1; //data shift right 1 bit
if(SampleTime >= D_IR_DATA_DIVIDE) IR_DataShit |= 0x80; //devide data 0 or 1
if(--IR_BitCnt == 0) //bit number is over?
{
B_IR_Sync = 0; //Clear SYNC
if(~IR_DataShit == IR_data) //判斷數(shù)據(jù)正反碼
{
if((IR_UserH == (User_code / 256)) &&
IR_UserL == (User_code % 256))
B_IrUserErr = 0; //User code is righe
else B_IrUserErr = 1; //user code is wrong
IR_code = IR_data;
B_IR_Press = 1; //數(shù)據(jù)有效
}
}
else if((IR_BitCnt & 7)== 0) //one byte receive
{
IR_UserL = IR_UserH; //Save the User code high byte
IR_UserH = IR_data; //Save the User code low byte
IR_data = IR_DataShit; //Save the IR data byte
}
}
}
}
}
/**************** Timer初始化函數(shù) ******************************/
void InitTimer(void)
{
TMOD = 0; //for STC15Fxxx系列 Timer0 as 16bit reload timer.
TH0 = Timer0_Reload / 256;
TL0 = Timer0_Reload % 256;
ET0 = 1;
TR0 = 1;
EA = 1;
}
/********************** Timer0中斷函數(shù)************************/
void timer0 (void) interrupt 1
{
IR_RX_HT6121();
}
|
|