It is very convenient to use printf to send data in the STM32 serial communication program. However, there are always problems when using it for the first time. The most common problem is that the main function cannot be entered during hardware access. In fact, it only needs a simple configuration.
Let's talk about what configurations are needed to use printf.
There are two configuration methods:
1. Configure the project properties. The detailed steps are as follows
1. First, include "stdio.h" (standard input and output header file) in your main file.
2. Redefine the
// send data
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (unsigned char) ch); // USART1 can be replaced by USART2, etc.
while (!(USART1->SR & USART_FLAG_TXE));
return (ch);
}
// Receive data
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
In this way, when using printf, the custom fputc function will be called to send characters.
3. In the "Target" -> "Code Generation" option of the project properties, check "Use MicroLIB"
MicroLIB is the default C backup library, and detailed information about it can be found on the Internet.
The configuration is now complete, and you can use printf to send data to the serial port at will in the project.
2. The second method is to add the "Regtarge.c" file to the project
1. Include the "stdio.h" file in the main file
2. Create a file in the project and save it as Regtarge.c, then add it to the project
Enter the following content in the file (just copy it)
#include
#include
#pragma import(__use_no_semihosting_swi)
extern int SendChar(int ch); // declare external function, defined in main file
extern int GetKey(void);
struct __FILE {
int handle; // Add whatever you need here
};
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f) {
return (SendChar(ch));
}
int fgetc(FILE *f) {
return (SendChar(GetKey()));
}
void _ttywrch(int ch) {
SendChar (ch);
}
int ferror(FILE *f) { // Your implementation of ferror
return EOF;
}
void _sys_exit(int return_code) {
label: goto label; // endless loop
}
3. Add the following two functions to the main file
int SendChar (int ch) {
while (!(USART1->SR & USART_FLAG_TXE)); // USART1 can be replaced with the serial port used in your program
USART1->DR = (ch & 0x1FF);
return (ch);
}
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
The configuration is now complete and you can use printf at will in the main file.
Previous article:STM32 learning experience (3)
Next article:The problem that STM32 cannot use printf() in MDK
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- Please tell me what's going on.
- KEE Master Classroom: Verify the RF properties of IoT devices during manufacturing faster and easier
- How to create a new TMS320F28379D project in CCS
- I'm a novice and I need help on how to install Grace on CCS10. Thanks in advance.
- DCA1000EVM User Guide
- When designing Cadence PCB, after setting the large cross cursor, the cursor cannot be seen. When setting it to a small cursor, it is normal. What is the reason?
- 【GD32L233C-START Review】1. Getting to know GD32L233C-START
- 【Share】F28002x Introduction
- Does learning Verilog / FPGA still require training?
- EEWORLD University Hall----Understand the mysterious face of uboot