51 MCU-Serial port printf series function

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

1. Implementation of printf series functions


We know that the microcontroller transmits data to the computer window through the serial port. Sometimes we need to send a string, sometimes we need to send the value of a variable, and sometimes we need to send a carriage return and line feed. So we encapsulate these three functions to facilitate the subsequent use of the serial port.


First of all, the "void printf_str(u8 *str)" function written by the author is specifically used to send strings to the computer.


Secondly, the value of the variable sent by "void printf_num(u32 num)" only supports displaying decimal numbers 0 to 4294967295, that is, the parameter is of u32 type.


Finally, "void printf_rn()" sends the carriage return and line feed characters.


You can try to read how these three parts of the code are implemented. If there are many places you don’t understand, you don’t need to delve into them. Just keep learning. The author will explain their application in the following knowledge points.


We hope that the serial port function module can also be encapsulated into a separate file for use like the timer.


So create the "uart.c" and "uart.h" files, copy the following code


2.uart.c code


#include

#include //See Chapter 6, Lecture 8 for details

   

void ConfigUART(u16 baud)

{

    SCON = 0x50; //Configure the serial port to mode 1

    TMOD &= 0x0F; // Clear the control bit of T1

    TMOD |= 0x20; //Configure T1 to mode 2

    TH1 = 256 - (11059200/12/32)/baud; //Calculate T1 reload value

    TL1 = TH1; //initial value equals reload value

    ET1 = 0; //Disable T1 interrupt

    ES = 1; // Enable serial port interrupt

    TR1 = 1; //Start T1

    

void printf_str(u8 *str)

    while(*str != '') //Continuously send string data until the end character is detected

    {

        SBUF=*str++;

        while(!TI); //Wait for the byte to be sent and TI is set to 1 to exit this while loop

        TI = 0; // clear the flag

    }  

}

   

void printf_num(u32 num)

{

    u8 buf[10];

    char i; //The value range is -128~127

    

    for (i=0; i<10; i++) //Convert the long integer to a 10-digit decimal array

    {

        buf[i] = num % 10;

        num = num / 10; //Discard the single digit and reload

    }

    

    for (i=9; i>=1; i--)  

    {

        if (buf[i] != 0)break; //Starting from the highest bit, ignore if 0 is encountered, and exit the loop if non-zero is encountered

    }

    

    while(i>=0) //The remaining low bits are sent out truthfully

    {   

        SBUF='0'+buf[i]; //If the value of buf[i] is 1, then in the "character format" mode, the computer window only needs to add '0'+1 to display the character "1", because '0' is the ASCII code value 48.

        while(!TI); //Wait for the byte to be sent and TI is set to 1 to exit this while loop

        TI = 0; // clear the flag

        i--;   

    }

}

   

void printf_rn() //Send carriage return and line feed characters

{

    SBUF='r';

    while(!TI); //Wait for the byte to be sent and TI is set to 1 to exit this while loop

    TI = 0; // clear the flag

    

    SBUF='n';

    while(!TI); //Wait for the byte to be sent and TI is set to 1 to exit this while loop

    TI = 0; // clear the flag

}


3.uart.h code


#ifndef __UART_H__

#define __UART_H__

   

void ConfigUART(u16 baud);

void printf_str(u8 *str);//send string

void printf_num(u32 num);//Send parameter value

void printf_rn(); //Send carriage return and line feed characters

   

#endif


4.main.c code


#include  

#include //See Chapter 6, Lecture 8 for details

#include

 

void main()

{  

    u8 key;

    u32 value=65535;

    LED_Init(); //Initialize LED hardware module

    KEY_Init(); //Initialize the key module

    EA = 1; //Close the main interrupt switch

    ConfigUART(9600);

   

    while(1)

    {  

        key=KEY_Scan(0,1000);

        if(key==4)

        {

            printf_str("value=");//send string

            printf_num(value); //Send the value of the variable

            printf_rn(); //Send carriage return and line feed characters

            value++;

        }

    } 

}

  

void InterruptUART() interrupt 4

{

    if (RI) // Byte received

    {

        RI = 0; //Manually clear the receive interrupt flag                     

    }

}


We did not write "if (TI) { TI = 0; }" in the serial port interrupt function because we have cleared TI to 0 in all sending functions, so there is no need to write TI in the serial port interrupt function.


Don't forget to add "uart.c" and "uart.h" to the project file after creating them.

10.9.png


Open the serial port, select the "Character format display" mode, and keep pressing K4 to print the following content

10.10.png

Reference address:51 MCU-Serial port printf series function

Previous article:51 MCU - Detailed understanding of ASCII code
Next article:51 MCU-LCD screen code explanation

Recommended Content
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号