求助高手帮忙改一下程序 本人太菜了 改不出来 (功能要求:当K1按下,单片机U1的程序通过编码发送到U2,当U2接收到编码 LED亮,K1断开LED灭。)现在是K1按下LED亮 松开K1按键LED不灭灯 。下面程序是齐全的不用下载可以的。
红外发射接收.rar
(39.21 KB, 下载次数: 6)
单片机源程序如下:
/*红外发射编码程序*/
#include<reg52.h>
sbit ir=P1^3;
sbit k1=P2^1;
unsigned int count, set_count;
bit irflag,keyflag;
unsigned char irsys[]={0x00,0xff};
unsigned char irdata,ircode;
void delay(unsigned int a)
{
unsigned char i;
while(--a!=0)
for(i=300;i>0;i--);
}
void keyscan()
{
if(k1==0)
{
delay(10);
if(k1==0)
{
keyflag=1;
irdata=0x01;
}
}
}
void ir_sendbyte() //红外发送一个字节数据
{
unsigned char i;
for(i=0;i<8;i++) //发送8位数据
{
set_count=43; //发送编码中的0.56ms高电平
irflag=1;
count=0;
TR0=1;
while(count<set_count);
TR0=0;
if(ircode&0x01) set_count=130; //判断红外编码最低位,若为1则1.69ms的低电平
else set_count=43; //为0则0.565ms的低电平
irflag=0;
count=0;
TR0=1;
while(count<set_count);
TR0=0;
ircode=ircode>>1;
}
}
void ir_send()
{
set_count=346; //发送编码中的引导码(4.5ms高电平+4.5ms低电平)
irflag=1;
count=0;
TR0=1;
while(count<set_count);
TR0=0;
set_count=346; //发送编码中的4.5ms低电平
irflag=0;
count=0;
TR0=1;
while(count<set_count);
TR0=0;
ircode=irsys[0];
ir_sendbyte();
ircode=irsys[1];
ir_sendbyte();
ircode=irdata; //发送8位数据码
ir_sendbyte();
ircode=~irdata; //发送8位数据反码
ir_sendbyte();
set_count=43; //发送编码中的0.56ms高电平
irflag=1;
count=0;
TR0=1;
while(count<set_count);
TR0=0;
irflag=0;
delay(23); //延时23ms(编码中的23ms低电平)
set_count=346; //发送编码中的引导码(4.5ms高电平+4.5ms低电平)
irflag=1;
count=0;
TR0=1;
while(count<set_count);
TR0=0;
set_count=346;
irflag=0;
count=0;
TR0=1;
while(count<set_count);
TR0=0;
set_count=43;
irflag=1;
count=0;
TR0=1;
while(count<set_count);
TR0=0;
irflag=0;
delay(23);
}
void timer0_init()
{
EA=1;
TMOD=0x02; //定时0 8位自动重装模式
ET0=1;
TH0=0xe6; //定时13us,38K红外矩形波,晶振24M
TL0=0xe6;
}
void main()
{
timer0_init();
count=0;
ir=0;
irflag=0;
while(1)
{
keyscan();
if(keyflag)
{
delay(10);
ir_send();
delay(500);
keyflag=0;
delay(100);
}
}
}
void timer0() interrupt 1
{
count++;
if(irflag==1)
ir=~ir; //有发射标志,则发射38khz的矩形波
else
ir=0;
}
/*红外编码接收程序*/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit led1=P1^0;
uchar irtime;
uchar startflag;
uchar irdata[33];
uchar bitnum;
uchar irreceok;
uchar ircode[4];
uchar irprosok;
void display();
void display1();
void timer0init()
{
TMOD=0x02;
TH0=0x00;
TL0=0x00;
ET0=1;
EA=1;
TR0=1;
}
void int0init()
{
IT0=1;
EX0=1;
EA=1;
}
void irpros()
{
uchar k,i,j;
uchar value;
k=1;
for(j=0;j<4;j++)
{
for(i=0;i<8;i++)
{
value=value>>1;
if(irdata[k]>6)
{
value=value | 0x80;
}
k++;
if(k>33)
k=1;
}
ircode[j]=value;
}
}
void main()
{
led1=0;
timer0init();
int0init();
while(1)
{
if(irreceok)
{
irpros();
}
display();
}
}
void display()
{
switch(ircode[2])
{
case 0x02:led1=1;
break;
}
}
void timer0 () interrupt 1
{
irtime++;
}
void int0 () interrupt 0
{
if(startflag)
{
if(irtime>32) //检测引导码
{
bitnum=0;
}
irdata[bitnum]=irtime;
irtime=0;
bitnum++;
if(bitnum==33)
{
bitnum=0;
irreceok=1;
startflag=0;
}
}
else
{
startflag=1;
irtime=0;
}
}
|