As an embedded system, microcontroller usually needs to be debugged and output debugging information during the development process so that developers can find and solve problems in time. This article will introduce several commonly used methods for printing debugging information on microcontrollers.
1. Serial port output
Serial port output is the most commonly used method for microcontrollers to print debugging information. The microcontroller sends data to the computer through the serial port, and then receives and displays it on the computer. It should be noted that a serial port line is required to connect the microcontroller and the computer, and parameters such as baud rate, data bits, and stop bits also need to be set.
In the microcontroller program, you can use the printf function to send the debugging information that needs to be output to the serial port. For example, the following code snippet outputs an integer value to the serial port:
```c
#include #include int main() { int number = 123; char str[16]; sprintf(str, "num=%d", num); printf("%sn", str); return 0; } ``` This code uses the sprintf function to format the integer value num into a string, and then uses the printf function to send the string to the serial port. On the computer, you can use serial port debugging tools such as HyperTerminal to receive and display this string. 2. LED indicator output Some microcontrollers do not have serial port functions, or need to use the serial port for other debugging work. At this time, you can consider using LED indicators to output debugging information. The microcontroller can output various debugging information, such as error codes, status information, etc., by controlling the on and off of LED indicators. In the microcontroller program, you can use GPIO output control to control the LED indicator. For example, the following code snippet uses an LED indicator to output an error code: ```c #include #define LED_PIN GPIO_Pin_13 #define LED_PORT GPIOC int main() { uint8_t err_code = 0x01; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = LED_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(LED_PORT, &GPIO_InitStructure); while (1) { if (err_code) { GPIO_ResetBits(LED_PORT, LED_PIN); } else { GPIO_SetBits(LED_PORT, LED_PIN); } } return 0; } ``` This code uses GPIO output control to control the LED indicator light. If err_code is not 0, the LED indicator light turns on, otherwise the LED indicator light goes out. On the computer, you can judge the status and error code of the microcontroller by observing the status of the LED indicator light. 3. LCD display output LCD display is a common output device that can be used to display various debugging information, status information, etc. Some microcontrollers integrate an LCD display controller and can be directly connected to the LCD display for output. If the microcontroller does not have an integrated LCD display controller, the LCD output can be achieved by connecting an external LCD display controller. In the program of the microcontroller, you can use the LCD display control instructions to control the output of the LCD display. For example, the following code snippet outputs a string on the LCD display: ```c #include #include "lcd.h" int main() { char str[] = "Hello, world!"; LCD_Init(); LCD_WriteString(str); while (1) { // do nothing } return 0; } ``` This code calls the LCD_Init function to initialize the LCD display, and then calls the LCD_WriteString function to output a string. On the computer, you can judge the status and debugging information of the microcontroller by observing the content on the LCD display. 4. Buzzer output The buzzer is a common output device that can be used to emit various sounds to represent various status and debugging information. Some microcontrollers have integrated buzzer controllers that can directly control the buzzer for output. If the microcontroller does not have an integrated buzzer controller, the buzzer output can be achieved by connecting an external buzzer controller. In the program of the microcontroller, the buzzer control instruction can be used to control the output of the buzzer. For example, the following code snippet uses a buzzer to emit a beep sound: ```c #include #define BUZZER_PIN GPIO_Pin_1 #define BUZZER_PORT GPIOA int main() { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = BUZZER_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(BUZZER_PORT, &GPIO_InitStructure); while (1) { GPIO_SetBits(BUZZER_PORT, BUZZER_PIN); Delay_ms(100); GPIO_ResetBits(BUZZER_PORT, BUZZER_PIN); Delay_ms(100); } return 0; } ``` This code uses GPIO output control to control the output of the buzzer, which emits a beep every 100ms. On the computer, you can judge the status and debugging information of the microcontroller by hearing the beep sound. To sum up, there are many ways to print debugging information on a microcontroller, and you can choose the appropriate method according to specific needs and hardware conditions. Among them, serial port output is the most commonly used method, because it can not only output various debugging information, but also enable program debugging and connection of debugging tools. Other methods such as LED indicator output, LCD display output and buzzer output are more suitable for some specific scenarios and needs.
Previous article:Graduation Project | Whose university doesn’t have a perpetual calendar?
Next article:Implementation Analysis of 51 Microcontroller IIC Bus
- Popular Resources
- Popular amplifiers
- 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?
- Vicor high-performance power modules enable the development of low-altitude avionics and EVTOL
- Chuangshi Technology's first appearance at electronica 2024: accelerating the overseas expansion of domestic distributors
- Chuangshi Technology's first appearance at electronica 2024: accelerating the overseas expansion of domestic distributors
- "Cross-chip" quantum entanglement helps build more powerful quantum computing capabilities
- Ultrasound patch can continuously and noninvasively monitor blood pressure
- Ultrasound patch can continuously and noninvasively monitor blood pressure
- Europe's three largest chip giants re-examine their supply chains
- Europe's three largest chip giants re-examine their supply chains
- Breaking through the intelligent competition, Changan Automobile opens the "God's perspective"
- The world's first fully digital chassis, looking forward to the debut of the U7 PHEV and EV versions
- Ground Design Tips
- How to prevent audio equipment from aging and make the sound better
- Discussion on BQ35100 Li-ion Battery Capacity Monitoring
- Evaluation: STM32F769I-DISCO connected to Gizwits Cloud to realize IoT development remote control and other functions
- Application of 4G solar energy monitoring
- GD32E231 analog IIC driver LPS22HH
- Partition Design of Mixed Signal PCB
- Sharing some knowledge: CMOS meets TTL
- Analysis of the top ten popular microprocessor and memory new products in 2006
- Pressure sensor problem