Using Printf() for serial port output in Keil C51

Publisher:konglingdeyuanLatest update time:2020-03-08 Source: eefocusKeywords:Keil  C51  Printf() Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

How to use printf to display information on the serial port? There are two ways. Because in keil C51, the printf function calls putchar to transfer data, and putchar should first determine whether TI is 1. If it is not 1, it waits for it to be 1. If it is 1, it clears it to 0 and then sends a character. Therefore, if you use the printf function directly, your program will wait for TI to be 1 in the putchar function. At this time, your program is equivalent to being dead. You can achieve your own purpose by rewriting the putchar function. TI is equivalent to being initialized and will not work if it is not assigned an initial value.


So the first way is to assign TI a value of 1 during initialization, for example


void uart_init()

 

{

    

PCON &= 0x7F; //Baud rate is not doubled

SCON = 0x50; //8-bit data, variable baud rate

AUXR |= 0x40; //Timer 1 clock is Fosc, i.e. 1T

AUXR &= 0xFE; //Serial port 1 selects timer 1 as the baud rate generator

TMOD &= 0x0F; // Clear timer 1 mode bit

TMOD |= 0x20; //Set timer 1 to 8-bit auto-reload mode

TL1 = 0xDC; //Set the initial timing value

TH1 = 0xDC; 

ET1 = 0; //Disable timer 1 interrupt

TR1 = 1; //Start timer 1    

    TI=1; //Send interrupt flag position 1

 

}


But sometimes, when using UART interrupt, the serial data transmission interrupt flag TI cannot be set to 1 for a long time. At this time, we need to rewrite the putchar function to redirect the output of the printf function, and then use printf to display it. For details, please refer to C: Keil/C51/LIB/PUTCHAR.C, the following is an example:


char putchar (char c) {

  while (!TI);

  TI = 0;

  return (SBUF = c);

}


Keywords:Keil  C51  Printf() Reference address:Using Printf() for serial port output in Keil C51

Previous article:The 51 MCU serial port prints garbled characters in both Chinese and English
Next article:51 MCU Getting Started Tutorial (0) —— Development Environment Configuration

Recommended ReadingLatest update time:2024-11-23 21:42

Using STlink II simulation debugging under Keil4 and Keil5
1. Close Keil software  2. Download the following compressed file STlink II related files, including three files ST-LINKII-KEIL.dll, StorAcc.dll and TOOLS.INI  3. Copy ST-LINKII-KEIL.dll and StorAcc.dll to the Keil\ARM\BIN directory.  4. Open the TOOLS.INI file, which is in the Keil directory. Add the following TDRV7=
[Microcontroller]
Using STlink II simulation debugging under Keil4 and Keil5
24CXX read and write driver C51 program
/**************************  Resources used in the file 1. Port: P0.2, P0.3 2. Call delay_ms function ******************************/ /************************  Port definition ************************/ sbit i2c_dat =P0^2; sbit i2c_clk =P0^3; #define IIC_TIME 10 //IIC operation delay //=======Set the chip address here
[Microcontroller]
C51 MCU Programming Skills: LCD1602 Programming Experience Sharing
Introduction: Let me first explain that the chip driver of the LCD1602 I am going to talk about below is HD44780. If your LCD1602 driver chip is not HD44780, then the following content is not applicable. This time I will share my LCD1602 programming experience: Let me first explain that the chip driver of the LCD160
[Microcontroller]
C51 MCU Programming Skills: LCD1602 Programming Experience Sharing
STM32_keil compiled memory size analysis
Program Size: Code=28784 RO-data=6480 RW-data=60 ZI-data=3900 Meaning 1. Code: The size of the FLASH space occupied by the program, stored in FLASH. 2. RO-data: Read-only-data, program-defined constants, stored in FLASH. 3. RW-data: Read-write-data, initialized variables, stored in SRAM. 4. ZI-data: Zero-Init-data,
[Microcontroller]
STM32_keil compiled memory size analysis
Performance comparison of AVR, C51 and PIC 8-bit microcontrollers
1. 51 Series   The most widely used eight-bit microcontroller is Intel's 51 series. Due to its reasonable hardware structure, standardized instruction system and long production history, it has the advantage of first-mover advantage. Many famous chip companies in the world have purchased the core patent technology of
[Microcontroller]
The difference between address latch 74ls373 and 273 and C51 microcontroller
74LS273    is an octal D flip-flop with common clock reset   74LS373    is a three-state in-phase octal D latch The pinouts of 273 and 373 are the same, the only difference is the functions of pins 1 and 11. For 273 (1). Pin 1 is reset CLR, which is valid at low level. When pin 1 is at low level, output pins 2 (
[Microcontroller]
8051 - 3 ways to add STC model in Keil
【1】Database replacement method (STC official website method): Note: This method is the simplest, but the disadvantage is that when selecting devices, you can only choose STC microcontrollers, and the original microcontrollers are gone, which is frustrating. Steps: First, back up the UV2.CDB (or UV3.CDB/UV4.CDB) file
[Microcontroller]
8051 - 3 ways to add STC model in Keil
Lesson 16 Use of C51 pointers
A pointer refers to the address of the storage area where the variable or data is located. For example, a character variable STR is stored at the address 51H in the DATA area of ​​the memory unit, then the address 51H in the DATA area is the pointer of the variable STR. Pointers are a very important concept in C langu
[Microcontroller]
Lesson 16 Use of C51 pointers
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号