Introducing several methods for printing debugging information from microcontrollers

Publisher:一直333Latest update time:2023-04-24 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Reference address:Introducing several methods for printing debugging information from microcontrollers

Previous article:Graduation Project | Whose university doesn’t have a perpetual calendar?
Next article:Implementation Analysis of 51 Microcontroller IIC Bus

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号