For boards without emulators, the serial port is a very good debugging tool. You can set the variables you need to observe
Send it to the PC through the serial port for subsequent analysis and debugging. Here we mainly introduce how to use the serial port in Keil C
Debugger.
The program code is as follows:
#include
#include
void serial_init(void);
void main()
{
serial_init();
while(1)
{
printf("Hello,Serial Port\n";
}
}
void serial_init(void) //Serial port initialization function
{
TMOD = 0x20; //T1 working mode 1, used to set the baud rate
TH1 = 0xF3; //Baud rate 4800Bps, 12MHz crystal
TL1 = 0xF3;
PCON |= 0x80; //SMOD = 1, baud rate doubled
SCON = 0x50; //Allow sending and receiving
ES = 0; //Disable serial port interrupt
IF = 1;
TR1 = 1;
}
It should be noted that the microcontroller crystal oscillator is 12MHz (otherwise the baud rate setting will be incorrect).
Create a project, compile, enter the debugging interface, select "Peripheraals"-"Serial" in the menu, and you will see the serial port settings.
In the interface, we can see that before the serial port is initialized, the parameters of the serial port are all 0x00, and other options are not set.
Press F10 to enter single-step debugging and execute the serial_init function. We can see that the serial port has been initialized and the baud rate is 4806. After the serial port is initialized, we can use the printf function to output the information we need (note that the stdio.h file is required). Here, we only output a simple string information example. Select the menu "View"-"Serial Window #1", you can see a blank
area, which is where the serial port information is displayed. Execute the printf function and you can see the words "Hello, Serial Port" printed on the screen.
When debugging a program on actual hardware, we can use this method to observe the values of related variables and debug the program when we encounter problems. Once the program is OK, we can remove the printf function.
Previous article:12864 Chinese character library serial port test program
Next article:A Brief Discussion on Displaying Chinese Characters in 1602
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- The NiMH battery charging circuit is relatively complex, and some SMD components cannot be found in the specification sheet.
- Ding~ The weekly evaluation information has been delivered~
- Is there any plugin or function in KEIL or other compilation environment that can help check if there is an error like if ( a=1 )
- [RVB2601 Creative Application Development] + Indoor Environment Monitoring Platform
- CCS+C6678LE Ethernet interface test sample code
- Simple explanation of the principle of DDS in FPGA
- Problems with dsp2812 downloading using C2Prog
- EEWORLD University Hall----Computer Vision (Lu Peng, Beijing University of Posts and Telecommunications)
- Why do the 2D lines in the DXF exported from PADS become very thick after opening it in CAD?
- New ways to play with VL53Lxx time-of-flight sensors