Yesterday we talked about how to send an English letter through the UART port. In fact, according to the ASCII code table, uppercase and lowercase letters, numbers, punctuation marks, and some symbols can all be sent, as long as you find the corresponding encoding.
Today I will talk about how to send strings and Chinese characters using the UART port.
If we follow yesterday's idea, sending a string of letters or numbers is also easy to implement. I just need to find the ASCII code corresponding to this string of letters or numbers and write it into an array. But this requires a table lookup, which is a bit troublesome. Today I will show you a simpler method.
Suppose I want to send 5 letters A, how can I do it using yesterday's method? Define an array:
Uchar table[] = {0x41,0x41,0x41,0x41,0x41}; //Five letters A
Looks silly, this time the method:
Uchar table[] = "AAAAA"; //Five letters A
Do you think this is high-end?
What if I want to send Chinese characters?
Uchar table[] = "Hello everyone, I am MCU Qihang!"; // string
Why is it so simple? I have to talk about coding again, although I don’t really understand it.
First of all, we know that computers can recognize binary codes: 0 and 1.
ASCII code is the standard code for processing English information. Sentences such as hello, fine, thank you! can be represented by ASCII code.
What about Chinese characters? In order for our language to go beyond Asia and enter computers, we also need a unified encoding. So, in May 1981, the National Bureau of Standards promulgated the "Chinese Character Encoding Character Set for Information Interchange - Basic Set", code-named GB2312-8. In this encoding, Chinese characters are represented by two bytes.
When compiling the string of characters we wrote, KEIL will help us generate the corresponding code, collectively known as: machine code! Chinese characters have their own machine codes, and English has its own machine codes.
Having said so much, let's start writing the program. It's still the circuit from yesterday, but this time it displays more characters! As shown below:
/**********************51 MCU learning routine****************************
* Platform: Keil U4 + STC89C52
* Name: UART port transmits a character A
*Written by: Qihang
* Crystal: 11.0592MHZ
**********************************************************************/
#include #define jingzhen 11059200UL /*Use 11.0592M crystal*/ #define botelv 9600UL /*Baud rate is defined as 9600*/ unsigned char zifuchuan[]="Hello everyone, I am MCU Qihang!"; //Characters to be displayed. volatile unsigned char sending; void delay(unsigned char i) { unsigned char j,k; for(j=i;j>0;j--) for(k=90;k>0;k--); } void init(void) //Serial port initialization { EA=0; //Temporarily disable interrupts TMOD&=0x0F; //Timer 1 mode control in the upper 4 bits TMOD|=0x20; //Timer 1 works in mode 2, automatic reload mode SCON=0x50; //Serial port works in mode 1 TH1=256-jingzhen/(botelv*12*16); //Calculate the timer reload value TL1=256-jingzhen/(botelv*12*16); PCON|=0x80; //Double the serial port baud rate ES=1; //Serial interrupt enabled TR1=1; //Start timer 1 REN=1; //Allow receiving EA=1; //Enable interrupts } void send(unsigned char d) //Send one byte of data. The parameter d is the data to be sent. { SBUF=d; //Write data to the serial port buffer sending=1; //Set the sending flag while(sending); //Wait for sending to complete } void sendc(unsigned char * pd) { while((*pd)!='') //Send string until 0 is encountered { send(*pd); //Send a character pd++; //Move to the next character } } int main() { init(); while(1) { delay(200); sendc(zifuchuan); } return(0); } void uart(void) interrupt 4 //Serial port send interrupt { if(RI) //Receive data { RI=0; // Clear interrupt request } else //After sending one byte of data { TI=0; sending=0; //clear the sending flag } }
Previous article:Learning 51 MCU - AD sampling and DA output based on PCF8591
Next article:Learn 51 MCU-UART port sends a character
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Study DSP basics and summarize fixed-point decimal operations
- How to Make DSP Digital Oscillator Generate Phase-Shifted Sine Wave
- The color of the soldering pad has turned purple, how can it turn red?
- Design and implementation of face recognition system based on DSP
- What kind of PMIC is needed for modern design?
- ZigBee Wireless Smart Door Lock Hotel Networking Example
- Showing goods [Little Red Board]
- 2021 ON Semiconductor Avnet RSL10 Bluetooth SoC Development and Design Competition, Fifth Post (Prototyping and Board Submission)
- msp430g2533 serial port assistant debugging program; connect 1.1 and 1.2 with Dupont line, and you can see the information on the serial port. Red and green...
- Summary: MianQi's journey to study motor drive (short video)