Learn 51 MCU-UART port sends a string of characters

Publisher:郑哥Latest update time:2020-07-21 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

       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

}

}


Reference address:Learn 51 MCU-UART port sends a string of characters

Previous article:Learning 51 MCU - AD sampling and DA output based on PCF8591
Next article:Learn 51 MCU-UART port sends a character

Latest Microcontroller Articles
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号