STM32 serial communication uses printf to send data configuration method

Publisher:古宝奇缘Latest update time:2016-06-13 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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 and define it in the 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:STM32 serial communication uses printf to send data configuration method

Previous article:Regular garbled characters encountered in STM32 serial communication
Next article:Analysis process of the first byte loss problem of STM32 serial port

Recommended ReadingLatest update time:2024-11-16 15:57

Solution to the problem that STM32 USB NAND Flash simulated U disk cannot be formatted
A few days ago, I was looking for the problem that the NAND Flash emulation USB disk program could not be formatted. On the night of the Mid-Autumn Festival, I was still working hard in the laboratory to adjust the code. Maybe it was because of the big full moon in Hangzhou, I felt particularly excited tonight, and my
[Microcontroller]
Use of STM32 ADC and internal temperature sensor
  STM32 comes with 1-3 ADC modules, and the sampling accuracy reaches 12 bits, which is a small upgrade compared to the 10 bits of the AVR microcontroller used in those days. This test program uses the ADC DMA interrupt method, so that the CPU can hand over the ADC task to the hardworking DMA. When the DMA completes a
[Microcontroller]
TinyOS transplantation method based on STM32 and CC2520
    The TinyOS system has become the most popular wireless sensor network operating system with its component structure model, event-driven, and concurrent advantages. However, TinyOS does not support STM32 and CC2520 chips. Therefore, based on the analysis of the basic principles of TinyOS, the implementation mechani
[Microcontroller]
TinyOS transplantation method based on STM32 and CC2520
45. Basic principles of serial port IAP
Before learning this tutorial, you first need to understand the programming principles of Flash. 1. Introduction to stm32 IAP 1. Programming method of stm32 2. STM32 boot mode selection BOOT1 is set to 0, BOOT0 is set to 1, and the program executes the startup program BootLoader code from the system memory,
[Microcontroller]
45. Basic principles of serial port IAP
STM32 interrupt learning summary
After two days, I can finally understand 32 interrupts. Because it is operated by library functions, some internal knowledge is not understood in detail, but I just understand that it is like this. But for me who want to do simple development, this is enough. I like to learn from a routine. The following program is
[Microcontroller]
Detailed introduction and technical knowledge of STM32 microcontroller
The core Cortex-M3 processor of STM32 is a standardized microcontroller structure. I hope you will think about what standardization means. In short, the Cortex-M3 processor has a 32-bit CPU, a parallel bus structure, a nested interrupt vector control unit, a debugging system, and a standard memory map. The Neste
[Microcontroller]
Detailed introduction and technical knowledge of STM32 microcontroller
Using STM32 general-purpose timer to realize output of two complementary PWM with adjustable duty cycle and frequency
MCU:STM32F334C8T6 PWM, or pulse width modulation, can be used to drive motors, full-bridge circuits, etc. Those who have used STM32 know that its timer can easily achieve PWM output, and the advanced timer's TIMx_CHy and TIMx_CHyN can easily achieve the output of complementary PWM waveforms. Advanced timer resourc
[Microcontroller]
Using STM32 general-purpose timer to realize output of two complementary PWM with adjustable duty cycle and frequency
STM32 interrupts and nested NVIC quick start
A quick introduction to STM32 interrupts and nested NVIC. I also learned it by reading this book: Cortex-M3 Definitive Guide by Joseph Yiu Translated by Song Yan It’s actually very simple. //CM3 has up to 240 interrupts (usually external interrupts are written as IRQs), which is what the software calls IRQ CHANAE
[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号