当前位置:网站首页>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
边栏推荐
- [the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
- 优先队列PriorityQueue (大根堆/小根堆/TopK问题)
- Voir ui plus version 1.3.1 pour améliorer l'expérience Typescript
- Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
- Caching mechanism of leveldb
- View UI plus released version 1.2.0 and added image, skeleton and typography components
- A brief introduction to the database of tyut Taiyuan University of technology in previous years
- (super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
- 自定义RPC项目——常见问题及详解(注册中心)
- 4. Binary search
猜你喜欢
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
西安电子科技大学22学年上学期《信号与系统》试题及答案
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
1.初识C语言(1)
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
PriorityQueue (large root heap / small root heap /topk problem)
Quickly generate illustrations
最新坦克大战2022-全程开发笔记-1
2. C language matrix multiplication
随机推荐
用栈实现队列
最新坦克大战2022-全程开发笔记-1
[modern Chinese history] Chapter 9 test
Floating point comparison, CMP, tabulation ideas
[while your roommate plays games, let's see a problem]
Change vs theme and set background picture
[面試時]——我如何講清楚TCP實現可靠傳輸的機制
fianl、finally、finalize三者的区别
Data manipulation language (DML)
六种集合的遍历方式总结(List Set Map Queue Deque Stack)
2.初识C语言(2)
Relational algebra of tyut Taiyuan University of technology 2022 database
Application architecture of large live broadcast platform
5. Function recursion exercise
9.指针(上)
【九阳神功】2016复旦大学应用统计真题+解析
2.C语言初阶练习题(2)
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
9. Pointer (upper)
西安电子科技大学22学年上学期《射频电路基础》试题及答案