程序:
1 接收程序
2 发送程序
1 接收程序
#include<REG51.h>
void init(void)
{
TMOD=0x20;
TH1=0xE8;
TL1=0xE8;
PCON=0x00;
TR1=1;
SCON=0x50;
}
void checkout(unsigned int para)
{ unsigned int paracheck=para;
redo: if(SBUF!=paracheck)
SBUF=0xFF;
while(TI==0);
TI=0;
while(RI==0);
RI=0;
if(SBUF!=paracheck)
goto redo;
else SBUF=0xBB;
while(TI==0);
TI=0;
}
void ledflash(unsigned char flashnum)
{
unsigned char i,k;
for(i=flashnum;i>0;i--)
for(k=200;k>0;k--)
{
;
P0=0x00;
P0=0xFF;
}
}
void get()
{ unsigned int getnum;
while(RI==0);
RI=0;
while((getnum=SBUF)!=0xCC)
{
if(getnum%4==0)
{ledflash(getnum);
SBUF=0xBB;
goto check;}
else
SBUF=0xFF;
check: while(TI==0);
TI=0;
while(RI==0);
RI=0;
}
}
void ledon()
{
P0=0x00;
P0=0xFF;
}
void ledoff()
{
P0=0xFF;
P0=0x00;
}
void main(void)
{
while(1)
{
init();
checkout(0xAA);
ledon();
get();
checkout(0xCC);
ledoff();
}
}
void init(void)
{
TMOD=0x20;
TH1=0xE8;
TL1=0xE8;
PCON=0x00;
TR1=1;
SCON=0x50;
}
void checkout(unsigned int para)
{ unsigned int paracheck=para;
redo: if(SBUF!=paracheck)
SBUF=0xFF;
while(TI==0);
TI=0;
while(RI==0);
RI=0;
if(SBUF!=paracheck)
goto redo;
else SBUF=0xBB;
while(TI==0);
TI=0;
}
void ledflash(unsigned char flashnum)
{
unsigned char i,k;
for(i=flashnum;i>0;i--)
for(k=200;k>0;k--)
{
;
P0=0x00;
P0=0xFF;
}
}
void get()
{ unsigned int getnum;
while(RI==0);
RI=0;
while((getnum=SBUF)!=0xCC)
{
if(getnum%4==0)
{ledflash(getnum);
SBUF=0xBB;
goto check;}
else
SBUF=0xFF;
check: while(TI==0);
TI=0;
while(RI==0);
RI=0;
}
}
void ledon()
{
P0=0x00;
P0=0xFF;
}
void ledoff()
{
P0=0xFF;
P0=0x00;
}
void main(void)
{
while(1)
{
init();
checkout(0xAA);
ledon();
get();
checkout(0xCC);
ledoff();
}
}
2 发送程序
#include<REG51.h>
void init(void)
{
TMOD=0x20;
TH1=0xE8;
TL1=0xE8;
PCON=0x00;
TR1=1;
SCON=0x50;
}
void checkout(unsigned int para)
{ unsigned int paracheck;
paracheck=para;
redo: SBUF=paracheck;
while(TI==0);
TI=0;
while(RI==0);
RI=0;
if(SBUF!=0xBB)
goto redo;
}
void sent(void)
{unsigned char i;
checkout(0xAA);
for( i=0;i<20;i++)
{
if(i%4==0)
checkout(i);
}
checkout(0xCC);
}
void ledon(void)
{
P0=0x00;
P0=0xFF;
}
void ledoff(void)
{
P0=0x00;
}
void main()
{
while(1)
{
init();
ledon();
sent();
ledoff();
};
}
void init(void)
{
TMOD=0x20;
TH1=0xE8;
TL1=0xE8;
PCON=0x00;
TR1=1;
SCON=0x50;
}
void checkout(unsigned int para)
{ unsigned int paracheck;
paracheck=para;
redo: SBUF=paracheck;
while(TI==0);
TI=0;
while(RI==0);
RI=0;
if(SBUF!=0xBB)
goto redo;
}
void sent(void)
{unsigned char i;
checkout(0xAA);
for( i=0;i<20;i++)
{
if(i%4==0)
checkout(i);
}
checkout(0xCC);
}
void ledon(void)
{
P0=0x00;
P0=0xFF;
}
void ledoff(void)
{
P0=0x00;
}
void main()
{
while(1)
{
init();
ledon();
sent();
ledoff();
};
}
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1351/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
评论列表