当前位置:网站首页>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

 Insert picture description here

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

原网站

版权声明
本文为[axu_ nine hundred and ninety thousand seven hundred and seven]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060916479851.html