/*从机2的接收程序*/
#include <reg51.h>
//数据定义格式为7E +从机地址+主机地址+命令+数据+校验(这一次作实验校验就不做了)+7E
code unsigned char a[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71};
code unsigned char b[]={0xfe,0xfd}; //,,,,,共阳数码管
unsigned char led[2]={0};
/*定义一个结构提*/
struct DatePacket
{
unsigned char MinorAddr; //从机地址
unsigned char MainAddr; //主机地址
unsigned char Comand;//51表示后面跟的为数据、52表示后面跟的是命令 AA为 (OK)A5为(Error)
//要求在数据中决不允许出现 51 52 AA A5 7E(为标志)这几个数据,刚好
//我们要传的数据中只需要低四位即可将高4位全部清0,决不会出现上面列出的数据
unsigned char KeyDate; //此为按键数据 只需要低四位 高四位清0
} RevDate;
unsigned char flag=2; //0--表示成功 1--表示失败,2--表示没有收到数据或数据没有收完
/*延时子函数*/
void delay(unsigned char delaytime)
{
unsigned char jj,jjj;
for(jj=0;jj<delaytime;jj++)
{
for(jjj=0;jjj<200;jjj++);
}
}
/*显示函数*/
void disp(void) //,,,,,,显示函数最好不要有循环
{
static unsigned char k;
led[0]=RevDate.KeyDate/10;
led[1]=RevDate.KeyDate%10;
P0=0;//你没有消影
P0=a[led[k]];
P2=b[k];
if(k++>=2) k=0;
}
/*初始化函数*/
void EsInit(void)
{
ES=1;
SCON=0X50;
TMOD=0X20;
TH1=TL1=-3;
TR1=1;
EA = 1;
}
/*中断接收*/
void RevInterruptInt1(void) interrupt 4
{
static unsigned char sys;//定义一个静态变量
unsigned char ReceiveData;
if( RI == 1 )
{
RI=0;
TI=0;
ReceiveData=SBUF;
switch(sys)
{
case 0: if(ReceiveData!=0x7E) { sys=0; flag=2;}
else sys=1; //先收报头
break;
case 1:
if(ReceiveData==0x7E) { sys=0; flag=2;}
if(ReceiveData!=0x62) { sys=0; flag=2;}
else { sys=2; RevDate.MinorAddr=ReceiveData;} //再收从机地址
break;
case 2:
if(ReceiveData!=0x60) { sys=0;flag=2;}
else { sys=3; RevDate.MainAddr=ReceiveData;} //主机地址
break;
case 3:
sys=4;
RevDate.Comand=ReceiveData; //命令
break;
case 4:
sys=5;
RevDate.KeyDate=ReceiveData; // 按键数据
P1=RevDate.KeyDate; //调试语句
break;
case 5:
if(ReceiveData!=0x7b) flag=1; //帧完
else flag=0; //
sys=0; //,,,,,,,,,这条指令搞忘了
break;
}
}
else TI=0;
}
/*主函数*/
void main(void)
{
EsInit();
RevDate.KeyDate=0;
while(1)
{
disp();
delay(10);
}
}