#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
int hightime, lowtime;
uchar m,inok,h,i,j;
uchar table[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
uchar mun[8];
uchar we[4];
sbit IRIN=P3^3;
sbit wlen=P2^7;
sbit wlrs=P2^5;
sbit RW=P2^6;
/*......................................延时程序,延时1ms................*/
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*......................................1602命令函数.....................*/
void write_com(uchar com)
{
wlrs=0; //写命令0/写数据1
RW=0; //读0/写1
P0=com; //给1602送命令
delay(5);
wlen=1;
delay(5);
wlen=0;
}
/*......................................1602数据函数.....................*/
void write_date(uchar date)
{
wlrs=1;
RW=0;
P0=date;
delay(5);
wlen=1;
delay(5);
wlen=0;
}
/*......................................1602初始化........................*/
void inint()
{
wlen=0;
write_com(0x38); //显示模式设置
write_com(0x0f); //光标显示设置
write_com(0x06); //光标闪动设置
write_com(0x80); //数据清零
}
/*......................................1602解码显示......................*/
void Function()
{
// uchar h,g;
mun[0]=we[0]/16;
mun[1]=we[0]%16;
mun[2]=we[1]/16;
mun[3]=we[1]%16;
mun[4]=we[2]/16;
mun[5]=we[2]%16;
mun[6]=we[3]/16;
mun[7]=we[3]%16;
for(h=0;h<8;h++)
{
// m=mun(h);
write_date(table[mun[h]]);
delay(5);
}
}
/*....................................红外解码............................*/
bit my(void)
{
uchar temp;
// int i,j;
for(i=0;i<4;i++) //4组数据:高用户码、低用户码、操作码、操作反码
{
temp=0;
for(j=0;j<8;j++) //每组码有八位数据
{
temp=temp>>1; //数据的按位左移
TH1=0;
TL1=0;
TR1=1;
while(IRIN==0);
TR1=0;
lowtime=TH1*256+TL1; //接收到低电平的时间
TH1=0;
TL1=0;
TR1=1;
while(IRIN==1);
TR1=0;
hightime=TH1*256+TL1; //接收到高电平的时间
if((lowtime<370)||(lowtime>640)) //是不是在0.56us左右,是不是由此遥控器发出的
return 0; //不是返回0
if((hightime<640)&&(hightime>370)) //高电平在0.56左右,这位是0
temp=temp&0x7f; //该位置0
if((hightime<1800)&&(hightime>1300)) //高电平在1.685左右,这位是1
temp=temp|0x80; //置1
}
we[i]=temp; //求出的值给四个数组
// if(we[2]=~we[3])
return 1; //解码完成,返回1
}
}
void main()
{
// m=0;
inint();
IE=0x84;
TCON=0x04;
TMOD=0x10;
TR1=1;
IRIN=1;
while(1);
/* {
if(inok)
P0=mun[m];
if(m>8)
m=0;
}*/
}
/*......................................外部中断1..............*/
void IR_IN() interrupt 2 //当遥控器有键按下,下降沿触发进入中断
{
EX1=0; //关外部中断1,
TH1=0; //定时器设初值
TL1=0;
TR1=1;
while(IRIN==0); //如果还是低电平,在此循环
TR1=0; //低电平结束,关定时器
hightime=TH1*256+TL1; //算低电平持续的时间
TH1=0;
TL1=0;
TR1=1;
while(IRIN==1);
lowtime=TH1*256+TL1;
if((hightime>7800)&&(hightime<8800)&&(lowtime>3600)&&(lowtime<4700)) //是不是此遥控器的引导码
{
if(my()==1) //解码是否完成
{
write_com(0x80);
Function();
}
}
IE=0x84; //为下次解码开中断
} |