标题:
单片机多机通信仿真图中的数码管不亮,是哪里出错了,求帮助
[打印本页]
作者:
病毒体
时间:
2019-3-12 20:56
标题:
单片机多机通信仿真图中的数码管不亮,是哪里出错了,求帮助
仿真图.png
(37.74 KB, 下载次数: 25)
下载附件
仿真图
2019-3-12 20:54 上传
单片机主机代码
#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;
uchar tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c};
uchar keyscan(void);
void delayms(uchar t);
void init_serial(void);
void SBUFSend(uchar key);
void senddat(uchar addr,uchar dat);
void display(uchar addr,uchar dat);
uchar keyscan()
{
uchar temp,key;
P1=0xfe;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xee:key=0;break;
case 0xde:key=1;break;
case 0xbe:key=2;break;
case 0x7e:key=3;break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
if(temp!=0xf0)
{
temp=P1;
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=P1;
temp=temp&0xf0;
}
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
if(temp!=0xf0)
{
temp=P1;
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=P1;
temp=temp&0xf0;
}
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7:key=12;break;
case 0xd7:key=13;break;
case 0xb7:key=14;break;
case 0x77:key=15;break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
}
}
return key;
}
void delayms(uchar 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;
}
void SUBFSend(uchar key)
{
SBUF=key;
while(TI==0);
TI=0;
}
void senddat(uchar addr,uchar dat)
{
TB8=1;
SBUFSend(addr);
TB8=0;
SBUFSend(dat);
}
void display(uchar addr,uchar dat)
{
P0=tab[addr];
LED1=0;
LED2=1;
delayms(1);
P0=0x00;
P0=tab[dat];
LED1=1;
LED2=0;
delayms(1);
P0=0X00;
}
void main()
{
char addr,dat;
init_serial();
dat=keyscan();
while(1){
if(MCU1==1)
{
addr=1;
dat=keyscan();
senddat(1,dat);
display(1,dat);
}
if(MCU2==0)
{
addr=2;
dat=keyscan();
senddat(2,dat);
display(2,dat);
}
}
}
void serial_int() interrupt 4
{
if(RI==1)
{
RI=0;
}
else TI=0;
}
从机1代码
#include <reg52.h>
#define uchar unsigned char
sbit LED1=P2^0;
sbit LED2=P2^1;
uchar DispCode[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//------------
void SerialInit()
{
SCON=0xf0; //方式3 允许接收,SM2=1
PCON=0x00; //串口波特率不加倍
TMOD=0x20; //定时器1,方式2
TH1=0xfd; //波特率为9600
TL1=0xfd;
TR1=1; //启动定时器
ES=1;
EA=1;
}
//------------
void SBUFSend(uchar Ch)
{
SBUF=Ch;
while(TI==0)
{
}
TI=0;
}
//------------
void main()
{
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--)
P0=DispCode[Ch];
}
else
{
SM2=1;
}
}
if(RB8==0)
{ P0=0x00;
LED1=1;
LED2=0;
for(i=500;i>0;i--)
P0=DispCode[Ch];
SM2=1;
}
}
}
从机2代码
#include<reg52.h>
#define uchar unsigned char
sbit LED1=P2^0;
sbit LED2=P2^1;
uchar DispCode[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//------------
void SerialInit()
{
SCON=0xf0; //方式3 允许接收,SM2=1
PCON=0x00; //串口波特率不加倍
TMOD=0x20; //定时器1,方式2
TH1=0xfd; //波特率为9600
TL1=0xfd;
TR1=1; //启动定时器
ES=1;
EA=1;
}
//------------
void SBUFSend(uchar Ch)
{
SBUF=Ch;
while(TI==0)
{
}
TI=0;
}
//------------
void main()
{
SerialInit();
while(1)
{
}
}
//------------
void SerialServer() interrupt 4
{
uchar Ch;
if(RI==1)
{ uchar i;
RI=0;
Ch=SBUF;
if(RB8==1)
{
if(Ch==2)
{
SM2=0;
SBUFSend(2);
LED1=0;
LED2=1;
for(i=500;i>0;i--)
P0=DispCode[Ch];
}
else
{
SM2=1;
}
}
if(RB8==0)
{ P0=0x00;
LED1=1;
LED2=0;
for(i=500;i>0;i--)
P0=DispCode[Ch];
SM2=1;
}
}
}
复制代码
作者:
wulin
时间:
2019-3-13 07:50
共阴数码管P0不接上拉电阻怎么亮?
作者:
病毒体
时间:
2019-3-13 10:24
wulin 发表于 2019-3-13 07:50
共阴数码管P0不接上拉电阻怎么亮?
谢谢,但是是接了上拉电阻,就是RP1,RP2,RP3
作者:
460783259
时间:
2019-3-13 17:00
preteus 仿真的效果 跟电脑本身的性能 也有关系, 延时过短 就是显示 不出来 你可以分步骤 验证!
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1