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() ); }
running result:
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() ); }
running result:
(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; } } }
running result:
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):
Send "123456" (send 3 times):
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; }
First send the character 'a', which is the default string sending method:
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:
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)).
Previous article:51 MCU serial port program C language version
Next article:51 MCU software program reset
- Popular Resources
- Popular amplifiers
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Learn C++ in 21 Days (8th Edition) (Siddhartha Rao, Yuan Guozhong)
- Algorithm Notebooks Practical Guide for Computer Training (Edited by Hu Fan and Zeng Lei)
- Arduino Advanced Development Definitive Guide (Original Book 2nd Edition)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- FPGA Implementation of 32-bit Single-precision Floating-point Multiplier
- A question about the data sheet
- TI low-power wireless charging solution
- TMS320F28335 project development record 8_28335 GPIO pin
- EEWORLD University Hall----Live Replay: ON Semiconductor Image Sensors Promote the Development of Automobile, Machine Vision and Artificial Intelligence
- mpy adds new sys.atexit function
- Newly made lithium battery management board
- A beautiful kitchen timer using an LED matrix display
- [RVB2601 Creative Application Development]_3_Music Play Construction
- The drive signal amplitude is too large at 240mV, and the optical module optical eye diagram quality is poor