/*说明:使用的是HS0038红外接收头 */
/*功能:实现红外接收,接收到的键值码于数码管显示 */
/*思路:检测外部中断下降沿的时间,来翻译红外信号 */
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define DataProt P0
sbit IR = P3^2;
sbit du = P2^6;
sbit we = P2^7;
uchar IRtime; //存储低电平到下一个低电平持续的时间
bit IR_ok; //标志着已经存好了全部IRtime
bit IR_receive_ok; //标志着接收好了键值码
uchar IRcord[4]; //存储键值码
uchar IRdata[33]; //存储数据码和用户码
uchar code leddata[]={
//0 1 2 3 4 5 6 7 8
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F,
//9 A B C D E F - . 关显示
0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71, 0x40, 0x80, 0x00
};
void IR_init()
{
TMOD = 0x02; //8位可自动装载定时器
TH0 = 0x00;
TL0 = 0x00;
EA = 1;
ET0 = 1;
TR0 = 1;
/* EX0 = 1;
IT0 = 1; //下降沿触发式中断 */
IT0 = 1;
EX0 = 1;
}
void IRcordpro() //【键值码获取】将IRdata[k]里的时间数据 翻译为 用户码和用户码
{
uchar i,j,k,dat,value;
k = 1; //IRdata[0]是起始位。所以k = 1:33 共32个数
for(i = 0;i < 4;i++)
{
for(j = 0 ;j < 8;j++)
{
dat = IRdata[k];
value >>= 1;
if(dat > 5)
value |= 0x80; //先接收低位
k++;
}
IRcord[i] = value; //将用户码和数据码保存于数组中去
value = 0; //置0,以便下次用
}
IR_receive_ok = 1; //表示已经把键值码取完了
}
void IRwork()
{
switch(IRcord[2])
{
case 0x45: DataProt = leddata[10]; break;
case 0x46: DataProt = leddata[11]; break;
case 0x47: DataProt = leddata[12]; break;
case 0x44: DataProt = leddata[13]; break;
case 0x40: DataProt = leddata[14]; break;
case 0x43: DataProt = leddata[15]; break;
case 0x07: DataProt = leddata[16]; break;
case 0x15: DataProt = leddata[17]; break;
case 0x09: DataProt = 0x70; break;
case 0x16: DataProt = leddata[0]; break;
case 0x19: DataProt = 0x49; break;
case 0x0d: DataProt = 0x38; break;
case 0x0c: DataProt = leddata[1]; break;
case 0x18: DataProt = leddata[2]; break;
case 0x5e: DataProt = leddata[3]; break;
case 0x08: DataProt = leddata[4]; break;
case 0x1c: DataProt = leddata[5]; break;
case 0x5a: DataProt = leddata[6]; break;
case 0x42: DataProt = leddata[7]; break;
case 0x52: DataProt = leddata[8]; break;
case 0x4a: DataProt = leddata[9]; break;
default: break;
}
IR_receive_ok = 0; //表示已经把键值码转换完了
}
void main()
{
IR_init();
DataProt = 0xff;
we = 1;
DataProt = 0xfe;
we = 0;
du = 1;
while(1)
{
/* if(IR_ok)
{
IRcordpro();
IR_ok = 0;
if(IR_receive_ok)
{
IRwork();
}
} */
if(IR_ok)
{
IRcordpro();
IR_ok = 0;
}
if(IR_receive_ok)
{
IRwork();
}
}
}
void timer0()interrupt 1
{
IRtime++; //277us加一次
}
void int0()interrupt 0
{
static uchar i = 0;
// static bit start_flag = 0;
static start_flag = 0;
if(start_flag)
{
if((IRtime > 47) && (IRtime < 50)) //如果IRtime处于(13053us:13053us),表示接收到起始信号
i = 0;
IRdata[i] = IRtime;
i++;
if(i == 33) //已经接收完了
{
IR_ok = 1;
i = 0;
}
}
else
{
start_flag = 1;
IRtime = 0;
}
}
|