当前位置:网站首页>Mode 1 two-way serial communication is adopted between machine a and machine B, and the specific requirements are as follows: (1) the K1 key of machine a can control the ledi of machine B to turn on a
Mode 1 two-way serial communication is adopted between machine a and machine B, and the specific requirements are as follows: (1) the K1 key of machine a can control the ledi of machine B to turn on a
2022-07-06 13:38:00 【axu_ nine hundred and ninety thousand seven hundred and seven】
( original )Proteus Virtual simulation . nail 、 Between machines B 1 Two way serial communication , The specific requirements are as follows :
(1) A machine k1 Press the key to control the... Of machine B through the serial port LEDI Lighten up 、LED2 destroy , A machine k2 Press the key to control machine B LED1 destroy 、LED2 Lighten up , A machine k3 Press the key to control machine B LED1 and LED2 All bright .
(2) Machine B K4 Press the key to control the serial port to send k4 The number of keystrokes , And displayed on the first machine P0 On the digital tube of the mouth .
【 Attach links to all resources of this experiment ( Code + Simulation file )
Click to download 】
Simulation diagram
The following is the experimental code ( It is divided into two programs If you don't know how to write, please download the resources step by step Inside is all the code and simulation files )
// A machine
#include<reg51.h>
unsigned char j=10;
char sign=1;
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
void delay(unsigned char k){
unsigned char i,j,h;
for(h=0;h<k;h++){
for(i=0;i<25;i++){
for(j=0;j<20;j++);
}
}
}
void twoDigitDisplay(unsigned char num,unsigned char time,unsigned char portNumber){
unsigned char box[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
unsigned char k;
if(portNumber==0){
if(num<0||num>99||time<0){
for(k=0;k<40;k++){
P2=0x00;
}
}else{
for(k=0;k<time;k++){
unsigned char numR = num%10;
unsigned char numL = num/10;
P2=0x11;
P0=box[numL];
delay(12);
P2=0x22;
P0=box[numR];
delay(12);
}
}
}
}
void main(){
unsigned char sum=0;
TR0=1;
EA=1;
ET0=1;
TMOD=0x21;
TH0=0xee;
TL0=0x00;
while(1){
if(sign==1){
// Perform data transmission
TR1=1;
TMOD=0x20;
TL1=0xfd;
TH1=0xfd;
SCON=0x40;
PCON=0x00;
}
while(sign==1){
// send data SBUF
if(P10==0){
SBUF=0xfe;while(TI==0);TI=0;continue;}
if(P11==0){
SBUF=0xfd;while(TI==0);TI=0;continue;}
if(P12==0){
SBUF=0xfc;while(TI==0);TI=0;continue;}
SBUF=0xff;
}
if(sign==-1){
// Perform data reception
TR1=1;
TL1=0xfd;
TH1=0xfd;
SCON=0x50;
PCON=0x00;
}
while(sign==-1){
// receive data SBUF
if(SBUF==0xf0){
sum++;
twoDigitDisplay(sum,5,0);
}
if(SBUF==0x0f){
twoDigitDisplay(sum,5,0);
}
}
}
}
void int0() interrupt 1 {
j--;
if(j==0){
TF0=0;
TH0=0xee;
TL0=0x00;
sign=sign*(-1);
j=10;
}
}
// B machine
#include<reg51.h>
unsigned char j=10;
char sign=1;
sbit P10=P1^0;
void main(){
unsigned char increment=0;
TR0=1;
EA=1;
ET0=1;
TMOD=0x21;
TH0=0xee;
TL0=0x00;
if(sign==1){
// Perform data reception
TR1=1;
TL1=0xfd;
TH1=0xfd;
TMOD=0x20;
SCON=0x50;
PCON=0x00;
}
while(sign==1){
// receive data SBUF
P2=SBUF;
}
if(sign==-1){
// Perform data transmission
TR1=1;
TL1=0xfd;
TH1=0xfd;
SCON=0x40;
PCON=0x00;
}
while(sign==-1){
// send data SBUF
if(P10==0){
SBUF=0xf0;
while(TI==0);TI=0;
}else{
SBUF=0x0f;
while(TI==0);TI=0;
}
}
}
void int0() interrupt 1 {
j--;
if(j==0){
TF0=0;
TH0=0xee;
TL0=0x00;
sign=sign*(-1);
j=10;
}
}
Experimental experience
1. The core idea of the program
This program adopts timed interrupt it0 timing , every other 50ms The two machines switch the receiving and sending States once . Machine a starts sending by default , Machine B receives by default . The timing of both machines is consistent , When the time comes, the first machine changes from sending to receiving . Machine B changes from receiving to sending . This ensures that the steps of the two machines are completely opposite . But speculation : In this way , The machine runs for a long time , The pace of the two machines will gradually become uncoordinated . It cannot achieve the purpose of long-term use .
① A machine sends data : Data sent by machine a SBUF from P1 State control of the three switches of the port , Hand it over to machine B to receive .
② Machine B receives data : Machine B receives the SBUF, Choose and judge , So that a machine P0 Port of LED Lights show different effects .
③ Machine B sends data : Machine B performs the sending task every time , Will send a 0xf0 perhaps 0x0f, The default is 0xf0 But when machine B p10 When the port button is pressed , Machine B will send another data 0x0f, Deliver the first machine .
④ A machine receives data : Machine a is based on the data from machine B SBUF Make a judgment on the value of . If it is 0x0f, sum The variable increases by one and then uses twoDigitDisplay(sum,5,0) Function to display . If it is 0xf0 ,sum The value of the variable does not increase , Give it directly to twoDigitDisplay(sum,5,0) Function shows .
2. Problems encountered in the experiment
Because programming is in a hurry , The experiment did not switch Replace with button, Cause switch k1 To k3 When pressed, it must be disconnected manually to switch the next state . And there will be tiny flicker in the digital display , Speculative delay function delay There is a problem with parameter setting .
Interested friends can continue to improve , Welcome to communicate with me .
This article is completely original Please respect the fruits of labor Welcome to forward give the thumbs-up Update more MCU experiments from time to time .
At last, I attach all the resource links of this experiment ( Code + Simulation file )
Click to download
边栏推荐
- IPv6 experiment
- 稻 城 亚 丁
- 3. C language uses algebraic cofactor to calculate determinant
- Aurora system model of learning database
- 【九阳神功】2022复旦大学应用统计真题+解析
- 六种集合的遍历方式总结(List Set Map Queue Deque Stack)
- Tyut outline of 2022 database examination of Taiyuan University of Technology
- 透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
- View UI plus released version 1.3.1 to enhance the experience of typescript
- MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
猜你喜欢
Arduino+ water level sensor +led display + buzzer alarm
这次,彻底搞清楚MySQL索引
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
9. Pointer (upper)
Data manipulation language (DML)
Custom RPC project - frequently asked questions and explanations (Registration Center)
C语言实现扫雷游戏(完整版)
Mortal immortal cultivation pointer-2
PriorityQueue (large root heap / small root heap /topk problem)
7.数组、指针和数组的关系
随机推荐
最新坦克大战2022-全程开发笔记-1
Wei Pai: the product is applauded, but why is the sales volume still frustrated
[modern Chinese history] Chapter V test
arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
【毕业季·进击的技术er】再见了,我的学生时代
View UI Plus 發布 1.3.1 版本,增强 TypeScript 使用體驗
5月14日杂谈
(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
Rich Shenzhen people and renting Shenzhen people
[the Nine Yang Manual] 2018 Fudan University Applied Statistics real problem + analysis
View UI Plus 发布 1.3.1 版本,增强 TypeScript 使用体验
5.函数递归练习
9.指针(上)
[the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
Redis实现分布式锁原理详解
1.C语言初阶练习题(1)
杂谈0516
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
2.C语言初阶练习题(2)
关于双亲委派机制和类加载的过程