当前位置:网站首页>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
边栏推荐
- Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
- [modern Chinese history] Chapter V test
- Cookie和Session的区别
- 3.猜数字游戏
- Atomic and nonatomic
- [the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
- Rich Shenzhen people and renting Shenzhen people
- Smart classroom solution and mobile teaching concept description
- 2. Preliminary exercises of C language (2)
- 用栈实现队列
猜你喜欢

2.C语言矩阵乘法

自定义RPC项目——常见问题及详解(注册中心)

Quickly generate illustrations

1. C language matrix addition and subtraction method

MySQL lock summary (comprehensive and concise + graphic explanation)

更改VS主题及设置背景图片

魏牌:产品叫好声一片,但为何销量还是受挫

Cookie和Session的区别

Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology

8. C language - bit operator and displacement operator
随机推荐
Cloud native trend in 2022
Atomic and nonatomic
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
Cookie和Session的区别
4.分支语句和循环语句
4.二分查找
Application architecture of large live broadcast platform
最新坦克大战2022-全程开发笔记-2
六种集合的遍历方式总结(List Set Map Queue Deque Stack)
String abc = new String(“abc“),到底创建了几个对象
C language Getting Started Guide
MPLS experiment
(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
9.指针(上)
MySQL锁总结(全面简洁 + 图文详解)
arduino+水位传感器+led显示+蜂鸣器报警
5月27日杂谈
View UI Plus 发布 1.3.1 版本,增强 TypeScript 使用体验
View UI plus released version 1.3.1 to enhance the experience of typescript
View UI plus released version 1.3.0, adding space and $imagepreview components