从论坛看到的一位朋友发的红外解码程序
请问大家这个程序为什么不判断用户码 只要数据码对就能正常
红色字体的用户码 我的这个遥控器实际用户码是01FE
单片机源程序如下:
/************* 用户系统配置 **************/
#define MAIN_Fosc 12000000L //定义主时钟, 模拟串口和红外接收会自动适应。5~36MHZ
#define D_TIMER0 125 //选择定时器时间, us, 红外接收要求在60us~250us之间
#define User_code 0xB649 //定义红外接收用户码
/************* 以下宏定义用户请勿修改 **************/
#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; //已收到同步标志
uchar IR_SampleCnt; //采样计数
uchar IR_BitCnt; //编码位数
uchar IR_UserH; //用户码(地址)高字节
uchar IR_UserL; //用户码(地址)低字节
uchar IR_data; //数据原码
uchar IR_DataShit; //数据反码
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; //一毫秒计数器
unsigned char T1RH=0xFC; //定时器时长 高位值
unsigned char T1RL=0x67; //定时器时长 低位值
/************* 本地函数声明 **************/
void Tx1Send(uchar dat);
uchar HEX2ASCII(uchar dat);
void InitTimer(void);
void PrintString(unsigned char code *puts);
/************* 外部函数和变量声明 *****************/
/* 配置并启动T0,ms-T0定时时间 */
void ConfigTimer1(unsigned int ms)
{
unsigned long tmp; //临时变量
// tmp = 11059200 / 12; //定时器计数频率
tmp = MAIN_Fosc / 12; //定时器计数频率
tmp = (tmp * ms) / 1000; //计算所需的计数值
tmp = 65536 - tmp; //计算定时器重载值
tmp = tmp + 18; //补偿中断响应延时造成的误差
T1RH = (unsigned char)(tmp>>8); //定时器重载值拆分为高低字节
T1RL = (unsigned char)tmp;
TMOD &= 0x0F; //清零T0的控制位
TMOD |= 0x10; //配置T0为模式1
TH1= T1RH; //加载T0重载值
TL1 = T1RL;
ET1 = 1; //使能T0中断
TR1 = 1; //启动T0
}
/* T0中断服务函数 */
void InterruptTimer1() interrupt 3
{
static unsigned char ti5=0;
TH1 = T1RH; //重新加载重载值
TL1 = T1RL;
ti5++; //计数标志自加1
if(ti5==20) //判断是否到1s
{
ti5=0; //将静态变量清0
// FlashLed=~FlashLed; //LED位求反
}
}
/********************* 主函数 *************************/
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键按下标志
}
}
}
//*******************************************************************
//*********************** 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
/******************** 红外采样时间宏定义, 用户不要随意修改 *******************/
#if ((D_TIMER0 <= 250) && (D_TIMER0 >= 60))
#define D_IR_sample D_TIMER0 //定义采样时间,在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) //判断数据正反码
{
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; //数据有效
}
}
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初始化函数 ******************************/
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中断函数************************/
void timer0 (void) interrupt 1
{
IR_RX_HT6121();
}
|