我现在发送端发送地址,接收端能收到,但是收到地址后就无法执行后面的操作了,麻烦给位帮我看看(程序是从网上找的例程小改了下,只改了几个端口定义)
发送端:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit LED1 = P3^4;
sbit LED2 = P3^5;
sbit MCU1 = P2^2;
sbit MCU2 = P2^3;
sbit rs485dir = P3^2;
uchar code tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x77,0x7c};
uchar keyscan(void);
void delayms(uint t);
void init_serial(void);
void SBUFSend(uchar key);
void senddat(uchar addr,uchar dat);
void display(uchar addr,uchar dat);
/*************************************************************************/
/******************************按键扫描函数*******************************/
/*************************************************************************/
uchar keyscan(void)
{
uchar temp,key;
P0 = 0xfe;
temp = P0;
temp = temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
if(temp!=0xf0)
{
temp = P0;
switch(temp)
{
case 0xee: key = 0; break;
case 0xde: key = 1; break;
case 0xbe: key = 3; break;
case 0x7e: key = 4; break;
}
while(temp!=0xf0)
{
temp = P0;
temp = temp&0xf0;
}
}
}
P0 = 0xfd;
temp = P0;
temp = temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
if(temp!=0xf0)
{
temp = P0;
switch(temp)
{
case 0xed: key = 4; break;
case 0xdd: key = 5; break;
case 0xbd: key = 6; break;
case 0x7d: key = 7; break;
}
while(temp!=0xf0)
{
temp = P0;
temp = temp&0xf0;
}
}
}
P0 = 0xfb;
temp = P0;
temp = temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
if(temp!=0xf0)
{
temp = P0;
switch(temp)
{
case 0xeb: key = 8; break;
case 0xdb: key = 9; break;
case 0xbb: key = 10; break;
case 0x7b: key = 11; break;
}
while(temp!=0xf0)
{
temp = P0;
temp = temp&0xf0;
}
}
}
P0 = 0xf7;
temp = P0;
temp = temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
if(temp!=0xf0)
{
temp = P0;
switch(temp)
{
case 0xe7: key = 12; break;
case 0xd7: key = 13; break;
case 0xb7: key = 13; break;
case 0x77: key = 15; break;
}
while(temp!=0xf0)
{
temp = P0;
temp = temp&0xf0;
}
}
}
return key;
}
/*************************************************************************/
/******************************毫秒延时函数*******************************/
/*************************************************************************/
void delayms(uint t)
{
uchar i,j;
for(i=0;i<t;i++)
for(j=0;j<110;j++);
}
/*************************************************************************/
/********************定时器串口寄存器初始化函数***************************/
/*************************************************************************/
void init_serial(void)
{
TMOD = 0x20;
TH1 = 0xfa;
TL1 = 0xfa;
PCON = 0x80;
SCON = 0x50;
TR1 = 1;
ES = 1;
EA = 1;
rs485dir = 1;
}
/*************************************************************************/
/******************************信息发送函数*******************************/
/*************************************************************************/
void SBUFSend(uchar key)
{
SBUF = key;
while(TI ==0);
TI = 0;
}
/*************************************************************************/
/****************************发送地址数据函数*****************************/
/*************************************************************************/
void senddat(uchar addr,uchar dat)
{
TB8 = 1;
SBUFSend(addr);
delayms(1);
TB8 = 0;
SBUFSend(dat);
delayms(1);
}
/*************************************************************************/
/*****************************数码管显示函数******************************/
/*************************************************************************/
void display(uchar addr,uchar dat)
{
P1 = tab[addr]; /*显示发送的地址*/
LED1 = 0;
LED2 = 1;
delayms(1000);
P1 = 0x00;
P1 = tab[dat]; /*显示发送的数据*/
LED1 = 1;
LED2 = 0;
delayms(1000);
P1 = 0x00;
}
void main()
{
char addr,dat;
init_serial();
dat = keyscan();
while(1)
{
if(MCU1==0)
{
addr = 1;
dat = keyscan();
senddat(1,dat);
display(1,dat);
}
if(MCU2==0)
{
addr = 2;
dat = keyscan();
senddat(2,dat);
senddat(2,dat);
}
}
}
void serial_init() interrupt 4
{
if(RI==1)
RI = 0;
if(TI==1)
TI = 0;
}
接收端:
#include <reg52.h>
#define uchar unsigned char
sbit LED1 = P1^0;
sbit LED2 = P1^1;
uchar DispCode[16] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void SerialInit()
{
SCON = 0xf0;
PCON = 0x00;
TMOD = 0x20;
TH1 = 0xfd;
TH1 = 0xfd;
TR1 = 1;
ES = 1;
EA = 1;
}
void SBUFSend(uchar Ch)
{
SBUF = Ch;
while(TI==0);
TI = 0;
}
void main()
{
P2 = 0xff;
SerialInit();
while(1);
}
void SerialServer() interrupt 4
{
uchar Ch;
if(RI == 1)
{
uchar i;
RI = 0;
Ch = SBUF;
if(RB8 == 1)
{
if(Ch==1)
{
SM2 = 0;
SBUFSend(1);
LED1 = 0;
LED2 = 1;
for(i=500;i>0;i--)
P2 = DispCode[Ch];
}
else
{
SM2 = 1;
}
}
if(RB8 == 0)
{
P2 = 0xff;
LED1 = 1;
LED2 = 0;
for(i=500;i>0;i--)
P2 = DispCode[Ch];
SM2 = 1;
}
}
}
|