【51单片机】【protues仿真】基于51单片机主从串行通信系统

目录

一、主要功能

二、使用步骤

三、硬件资源

四、软件设计

五、实验现象

一、主要功能

1、LCD1602液晶显示

2、矩阵按键​,独立按键设置采集模式

3、主机显示从机采集的电压数据

4、从机采集电压数据传送到主机

5、可以设置采集显示的时间0.5s或1s

二、使用步骤

基于51单片机的主从串行通信系统通常采用主从式多机通信架构,主机通过串口控制多个从机,实现数据交互与设备协同。

三、硬件资源

1、51单片机核心模块

2、按键模块

3、数模转换模块

4、LCD1602显示模块

四、软件设计

#include<reg51.h>

#include <INTRINS.H>

#include"lcd.h"

#define GPIO_KEY P1

char KeyValue;

sbit key_1 = P2^0;

sbit key_2 = P2^1;

unsigned char receiveData=0;

unsigned int V_A = 0;

unsigned int V_B = 0;

unsigned char rec[6];

char r = 0;

int count = 0;

bit start = 0;

int time = 10; //默认1s时间

void delay10ms(unsigned char d) //误差 0us

{

unsigned char a, b,c;

for (c=d;c>0;c--)

{

for (b=38;b>0;b--)

{

for (a=130;a>0;a--);

}

}

}

void display(void)

{

if(start)

{

if(count<10)

{

LcdWriteCom(0x80); //清屏

LcdWriteData('A');

LcdWriteData('-');

LcdWriteData('0'+V_A/100);

LcdWriteData('.');

LcdWriteData('0'+(V_A%100)/10);

LcdWriteData('0'+V_A%10);

LcdWriteData('V');

LcdWriteCom(0xc0);

LcdWriteData('B');

LcdWriteData('-');

LcdWriteData('0'+V_B/100);

LcdWriteData('.');

LcdWriteData('0'+(V_B%100)/10);

LcdWriteData('0'+V_B%10);

LcdWriteData('V');

}

else

{

LcdWriteCom(0x80);

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteCom(0xc0);

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteData(' ');

LcdWriteData(' ');

}

LcdWriteCom(0x8b);

LcdWriteData('S');

LcdWriteData('T');

LcdWriteData('A');

LcdWriteData('R');

LcdWriteData('T');

}

else

{

LcdWriteCom(0x80);

LcdWriteData('A');

LcdWriteData('-');

LcdWriteData('0'+V_A/100);

LcdWriteData('.');

LcdWriteData('0'+(V_A%100)/10);

LcdWriteData('0'+V_A%10);

LcdWriteData('V');

LcdWriteCom(0xc0);

LcdWriteData('B');

LcdWriteData('-');

LcdWriteData('0'+V_B/100);

LcdWriteData('.');

LcdWriteData('0'+(V_B%100)/10);

LcdWriteData('0'+V_B%10);

LcdWriteData('V');

LcdWriteCom(0x8b);

LcdWriteData('S');

LcdWriteData('T');

LcdWriteData('O');

LcdWriteData('P');

LcdWriteData(' ');

}

}

void main(void)

{

LcdInit();

UsartConfiguration();

LcdWriteCom(0xcb);

LcdWriteData('1');

LcdWriteData('.');

LcdWriteData('0');

LcdWriteData('S');

while(1)

{

KeyValue = 99;

KeyDown();

if(KeyValue==14)

{

time = time - 5; //

}

if(KeyValue==15)

{

time = time + 5;

}

if(time<5)

time = 5;

else if(time>990)

time = 990;

LcdWriteCom(0xca);

LcdWriteData('0'+time/100);

LcdWriteData('0'+time%100/10);

LcdWriteData('.');

LcdWriteData('0'+time%10);

LcdWriteData('S');

if(start==0)

{

if(KeyValue==1)

senddata(1);

else if(KeyValue==2)

senddata(2);

}

else

{

if(count>time*2)

{

senddata(1);

delay10ms(10);

if(rec[0]==1)

{

V_A = rec[1]*2;

}

else if(rec[0]==2)

{

V_B = rec[1]*2;

}

senddata(2);

delay10ms(10);

if(rec[0]==1)

{

V_A = rec[1]*2;

}

else if(rec[0]==2)

{

V_B = rec[1]*2;

}

count = 0;

}

}

if(rec[0]==1)

{

V_A = rec[1]*2;

}

else if(rec[0]==2)

{

V_B = rec[1]*2;

}

if(key_1==0)

{

while(key_1==0);

start = 0;

//senddata(1);

}

if(key_2==0)

{

while(key_2==0);

start = 1;

}

display();

delay10ms(10);

}

}

void delay1s(void) interrupt 1

{

TH0 = (65536-45000)/256;

TL0 = (65536-45000)%256;

count++;

}

五、实验现象

演示视频:

相关推荐
无垠的广袤21 小时前
【工业树莓派 CM0 NANO 单板计算机】本地部署 EMQX
linux·python·嵌入式硬件·物联网·树莓派·emqx·工业物联网
序属秋秋秋1 天前
《Linux系统编程之进程环境》【环境变量】
linux·运维·服务器·c语言·c++·操作系统·系统编程
雲烟1 天前
嵌入式设备EMC安规检测参考
网络·单片机·嵌入式硬件
泽虞1 天前
《STM32单片机开发》p7
笔记·stm32·单片机·嵌入式硬件
Yue丶越1 天前
【C语言】数据在内存中的存储
c语言·开发语言·网络
田甲1 天前
【STM32】 数码管驱动
stm32·单片机·嵌入式硬件
up向上up1 天前
基于51单片机垃圾箱自动分类加料机快递物流分拣器系统设计
单片机·嵌入式硬件·51单片机
Yue丶越1 天前
【C语言】字符函数和字符串函数
c语言·开发语言·算法
纳祥科技1 天前
Switch快充方案,内置GaN,集成了多个独立芯片
单片机
桧***攮1 天前
C在物联网协议中的实现
物联网