发送端程序:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit led=P2^3;
sbit led1=P2^4;
sbit DE=P3^6;
void delayms(uchar ms)
{
uchar i;
while(ms--)
for(i=0;i<110;i++);
}
void init()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
SCON=0x50;
TI=0;
EA=1;
ES=1;
TR1=1;
DE=1;
}
void send(uchar dat)
{
DE=1;
SBUF=dat;
while(!TI);
TI=0;
DE=0;
led = ~led;
}
void main()
{
init();
while(1)
{
send(0xff);
delayms(500);
}
}
接收端程序:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DE=P3^5;
sbit led=P2^0;
sbit led1=P2^1;
sbit buzzer=P2^2;
uchar a;
void delayms(uchar ms)
{
uchar i;
while(ms--)
for(i=0;i<110;i++);
}
void init()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
SCON=0x50;
TI=0;
EA=1;
ES=1;
TR1=1;
DE=0;
}
void main()
{
init();
while(1)
{
if(a==0xff)
{
buzzer=0;
led1=0;
delayms(200);
led1=1;
delayms(200);
}
else
{
buzzer=1;
}
delayms(200);
}
}
void serier() interrupt 4
{
if(RI)
{
a=SBUF;
RI=0;
}
}
|