How to use printf() function in STM32

Publisher:LuckyDaisyLatest update time:2015-11-10 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
STM32 serial communication using printf to send data configuration method (development environment Keil RVMDK

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 function in the main file as follows:

// 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.

Keywords:STM32 Reference address:How to use printf() function in STM32

Previous article:STM32 learning experience (3)
Next article:The problem that STM32 cannot use printf() in MDK

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号