|
用以下程序进行proteus仿真,只有从机1响应而且开关不起作用,从机1、2除了地址外相同,请诸位看看是什么原因,怎么改!
#include<reg51.h>
#define unit unsigned int
#define uchar unsigned char
#define addr1 0x31
#define addr2 0x32
sbit P01=P0^1;
sbit P02=P0^2;
sbit P03=P0^3;
void delay(unit z)
{
unit x,y;
for(x=z;x<0;x--);
for(y=110;y>0;y--);
}
void Uart_Init(void)
{
TMOD=0x20;
TH1=250;
TH0=250;
TR1=1;
PCON=0x80;
SCON=0xd0;
TI=1;
RI=1;
}
uchar Receive(void)
{
uchar Data;
while(!RI);
Data=SBUF;
RI=0;
return Data;
}
void send_addr(uchar addr)
{
uchar tem;
while(addr!=tem)
{
while(!TI);
TI=0;
TB8=1;
SBUF=addr;
P03=0;
tem=Receive();
P03=1;
}
}
void send_data(uchar ddata)
{
while(!TI);
TI=0;
TB8=0;
SBUF=ddata;
while(!TI);
}
void main()
{
Uart_Init();
P03=0;
while(1)
{
if(P01==0)
{
delay(5);
if(P01==0)
{
P03=1;
delay(1);
send_addr(addr1);
send_data(0x33);
delay(1);
P03=0;
}
}
if(P02==0)
{
delay(5);
if(P02==0)
{
P03=1;
delay(1);
send_addr(addr2);
send_data(0x34);
delay(1);
P03=0;
}
}
}
}
|
|