c:
.zip
#include
//采用11.0592晶振 发送
#include "send.h"
sbit signalBit=P1^0;
sbit p20=P2^0;
sbit p21=P2^1;
sbit p22=P2^2;
sbit p23=P2^3;
sbit p24=P2^4;
sbit p25=P2^5;
sbit p26=P2^6;
sbit p27=P2^7;
void SendIRdata(uchar dat)
{
uchar i,j,dat0=~dat;
/********* 发送9ms的高电平 ************/
TH1=0XDF;
TL1=0XE3;
TR1=1;
signalBit=1;
while(!TF1); //TF1=1的同时cpu转向中断程序并将TF1置0
/********* 发送4.5ms的低电平 *************/
TH1=0xef;
TL1=0xf0;
signalBit=0;
while(!TF1);
/*********发送数据时低位在前***********/
for(j=0;j<2;j++)
{
for(i=0;i<8;i++)
{ TH1=0XFE; //0.565ms的高电平
TL1=0X70;
signalBit=1;
while(!TF1);
if(dat&0x01)
{
TH1=0XFA; //1时间1.685ms
TL1=0X30;
}
else
{
TH1=0XFE; //0时间 0.56ms
TL1=0x50;
}
signalBit=0;
while(!TF1);
dat=dat>>1;
}
dat=dat0;
}
TR1=0; //发送截止码
signalBit=1;
delay(12);
signalBit=0;
}
void keyscan()
{
unsigned l,h,key;
P2=0xf0;
l=P2;
l=l&0xf0;
if(l!=0xf0)
{
delay(200);
if(l!=0xf0)
{
l=P2&0xf0;
l=l|0x0f;
P2=l;
h=P2;
h=h&0x0f;
l=l&0xf0;
key=l+h;
}
switch(key)
{
case 0xee:SendIRdata(0); break;
case 0xde:SendIRdata(1); break;
case 0xbe:SendIRdata(2); break;
case 0x7e:SendIRdata(3); break;
case 0xed:SendIRdata(4); break;
case 0xdd:SendIRdata(5); break;
case 0xbd:SendIRdata(6); break;
case 0x7d:SendIRdata(7); break;
case 0xeb:SendIRdata(8); break;
case 0xdb:SendIRdata(9); break;
case 0xbb:SendIRdata(10); break;
case 0x7b:SendIRdata(11); break;
case 0xe7:SendIRdata(12); break;
case 0xd7:SendIRdata(13); break;
case 0xb7:SendIRdata(14); break;
case 0x77:SendIRdata(15); break;
}
}
//delay(6000);
}
void main(void)
{
signalBit=0;
TMOD=0x10;//设定定时器1工作方式为1
ET1 =1;
TR1= 0;
EA=1;
while(1)
{ /*delay(60000); delay(60000);delay(60000);
SendIRdata(15);
delay(60000);delay(60000); delay(60000);
SendIRdata(14);
delay(60000); delay(60000); delay(60000);
SendIRdata(13);
delay(60000); delay(60000); delay(60000);
SendIRdata(12);
delay(60000); delay(60000);delay(60000);
SendIRdata(11);
if(p20==0) {delay(200);SendIRdata(0);}
if(p21==0) {delay(200);SendIRdata(1);}
if(p22==0) {delay(200);SendIRdata(2);}
if(p23==0) {delay(200);SendIRdata(3);}
if(p24==0) {delay(200);SendIRdata(4);}
if(p25==0) {delay(200);SendIRdata(5);}
if(p26==0) {delay(200);SendIRdata(6);}
if(p27==0) {delay(200);SendIRdata(7);} */
keyscan();
}
}
void isr_T1(void) interrupt 3
{;}
#include //采用11.0592晶振 接收
#include
#define uchar unsigned char
#define uint unsigned int
sbit IR=P3^2;
sbit rs=P2^4;
sbit rw=P2^5;
sbit e=P2^6;
uchar zm1[]=" RECIVE ";
uchar zm2[]="data: ";
uchar a[2];
void delay(uint t)
{
while(t--);
}
void write_com(uchar com)
{ e=0;
rs=0;
rw=0;
P0=com;
delay(50);
e=1;
delay(50);
e=0;
delay(50);
}
void write_data(uchar dat)
{ rs=1;
e=0;
rw=0;
P0=dat;
delay(50);
e=1;
delay(50);
e=0;
}
void start()
{ delay(200);
write_com(0x38);
delay(200);
write_com(0x38);
delay(200);
write_com(0x38);
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x06);
write_com(0x0c);
}
void lcd(char *p)
{ uchar i;
write_com(0x80);
for(i=0;i<16;i++)
{ write_data(zm1[i]); }
write_com(0x80+0x40);
for(i=0;i<16;i++)
{ write_data(p[i]); }
}
void disp()
{ char i;
zm2[5]=a[0]/16;
zm2[6]=a[0]%16;
for(i=5;i<7;i++)
{
if(zm2[i]>0x09) zm2[i]+=0x37;
else zm2[i]+=0x30;
}
lcd(zm2);
}
/************************************************************
解码正确,返回1,否则返回0
*************************************************************/
bit DeCode(void)
{ uint hTime,lTime;
uchar i,j;
uchar temp=0; //储存解码出的数据
for(i=0;i<2;i++)
{
for(j=0;j<8;j++)
{
temp=temp>>1;
TH0=0;
TL0=0;
TR0=1;
while(IR==0) ;//320us
TR0=0;
hTime=TH0*256+TL0;
TH0=0;
TL0=0;
TR0=1;
while(IR==1);
TR0=0;
lTime=TH0*256+TL0;
if((hTime<300)||(hTime>600))
return 0;
if((lTime>300)&&(lTime<600)) //如果高电平时间在560微秒左右,即计数560/1.085=516次
{
temp=temp&0x7f;
}
if((lTime>1300)&&(lTime<1850)) //如果高电平时间在1680微秒左右,即计数1680/1.085=1548次
{
temp=temp|0x80;
}
}
a[i]=temp;
temp=0;
}
if(a[0]==~a[1])
return 1; //解码正确,返回1
else return 0;
}
//-----------------------------------------------
void main()
{
EX0=1; //定时器,外中断0允许
ET0=1;
IT0=1; //外中断的下降沿触发
TMOD=0x01; //定时器模式1
TR0=0; //定时器关闭
EA=1;
start();
lcd(zm2);
while(1);
}
void Int0(void) interrupt 0
{ uint hTime,lTime;
EX0=0;
TH0=0;
TL0=0;
TR0=1;
while(IR==0); //9ms启始码部分
TR0=0;
hTime=TH0*256+TL0;
TH0=0;
TL0=0;
TR0=1;
while(IR==1); //4.5ms起始码部分
TR0=0;
lTime=TH0*256+TL0;
if((hTime>7500&&hTime<9000)&&(lTime>3500&&lTime<4700))
{
DeCode();// 执行遥控解码功能
disp();
}
EX0=1;
}
void isr_T1(void) interrupt 1
{
;
}
阅读(1723) | 评论(0) | 转发(1) |