51 MCU two-way communication example

Publisher:RainbowGardenLatest update time:2021-10-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The LED light of machine B can be lit by controlling the buttons of machine A, and numbers can be sent to machine A by controlling the buttons of machine B, and displayed using a digital tube.


Circuit diagram:

insert image description here

Machine code:

#include

#define uchar unsigned char

#define uint unsigned int 


sbit LED1=P1^0;

sbit LED2=P1^3;

sbit K1=P1^7;

uchar Operation_No=0; //Operation code

//Digital tube code

uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};


//Delay

void DelayMS(uint ms)

{

flying i;

while(ms--) 

{

for(i=0;i<120;i++);

}

}

//Send characters to the serial port

void Putc_to_SerialPort(uchar c)

{

SBUF=c; 

while(TI==0); 

TI=0; //TI hardware set to 1, software cleared to 0

}

//Main program

void main()

{

LED1=LED2=1; 

P0=0x00;

SCON=0x50; //Serial port mode 1, allow receiving

TMOD=0x20; //T1 working mode 2

PCON=0x00; //Baud rate is not doubled

TH1=0xfd; 

TL1=0xfd;    //9600

IF=RI=0; //

TR1=1;

IE=0x90; //Enable serial port interrupt

while(1)

{

DelayMS(100);

if(K1==0) //Select operation code 0, 1, 2, 3 when K1 is pressed

{

while(K1==0); 

{

Operation_No=(Operation_No+1)%4;

}

 

switch(Operation_No) //Send A/B/C or stop sending according to the operation code

{

case 0:    Putc_to_SerialPort('X'); LED1=LED2=1;break;

case 1:    Putc_to_SerialPort('A');LED1=~LED1;LED2=1;break;

case 2:    Putc_to_SerialPort('B'); LED2=~LED2;LED1=1; break;

case 3:    Putc_to_SerialPort('C'); LED1=~LED1;LED2=LED1; break;

}

}

}

}

//Serial port receiving interrupt function of machine A

void Serial_INT() interrupt    4

{

if(RI)

{

RI=0;

if(SBUF>=0&&SBUF<=9) 

{

P0=DSY_CODE[SBUF];

}

else 

{

P0=0x00;

}

}

}


Machine B code:

#include

#define uchar unsigned char

#define uint unsigned int 

sbit LED1=P1^0;

sbit LED2=P1^3; 

sbit K2=P1^7; 

fly NumX=-1;

//Delay

void DelayMS(uint ms)

{

flying i;

while(ms--) 

{

for(i=0;i<120;i++);

}

}

//Main program

void main()

{

LED1=LED2=1;

SCON=0x50; //Serial port mode 1, allow receiving

TMOD=0x20; //T1 working mode 2



TH1=0xfd; //Baud rate 9600

TL1=0xfd;

PCON=0x00; //Baud rate is not doubled

SEE=OF=0; 

TR1=1; 

IE=0x90; 

while(1)

{

DelayMS(100);

if(K2==0)

{

while(K2==0);

NumX=++NumX; //Generate a number in the range of 0~10, where 10 means off

SBUF=NumX; 

if(NumX==10)

{

NumX=-1;

}

while(TI==0); 

TI=0; //TI hardware set to 1, software cleared to 0

}

}

}

void Serial_INT() interrupt 4

{

if(RI) //If received, LED will be activated

{

RI=0;

switch(SBUF) //Complete different actions according to the different command characters received

{

case 'X': LED1=LED2=1;break; //all off

case 'A':  LED1=0;LED2=1;break;    //LED1 亮 

case 'B':  LED2=0;LED1=1;break;    //LED2 亮 

case 'C': LED1=LED2=0; //all on

}

}

}


Effect screenshots:

 search

会员中心 

collect

dynamic

information

creation

51 MCU two-way communication example

Qingye Murong  2020-05-06 09:16:53   2089 51 single chip microcomputer

51 single chip microcomputerThis column includes this content

18 articles2 Subscribe

