具体目的及原理
目的:使用一个51单片机作为主机通过面包板链接达到同时控制3个从机的目的。 原理:主机按下任意一个按键通过串口传输协议给所有的从机发送一个数据当做从机是否执行相应指令的开关,每个从机收到对应的指令后执行相对应的操作就行。 注意事项: 面包版的链接须在同一行内进行,因为面包版的同一行是互相连通的,若分开链接则从机不会收到信号的传输 接线时注意是会三合一杜邦线的可以主机的TX接口接到从机的RX接口,不会三合一杜邦线的可以通过面包板来达到同样的效果。
实现代码
主机发送代码:
#include<reg52.h>
sbit k1 = P3^4;
sbit k2 = P3^5;
sbit k3 = P3^6;
void delay(unsigned int xms)
{
unsigned i,j;
for(i=xms;i>0;i--)
for(j=112;j>0;j--);
}
void Send (unsigned char date)
{
SBUF = date;
while (!TI);
TI = 0;
}
void main (void)
{
TMOD = 0x20;
SCON = 0x40;
PCON = 0x00;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1;
ES = 1;
EA = 1;
while(1)
{
if(k1==0)
{
delay(20);
if(k1==0)
{
SBUF=0xA1;
}
while(!k1);
}
if(k2==0)
{
delay(20);
if(k2==0)
{
SBUF=0xA2;
}
while(!k2);
}
if(k3==0)
{
delay(20);
if(k3==0)
{
SBUF=0xA3;
}
while(!k3);
}
}
}
从机流水灯代码:
#include<reg52.h>
#define uint unsigned int
#define on 0
#define off 1
unsigned char date;
sbit led1=P1^0;
sbit led2=P1^1;
sbit led3=P1^2;
sbit led4=P1^3;
sbit led5=P1^4;
sbit led6=P1^5;
sbit led7=P1^6;
sbit led8=P1^7;
void delay(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=112;j>0;j--);
}
void ConfigUART(unsigned int baud);
void main()
{
ConfigUART(9600);
while(1)
{
while(!RI);
RI = 0;
date=SBUF;
if(date==0xA1)
{
while(1)
{
led1=on;
delay(200);
led1=off;
led2=on;
delay(200);
led2=off;
led3=on;
delay(200);
led3=off;
led4=on;
delay(200);
led4=off;
led5=on;
delay(200);
led5=off;
led6=on;
delay(200);
led6=off;
led7=on;
delay(200);
led7=off;
led8=on;
delay(200);
led8=off;
}
}
}
}
void ConfigUART(unsigned int baud)
{
SCON = 0x50;
TMOD &= 0X0F;
TMOD |= 0x20;
TH1 = 256 - (11059200/12/32)/baud;
TL1 = TH1;
ET1 = 0;
TR1 = 1;
}
从机LED流水代码:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define duan P0
unsigned char date;
sbit wei1 =P2^4;
sbit wei2 =P2^5;
sbit wei3 =P2^6;
sbit wei4 =P2^7;
uchar code sz[13]={0x31,0x09,0x0f,0x01,0x07,0x0b,0x0d,0x0e,0x08,0x38,0x19,0x29,0x39};
void delay (unsigned int xms)
{
unsigned int i,j;
for (i=xms;i>0;i--)
for (j=112;j>0;j--);
}
#include<reg52.h>
void ConfigUART(unsigned int baud);
void main()
{
ConfigUART(9600);
while(1)
{
while(!RI);
RI = 0;
date=SBUF;
if(date==0xA2)
{
int m;
while(1)
{
for(m=1;m<100;m++)
{
duan=sz[0];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[2];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
for(m=1;m<100;m++)
{
duan=sz[12];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[3];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[2];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
for(m=1;m<100;m++)
{
duan=sz[12];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[3];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[2];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
for(m=1;m<100;m++)
{
duan=sz[12];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[4];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
for(m=1;m<100;m++)
{
duan=sz[12];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[5];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
for(m=1;m<100;m++)
{
duan=sz[12];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[6];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
for(m=1;m<100;m++)
{
duan=sz[12];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[7];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
for(m=1;m<100;m++)
{
duan=sz[12];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[8];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[2];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
for(m=1;m<100;m++)
{
duan=sz[12];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[8];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[2];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
for(m=1;m<100;m++)
{
duan=sz[9];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[2];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
for(m=1;m<100;m++)
{
duan=sz[10];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[2];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
for(m=1;m<100;m++)
{
duan=sz[11];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[2];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
for(m=1;m<100;m++)
{
duan=sz[0];
wei1 =1;
wei2 =0;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =1;
wei3 =0;
wei4 =0;
delay(2);
duan=sz[1];
wei1 =0;
wei2 =0;
wei3 =1;
wei4 =0;
delay(2);
duan=sz[2];
wei1 =0;
wei2 =0;
wei3 =0;
wei4 =1;
delay(2);
}
}
}
}
}
void ConfigUART(unsigned int baud)
{
SCON = 0x50;
TMOD &= 0X0F;
TMOD |= 0x20;
TH1 = 256 - (11059200/12/32)/baud;
TL1 = TH1;
ET1 = 0;
TR1 = 1;
}
从机蜂鸣器代码:
#include<reg52.h>
#include<intrins.h>
#define on 0
#define off 1
sbit fm = P2^3;
void delay(unsigned int xms)
{
unsigned int i,j;
for(i=xms;i>0;i--)
for(j=112;j>0;j--);
}
void ConfigUART(unsigned int baud);
void main()
{
ConfigUART(9600);
while(1)
{
if(date==0xA3){
while(1){
P1 = 0xfe;
delay(100);
P1 = _crol_ (P1,1);
fm = on;
delay(100);
fm = off;
}
}
}
}
void ConfigUART(unsigned int baud)
{
SCON = 0x50;
TMOD &= 0X0F;
TMOD |= 0x20;
TH1 = 256 - (11059200/12/32)/baud;
TL1 = TH1;
ET1 = 0;
TR1 = 1;
}
图片展示
|