51 MCU implements printf

Publisher:MagicGardenLatest update time:2020-03-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When the printf function formats the output, it calls the char putchar(char c); function. This function can be found in "stdio.h", so we need to construct such a function ourselves, that is, through the serial port putchar(), the code is as follows:


char putchar(char c) // redirect

{

Uart_SendChar(c);

return c;

}

 

void Uart_SendChar(unsigned char  dat)

{

    SBUF = dat; 

    while(!IF); 

    IF = 0; 

}

Then after the serial port is initialized, include the "stdio.h" header file and you can use the printf function.



NOTE:


Using the printf function in the stdio library will increase the amount of code by about 1k, so use it with caution in a small-capacity 51 microcontroller! !

Reference address:51 MCU implements printf

Previous article:How to use printf on MCU to make serial port output
Next article:The 51 MCU serial port prints garbled characters in both Chinese and English

Recommended ReadingLatest update time:2024-11-16 21:32

AVR microcontroller serial port to implement printf (using variable parameter function)
   #include    #include       #include       typedef unsigned char uint8;   static void usart_init(void)   {   UCSRA = 0x02;      UCSRB = 0x18;      UCSRC = 0x06;      UBRRH = 0x00;    UBRRL = 103;    }   static void put_char(uint8 data)   {       if (data == '/r')           put_char(0x09);       wh
[Microcontroller]
STM32 Learning 9: Printf redirection through USART
Outputting the contents of the printf function through the USART serial port can facilitate debugging of the STM32 internal program. The sample code is as follows: /*******************************************************************************  * Function Name  : fputc  * Description: Redirect fput to redirect print
[Microcontroller]
51 MCU printf function debugging program
It is mainly divided into three parts: (1) 51 single-chip microcomputer program part; (2) Keil tool part; (3) Why printf outputs to the serial port. (1) 51 MCU program part #include stdio.h #include reg51.h   void InitUART(void) //Use timer 1 as the serial port baud rate generator {     TH1 = 0xF3; //Crystal osc
[Microcontroller]
STM32 printf redirection
Configuration method of using printf to send data in STM32 serial communication (development environment Keil RVMDK)   It is very convenient to use printf to send data in the STM32 serial communication program. However, there are always problems when using it for the first time. The most common problem is that the mai
[Microcontroller]
51 MCU (STC15W408AS) maps printf function to realize serial port transmission and reception
Prerequisite: Realize serial port data transmission and reception /********************************************************  *Function name: char putchar(char c)                              *Function: map printf                                      *Parameter: char c                                        *Return v
[Microcontroller]
stm8——Implementation of serial port printf
Serial port communication is one of the most basic and important functions of microcontroller learning. The serial port can be used indirectly as a debugging interface to achieve communication between the microcontroller and the computer. It can also communicate with some modules (such as Bluetooth, WiFi), and can als
[Microcontroller]
Printf() function redirection problem about serial port communication in STM32
It is very convenient to use the printf() function to send data in the STM32 serial communication program, but it requires simple configuration first. The following are two configuration methods. Method 1: Configure project properties Step 1: Include "stdio.h" (standard input and output header file) in main.c; Step 2:
[Microcontroller]
MCU redirects printf to SWO port
一. IAR SWO IAR comes with the printf via SWO function in the compilation options. First, let's talk about how to output information to the SWO port. It's done in one sentence. ITM_SendChar(ch);  Strictly speaking, ITM is a feature provided by Cortex-M, not STM32. Use this function to output information to the SWO port
[Microcontroller]
MCU redirects printf to SWO port
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号