STM32—printf function redefinition

Publisher:dadigtLatest update time:2019-02-12 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  In order to facilitate debugging, we often need to use the printf function to print out debugging information. In Keil software, if you want to use the printf output function, please note that you need to select the use MicrolLIB option first, as shown below.


Write the picture description here


  Next, add the declaration of the printf function below the header file of the main function. The code is as follows:


/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "stm32f4xx_hal.h"


 // The added code is as follows, function refactoring

#ifdef __GNUC__ //gcc compiler macro definition

  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf

 set to 'Yes') calls __io_putchar() */

    #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

#else

    #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

#endif /* __GNUC__ */


/*The above means:

If the macro __GNUC__ is defined, that is, using the GCC compiler, then define the macro #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

If the macro __GNUC__ is not defined, that is, the GCC compiler is not used, then define the macro #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) */


//Add the implementation part of printf reconstruction function

PUTCHAR_PROTOTYPE

{

    HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF);

    return ch;

}


  Add a print output function in the while(1) loop of the main function to test the effect.


  while (1)

    {

        printf("printf teset\r\n");//print output

        HAL_Delay(1000); //Delay 1S

    }


  The test result is as shown in the figure below, which can realize the printing of printf function. 


Write the picture description here

Keywords:STM32 Reference address:STM32—printf function redefinition

Previous article:STM32—cubeMX+DMA+USART receives data of any length
Next article:Detailed explanation of time.h file in STM32

Recommended ReadingLatest update time:2024-11-16 13:58

STM32 dual stack and its use in uC/OS-II
Note: There may be many wrong ideas in it. I hope everyone will correct them in time after discovering them. First, let's learn about the dual stack. The following picture is from the "Cortex-M3 Definitive Guide". It's a bit boring, but you still need to read it.    Summarize: 1.
[Microcontroller]
STM32 dual stack and its use in uC/OS-II
Detailed explanation of the waveform of the output IO port based on the KEIL4 MDK software simulation of STM32
This is a simulation step for the PWM waveform output of the PA6 and PA7 ports of STM32. Other situations are similar... I have taken screenshots for each step. Please follow the steps one by one. Just slightly change some parameters according to your own situation. You will definitely be able to get the waveform! P
[Microcontroller]
STM32 USART DMA unknown length data reception and forwarding
Today, I finally realized that for data of unknown length, serial port 1 receives the command from the host computer, serial port 2 forwards the command to the module, and then receives the module data and sends it out through serial port 2. The entire serial port reception and transmission is completed by DMA, using
[Microcontroller]
STM32 sprintf function
Sprint function in single-chip microcomputer: Note 1: When using this function, you must include the stdio.h header file, otherwise the program will easily get stuck . Note 2: The difference between sprintf and printf functions: The two functions are similar, but the sprintf function prints to a string (converts the v
[Microcontroller]
STM32 - Array is defined as a pointer when it is used as the entry parameter of the called function
STM32-When an array is used as an entry parameter of a called function, two conditions are required. 1: Array first address 2: Array length In the called function, for example, if the data is saved in the array pbuffer , the entry parameter should be defined as a pointer u8 *pbuffer, 1 When calling, for example,
[Microcontroller]
STM32 - Array is defined as a pointer when it is used as the entry parameter of the called function
Design of STM32 software timer
  Timers are divided into hardware timers and software timers. Almost all microcontrollers are equipped with a limited number of hardware timers, that is, the controller itself has a dedicated module for timing. The working principle of almost all hardware timers is the same: the timer counts under the periodic pulse
[Microcontroller]
STM32 development board development notes (1) - overall image of the development board main control chip
A well-known person in the domestic industry wants to use stm32 to develop a product and is going to record the development process of this product. I have a stm32 development board bought from Taobao. The model of the controller IC is stm32f103vet6. Since ST has launched many products, Moreover, the name of each pr
[Microcontroller]
STM32 development board development notes (1) - overall image of the development board main control chip
Paired use of STM32 compilation instruction #pragma pack
#pragma pack can be used to specify the memory alignment value of the member variables of the data structure. The possible values ​​are: 1, 2, 4, 8, 16. Use the pack directive in pairs to avoid accidentally affecting the memory alignment of structure members in other source files in the project. If the memory al
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号