Subscribe to our column

  The LED light of machine B can be lit by controlling the buttons of machine A, and numbers can be sent to machine A by controlling the buttons of machine B, and displayed using a digital tube.

Circuit diagram:

insert image description here

Machine code:

#include
#define uchar unsigned char
#define uint unsigned int 

sbit LED1=P1^0;
sbit LED2=P1^3;
sbit K1=P1^7;
uchar Operation_No=0; //Operation code
//Digital tube code
uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

//Delay
void DelayMS(uint ms)
{
	flying i;
	while(ms--) 
	{
		for(i=0;i<120;i++);
	}
		
}
//Send characters to the serial port
void Putc_to_SerialPort(uchar c)
{
	SBUF=c; 
	while(TI==0); 
	TI=0; //TI hardware set to 1, software cleared to 0
}
//Main program
void main()
{
	LED1=LED2=1; 
	P0=0x00;
	SCON=0x50; //Serial port mode 1, allow receiving
	TMOD=0x20; //T1 working mode 2
	PCON=0x00; //Baud rate is not doubled
	TH1=0xfd; 
	TL1=0xfd;   			//9600
	IF=RI=0;       //
	TR1=1;
	IE=0x90; //Enable serial port interrupt
	while(1)
	{
		DelayMS(100);
		if(K1==0) //Select operation code 0, 1, 2, 3 when K1 is pressed
		{
		while(K1==0); 
			{
				Operation_No=(Operation_No+1)%4;
			}
			
		 
		switch(Operation_No) //Send A/B/C or stop sending according to the operation code
			{
				case 0:    Putc_to_SerialPort('X'); LED1=LED2=1;break;
				case 1:    Putc_to_SerialPort('A');LED1=~LED1;LED2=1;break;
				case 2:    Putc_to_SerialPort('B'); LED2=~LED2;LED1=1; break;
				case 3:    Putc_to_SerialPort('C'); LED1=~LED1;LED2=LED1; break;
			}
		}
	}
}
//Serial port receiving interrupt function of machine A
void Serial_INT() interrupt    4
{
	if(RI)
	{
		RI=0;
		if(SBUF>=0&&SBUF<=9) 
		{
			P0=DSY_CODE[SBUF];
		}
			
		else 
		{
			P0=0x00;
		}
			
	}
} 

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182

Machine B code:

#include
#define uchar unsigned char
#define uint unsigned int 
sbit LED1=P1^0;
sbit LED2=P1^3; 
sbit K2=P1^7; 
fly NumX=-1;
//Delay
void DelayMS(uint ms)
{
	flying i;
	while(ms--) 
	{
		for(i=0;i<120;i++);
	}
		
}
//Main program
void main()
{
	LED1=LED2=1;
	SCON=0x50; //Serial port mode 1, allow receiving
	TMOD=0x20; //T1 working mode 2


	TH1=0xfd; //Baud rate 9600
	TL1=0xfd;
	PCON=0x00; //Baud rate is not doubled
	SEE=OF=0; 
	TR1=1; 
	IE=0x90; 
	while(1)
	{
		DelayMS(100);
		if(K2==0)
		{
			while(K2==0);
			NumX=++NumX; //Generate a number in the range of 0~10, where 10 means off
			SBUF=NumX; 
			if(NumX==10)
			{
				NumX=-1;
			}
			while(TI==0); 
			TI=0; //TI hardware set to 1, software cleared to 0
		}
	}
}
void Serial_INT() interrupt 4
{
	if(RI) //If received, LED will be activated
	{
		RI=0;
		switch(SBUF) //Complete different actions according to the different command characters received
		{
			case 'X': LED1=LED2=1;break; //all off
			case 'A':  LED1=0;LED2=1;break;    //LED1 亮 
			case 'B':  LED2=0;LED1=1;break;    //LED2 亮 
			case 'C': LED1=LED2=0; //all on
		}
	}
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263

Effect screenshots:

insert image description here


Reference address:51 MCU two-way communication example

Previous article:51 MCU and computer communication, 51 MCU and PC communication method summary
Next article:Serial communication of 51 single chip microcomputer (I)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号