Library function printf in KeilC51

Publisher:omicron25Latest update time:2016-11-07 Source: eefocusKeywords:KeilC51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
When using Keil simulation to view the serial port output, I encountered a problem. The code is as follows:

#include

#include /* Add to use printf function*/

main()

{

     SCON = 0x50; /*SCON: working mode 1, 8-bit UART, allow receiving*/

     TMOD |= 0x20;/*TMOD: Timer T1, working mode 2, 8-bit automatic reload mode*/

     TH1= 0xf3;/*When the baud rate is 2400, the timer initial value*/

     TR1= 1;/*Timer T1 starts running*/

     TI = 1; /*Allow sending data*/

     printf("hello world");

}

After debugging and running the above code, the serial port continuously outputs "hello world" and the program falls into an infinite loop.

Solution to the problem: add while(1); at the end of the program.

Reason: If you do not add while(1); the MCU will execute a RET instruction, which will reset the program.

           Bit (software reset, pop 0000H from the stack to the PC register), the program restarts after reset

           implement.

Thinking: I am used to programming in Windows. After the main program is executed, it will exit to the operating system, and the microcontroller will

           It’s different now, where to retreat? The following is the conversation between the microcontroller and the compiler:

           MCU: I have nothing to do, I'd rather die~~~

           Compiler: Want to die? You should reset it~~~RET

           MCU: o, shit~~~

 

MCU serial port related issues: The standard 51 has only one serial port, but now many MCUs based on the 51 core have

The microcontroller will have two or more serial ports, so which serial port does the printf function target?

Answer: There is a putchar.c file in the Keil\C51\LIB directory. The printf function will call the

The putchar function uses the standard 51 registers (such as TI, SBUF).

Their addresses are fixed, so we can see that the printf function only targets serial port 0.

Support serial port 1 or serial port 2. Then modify the register of putchar function, for example, change SBUF to

SBUF1. But can I use the printf function to apply to both serial port 0 and serial port 1? I want to do it myself.

French bar :)

In addition: If you just use printf to output a string, you should write a function yourself, because printf needs to process

The output of various types of data takes up a lot of code space. I tried it and there was only one printf statement in the program.

The code is 1K, and printf is also slow to execute.

Keywords:KeilC51 Reference address:Library function printf in KeilC51

Previous article:The use of header files in C51 programming
Next article:C51 Language Design Basics--Data Types and Functions

Recommended ReadingLatest update time:2024-11-16 13:07

There are a series of problems when using printf() in MPLAB X IDE
Development Environment Microcontroller: PIC18F45K80 IDE: MPLAB X IDE Solve the cause of printf exception. 1. The following printf successfully prints data to the serial port #putch function implementation, printf requires void putch(char data)   {     while(!TXSTA1bits.TRMT);         continue;     TXREG1=data; }
[Microcontroller]
Use of AVR serial port getchar(), getchar(), printf() and other functions
Use of AVR serial port getchar(), getchar(), printf() and other functions AVR microcontrollers can use getchar(), getchar(), printf() and other functions, and their operations are connected to the serial port by default. You need to include stdio.h. ICC will include this file by default, but it is still recomm
[Microcontroller]
Use of AVR serial port getchar(), getchar(), printf() and other functions
STM32 serial port printf sends Chinese garbled characters
1. First, make sure that the Keil program is compiled correctly and the program has been debugged. English characters can be sent normally, but Chinese characters are garbled when sent. 2. Use Notepad to open the main.c file (or other main program), click Save As, select ANSI encoding in the lower right corner, and
[Microcontroller]
STM32--Analysis of printf redirection in Atomic Brother USART experiment
There is a part of the USART code that I don't quite understand. #if 1 #pragma import(__use_no_semihosting)              //Support functions required by the standard library                  struct __FILE  {      int handle;      /* Whatever you require here. If the only file you are using is */      /* standard out
[Microcontroller]
Using printf function in Keil
In C51, printf and scanf functions are mainly used for sending and receiving serial data. Sometimes when debugging software, you need to use the printf function to output some results To see these results in Keil, only two steps are needed: (Of course, you must first enter the debug interface and then do the followi
[Microcontroller]
Solution to the problem that the STM32 program cannot run after adding the printf function (serial port experiment)
The default output device of the standard library function is the display. To output to the serial port or LCD, you must redefine the functions related to the output device called in the standard library function. For example: if printf is output to the serial port, the output in fputc needs to be directed to the seri
[Microcontroller]
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号