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
}
}
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
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Philips sells major stake in semiconductor unit for $6.4 billion
- TMS320F28335 Serial Port SCI
- Multi-channel current detection
- Diesel Engine Measurement and Control System Based on Virtual Instrument Technology
- 【Topmicro Intelligent Display Module】Part 4: Comprehensive evaluation of text scrolling, RTC, etc.
- Display QR code-Intelligent TFT module
- EEWORLD University ---- Open Source PWM Robotic Arm (STM32 Version) Video Tutorial
- Waveform changes of different ANT frequencies
- Regulator tube usage help
- [GD32L233C-START Review] IV. USART