The microcontroller does not support the printf function, so implement several common serial port printing functions

Publisher:MoonlightStarLatest update time:2020-06-16 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Under keil C51 or iar for c8051 compiler:

1. int occupies two bytes: -32768~+32767

2. Long occupies four bytes: -2147483648~+2147483647

3. float occupies four bytes: 3.40E+38 ~ +3.40E+38

4. Double occupies 8 bytes range: -1.79E+308 ~ +1.79E+308

51 MCU is a general term for all MCUs compatible with Intel 8031 ​​instruction system. The ancestor of this series of MCUs is Intel's 8004 MCU. Later, with the development of Flash ROM technology, 8004 MCU has made great progress and become one of the most widely used 8-bit MCUs.



In stand-alone development, serial port debugging or collecting real-time data of various data types often requires the use of printf() function redirection, but the performance of printf() is poor for microcontrollers with scarce resources. For the convenience of future work, several commonly used printing functions are recorded.


For non-recursive implementation, see another blog http://www.cnblogs.com/02xiaoma/archive/2012/06/22/2558618.html

However, the void printflt(double flt) in the blog cannot correctly print the format of 0 after the decimal point, such as 1.000123, 1.01 1.001, 1.0001, etc.


Non-recursive

Integer printing

void printInterger(int integer)
{
    unsigned char isnegative = 0,len = 0, intstr[10]={''};
    if(integer < 0)
    {
        isnegative = 1;
        integer = 0 - integer;
    }
    if (integer == 0)
        intstr[len++] = '0';
    while (integer)
    {
        intstr[len++] = (integer % 10) + '0';
        integer = integer / 10;
    }
    if(isnegative)
        intstr[len++] = '-';
    //Note the data type of k. Use k >= 0; as the end condition. k is a signed type. If it is an unsigned type, the k value rolls back to the maximum value
    for(len = len; len > 0; len--)
    {
        //printf("%c", intstr[len-1]);
        UartSend(UART1,&intstr[len-1],1);
}}

Floating point numbers

void printFlt(float flt,int len)
{
    int integer;
    integer = (int)flt;
    printHex(integer);
    //printf(".");
    UartSend(UART1,".",1);
 
    flt = flt - integer;
    //while(flt&&(len--))
    while(flt)
    {
        flt = flt*10;
        integer = (int)flt + '0';
        flt = flt - integer;
        //printf("%c",integer);
        UartSend(UART1,&integer,1);
    }
}

————————————————

Recently, some students are troubled by the problem of how to use the serial port of the microcontroller to output and display on the host computer serial port assistant during the learning process of 51 single-chip microcomputer. In fact, many development environments support the use of the most commonly used printf function in C language, and Keil is no exception. It is not only the most commonly used STC89C52, but also other 51 series microcontrollers under Keil, such as STC12, STC15, etc. The specific steps are as follows:

Create an empty project in Keil, add a source file, such as main.c, paste the following code, compile to generate a hex file, and download it to the microcontroller.

        It should be noted that: 1. The stdio.h header file needs to be included 2. TI = 1 is required in the serial port initialization function;

        Without further ado, let's get straight to the code:

#include
#include
 
void InitUART(void)//Use timer 1 as the serial port baud rate generator
{
    TH1 = 0xFD; //Crystal oscillator 11.0592mhz baud rate set to 9600
    TL1 = TH1;
    TMOD |= 0x20; //Timer 1 mode 2
    SCON = 0x50; //Serial port receive enable
    ES = 1; //Serial port interrupt enable
    TR1 = 1; //Timer 1 enable
    TI = 1; //Send interrupt flag, must be set
}
//Millisecond delay function
void delay_ms(unsigned int t)
{
    unsigned char a,b;
    while(t--)
    {
      for(b=102;b>0;b--)
      for(a=3;a>0;a--);
    }
}
 
void main()
{
    InitUART(); //Initialize the serial port
    EA = 1; //Open the general interruptwhile
    (1)
    {
        delay_ms(500);
        printf("Hello World!n");//Serial port print result
    }
}


Keywords:MCU Reference address:The microcontroller does not support the printf function, so implement several common serial port printing functions

Previous article:How to use printf for serial port debugging on 51 single chip microcomputer
Next article:51 MCU custom function to realize printf() of any serial port

Recommended ReadingLatest update time:2024-11-23 11:30

Introduction to AVR microcontrollers: structure and application of microcontroller embedded systems
Single-chip microcomputer, full name of single-chip microcontroller, single-chip microcomputer (Single-Chip Microcomputre), also known as embedded microcontroller (Embedded Microcontroller). The embedded system composed of single-chip microcomputer as the main control core is called single-chip microcomputer embedded
[Microcontroller]
Design of operational amplifier parameter measurement system based on DDS and MCU
introduction In the circuit design of modern scientific research institutions and the teaching of electronic systems in colleges and universities, integrated operational amplifiers are widely used as basic devices for signal processing. Accurately mastering the parameters of integrated operational amplifiers is
[Microcontroller]
Design of operational amplifier parameter measurement system based on DDS and MCU
L298N DC Motor Speed ​​Control System Based on 51 Single Chip Microcomputer
This design selects STC89C52 MCU as the main control chip, selects DC motor with photoelectric encoder as the controlled object, uses T0 timer of MCU to generate PWM signal and sends it to DC motor. In Proteus simulation environment, L298N DC motor drive circuit, matrix keyboard scanning circuit and LCD12864 display c
[Microcontroller]
L298N DC Motor Speed ​​Control System Based on 51 Single Chip Microcomputer
What is the difference between the microcontroller system clock and the real-time clock?
1. Most single-chip microcomputers have only one system clock. It is the driving source of each beat of the CPU. This frequency is generally several MHz. The speed is relatively fast, and its purpose is nothing more than to make the single-chip microcomputer work faster. Why is it not the number of GHz? This is determ
[Microcontroller]
LCD display AT89C52 single chip DS1302 real time clock program
#include'reg52.h' //Header file containing microcontroller registers #include'intrins.h' //Header file containing _nop_() sbit RS=P2^0; //LCD read/write selection bit sbit RW=P2^1; //LCD read/write selection bit sbit E=P2^2; //LCD enable terminal sbit BF=P0^7; //Busy signal sbit SCLK=P1^0; //1302 clock output sbit DAT
[Microcontroller]
Ordinary MCU Teaching Lesson 7 Addressing Mode and Instruction System
Through the previous study, we have understood the internal structure of the microcontroller, and we also know that to control the microcontroller and let it do things for us, we need to use instructions. We have learned a few instructions, but they are very scattered. From now on, we will systematically study the inst
[Microcontroller]
Basic principles of 51 single chip microcomputer 16X16 dot matrix display learning board
  1. Function The      16×16 dot matrix display learning board is shown in the figure below. The brightness of each point of the LED display is uniform and sufficient. It can display graphics and text. The data code is obtained through the text image modeling software, and various graphics or text can be displayed. St
[Microcontroller]
Basic principles of 51 single chip microcomputer 16X16 dot matrix display learning board
Medium frequency power supply test system based on AVR microcontroller
1 Introduction The measurement and monitoring of electrical parameters are an important part of the power system. Starting from the engineering practice of the test system, this paper completes the measurement and real-time monitoring of the voltage, current, frequency, power factor, active power and other para
[Microcontroller]
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号