51 MCU serial port program, string/hexadecimal sending and receiving

Publisher:QuantumPulseLatest update time:2016-06-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This article will explain the sending and receiving of 51 serial communication. It is divided into: single character receiving, string receiving; decimal sending and receiving, hexadecimal sending and receiving.

For string sending and hexadecimal sending, please refer to: http://blog.csdn.net/yibu_refresh/article/details/22695063

The programs are all sent by the PC serial port tool, received by the microcontroller, and the received values ​​are returned to the PC.

 

1: Sending and receiving a single character

#include  #define uint unsigned int #define uchar unsigned char //define receiving character uchar Buffer; //Serial port initialization function void URATinit() { TMOD=0x20; SCON=0x50; EA=1; ES=1; TR1=1; TH1=0xfd; TL1=0xfd; } //Interrupt function void receive() interrupt 4 { if(RI) { Buffer=SBUF; RI=0; } SBUF=Buffer; while(!TI); TI=0; } //Main function void main() { URATinit() ); }
In the interrupt function, if data is received, RI is set to 1 by hardware. At this time, the data in the SBUF buffer is assigned to the Buffer, and RI is set to 0, waiting for the next reception. At the same time, the received data is put into the buffer and sent to the PC. When the transmission is completed, TI will be set to 1 by hardware. At this time, TI needs to be set to 0 to wait for the next transmission.

running result:

51 MCU serial port program, string/hexadecimal sending and receiving
 

If data 1 is sent, 1 is returned.

 

2. String Reception

(1)

#include  #define uint unsigned int #define uchar unsigned char //Define receive array uchar Buffer[5]={0}; uchar i=0,j=0; //Serial port initialization function void URATinit() { TMOD=0x20; SCON=0x50; EA=1; ES=1; TR1=1; TH1=0xfd; TL1=0xfd; } //Interrupt function void receive() interrupt 4 { if(RI) { Buffer[i]=SBUF; RI=0; } SBUF=Buffer[i]; while(!TI) ; TI=0; i++; if(i>=5){ i=0; } } //Main function void main() { URATinit() ); }		 
In the interrupt function, Buffer[] is used to receive the received data, and Buffer[] is sent to the host computer. Here, please pay attention to the definition of variable i. If it is defined as a global variable, Buffer[0-5] can receive data. It is necessary to count i to prevent overflow when it is greater than 5.

running result:

51 MCU serial port program, string/hexadecimal sending and receiving
 

(2)

#include  #define uint unsigned int #define uchar unsigned char //Define receive array uchar Buffer[5]; uchar i=0,flag; //Delay function delay(uint ms) { uchar i; while(ms--) for(i=0;i<123;i++); } //Serial port initialization function void URATinit() { TMOD=0x20; SCON=0x50; EA=1; ES=1; TR1=1; TH1=0xfd; TL1=0xfd; } //Interrupt function void receive() interrupt 4 { if(RI) { Buffer[i++]=SBUF; RI=0; if(i>=5){ i=0; } flag=1; } } //Main function void main() { uchar k=0; for(k;k<5;k++){ Buffer[k]=0; } URATinit( ); while(1){ if(flag) { uchar j=0; for(j;j<5;j++){ SBUF=Buffer[j]; while(!TI) ; TI=0; delay(50); } flag=0; } } }		  
In this case, the received string is not sent from the interrupt function, but in the main function. Therefore, a flag is needed to determine whether data is received, and a while(1) loop is used to continuously determine and output the received string.

running result:

51 MCU serial port program, string/hexadecimal sending and receiving
 

In fact, method (1) is better than method (2). Now let's send the strings "1234" and "123456" to see the effect:

Send "1234" (send 3 times):

51 MCU serial port program, string/hexadecimal sending and receiving
51 MCU serial port program, string/hexadecimal sending and receiving
 

Send "123456" (send 3 times):

51 MCU serial port program, string/hexadecimal sending and receiving
51 MCU serial port program, string/hexadecimal sending and receiving
 

It can be seen that method (1) can always correctly transmit and display the sent string, while method (2) has certain limitations and can only produce the desired display effect when transmitting a string of 5 characters.

Analysis shows that: (1) directly sends the received characters in the interrupt, one character is sent after receiving one, which is a real-time effect. (2) In the main program, the received Buffer array is sent as a whole. For example, when "1234" is received, since the Buffer is a 5-bit array, the first transmission will assign a value to Buffer[0-3], and Buffer[4] will not be assigned a value. When it is returned to the host computer, the first output is "1234", but the second transmission will assign a value to Buffer[4], and the overflow will return i to 0. When the Buffer is output again, it causes the transmission string to overlap and become chaotic. In fact, (1) also has this phenomenon, but the return of (1) is timely.

 

3. String sending and hexadecimal sending:

#include #define uchar unsigned char #define uint unsigned int uchar num; sbit dula=P2^6; //Declare the latch end of U1 latch sbit wela=P2^7; //Declare the latch end of U2 latch uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71}; void delay(uint xms) { uint i,j; for(i=xms;i>0;i--) //i=xms means delay of about xms milliseconds for(j=110;j>0;j--); } void display(uint value) //Display sub-function { uchar wan,qian,bai,shi,ge; //define thousands and hundreds of digits wan=value/10000; qian=value%10000/1000; bai=value%1000/100; shi=value%100/10; ge=value%10; dula=1; P0=table[wan]; dula=0; P0=0xff; wela=1; P0=0xfe; wela=0; delay(2); dula=1; P0=table[qian]; dula=0; P0=0xff; wela=1; P0=0xfd; wela=0; delay(2); dula=1; P0=table[bai]; dula=0; xff; wela=1; P0=0xf7; wela=0; delay(2); dula=1; P0=table[ge]; dula=0; P0=0xff; wela=1; P0=0xef; wela=0; delay(2); } void init() //initialization function{ TMOD=0x20; //set timer 1 working mode TH1=0xfd; TL1=0xfd; TR1=1; SM0=0; SM1=1; REN=1; EA=1; ES=1; } void main() { init(); while(1) { display(num); } } void ser() interrupt 4 //serial port interrupt function{ if(RI){ num=SBUF; RI=0; } //set RI to 0 in order to receive the next data SBUF=num; while(!TI); TI=0; }
This program can display the received characters/data on the digital tube and return the received data to the host computer for display.

First send the character 'a', which is the default string sending method:

51 MCU serial port program, string/hexadecimal sending and receiving
 

Send the character 'a', and the MCU returns 'a' to the host computer (default string display mode). However, the digital display shows 97, which is the ASCII code of 'a'. This shows that the digital tube is always ASCII code transmission during the transmission process. The reason why the digital tube does not show 'a' is that the digital tube is in decimal display mode, so it shows 97. ('a' (ASCII display) -> 97 (decimal display) -> 'a' (ASCII code display))

Send the character 'a', select hexadecimal sending, hexadecimal display:

51 MCU serial port program, string/hexadecimal sending and receiving
 

At this time, the sending end is hexadecimal 'a', which is decimal 10. The digital tube displays 10, and the returned value is displayed as 0A in hexadecimal.

From the reference article at the beginning of the article, we know that when sending in hexadecimal, two bits of data are sent each time, such as sending decimal 20, which is hexadecimal 14, the digital tube will display 20. (14 (hexadecimal display) -> 20 (decimal display) -> 14 (hexadecimal display)).

 
Reference address:51 MCU serial port program, string/hexadecimal sending and receiving

Previous article:51 MCU serial port program C language version
Next article:51 MCU software program reset

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号