STM32F103 serial port configuration and printing using printf

Publisher:幸福如意Latest update time:2019-01-29 Source: eefocusKeywords:STM32F103 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

First, configure the serial port clock:


    // USART1 clock enable 

    RCC_APB2PeriphClockCmd(

    RCC_APB2Periph_USART1 |

    RCC_APB2Periph_GPIOA |

    RCC_APB2Periph_AFIO, ENABLE);

Then configure the port, configure the serial port, and enable it.


 


void USART1_Init(void)

{

/////// config the gpio

    GPIO_InitTypeDef GPIO_InitStructure;

    

    /* PA9 USART1_Tx */ 

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //Configure the send port

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 

    GPIO_Init(GPIOA, &GPIO_InitStructure); 

    /* PA10 USART1_Rx  */ 

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //Configure the receiving port

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;// 

    GPIO_Init(GPIOA, &GPIO_InitStructure);

    

//////////////

    

    USART_InitTypeDef USART_InitStructure;//

 

    USART_InitStructure.USART_BaudRate = 115200; //Configure baud rate

    USART_InitStructure.USART_WordLength = USART_WordLength_8b;//

    USART_InitStructure.USART_StopBits = USART_StopBits_1;// 

    USART_InitStructure.USART_Parity = USART_Parity_No;//

    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//

    

    //config clock

    USART_ClockInitTypeDef   USART_ClockInitStructure;

    

    USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;     

    USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;      

 

    USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;      

    USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; 

    

    USART_ClockInit(USART1, &USART_ClockInitStructure);

 

    /* Configure USART1 */ 

    USART_Init(USART1, &USART_InitStructure);//

    

    /* Enable the USART1 */ 

    USART_Cmd(USART1, ENABLE);//??1? 

    

}

The serial port is configured. In order to use printf for printing, it needs to be redirected:


Add the following code in stm32f10x_usart.c:


int fputc(int ch, FILE *f)

{

 /* Write a character to USART */

 USART_SendData(USART1, (uint8_t) ch);

 

 /* Loop until sending is complete*/

 while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);

 

 return ch;

}

Add the stdio.h header file to stm32f10x_usart.h, and then add the declaration int fputc(int ch, FILE *f);


Finally, check "Use MicroLIB" under Target in the settings.


Keywords:STM32F103 Reference address:STM32F103 serial port configuration and printing using printf

Previous article:stm32 printf function redirection
Next article:Summary of STM32 library function programming ideas and comparison with register programming

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

Setting and using the touch screen of STM32f103
The touch screen solution is adopted for human-computer interaction of the multi-functional acquisition and display platform. The touch screen function mainly relies on the resistive touch screen. The main part of the touch screen is a resistive film screen that matches the display surface very well. This is a multi-
[Microcontroller]
Setting and using the touch screen of STM32f103
STM32F103 remaps the JTAG port to a normal GPIO
For beginners of Mini STM32, why can't the output be controlled when using PB3 and PB4? First, after the STM32F10x series MCU is reset, PA13/14/15 & PB3/4 are configured as JTAG ports by default. Sometimes, in order to make full use of the resources of the MCU I/O port, we will set these ports as normal I/O ports. The
[Microcontroller]
STM32F103RCT6+USART3+UART5 initialization code
The difference between USART and UART is that USART supports synchronous transmission and reception, while UART only supports asynchronous transmission and reception. Synchronous mode: USART needs a synchronous signal USART_CK. Usually, the synchronous signal is rarely used. Therefore, the general microcontroller UA
[Microcontroller]
ADC multi-channel acquisition program for STM32f103 digital electrical acquisition circuit
STM32 has 1~3 ADCs (STM32F101/102 series has only 1 ADC), which can be used independently or in dual mode (increasing the sampling rate). The ADC of STM32 is a 12-bit successive approximation analog-to-digital converter. It has 18 channels and can measure 16 external and 2 internal signal sources. The A/D conversion o
[Microcontroller]
ADC multi-channel acquisition program for STM32f103 digital electrical acquisition circuit
STM32F103 controls AD7606 to collect analog signals
1、配置STM32F103的SPI口 void AD7606_Port_Init(void) { GPIO_InitTypeDef  GPIO_InitStructure; SPI_InitTypeDef   SPI_InitStructure; RCC_APB2PeriphClockCmd(AD_SPI_CS_GPIO_CLK | AD_SPI_MISO_GPIO_CLK | AD_SPI_SCK_GPIO_CLK, ENABLE); RCC_APB1PeriphClockCmd(AD_SPI_CLK, ENABLE);  /////////////////////////SPI_CLK/////////////////////
[Microcontroller]
STM8 uses printf instead of uart method
Add in the program: (the header file needs to add #include )  #ifdef __GNUC__  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)  #else  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)  #endif   PUTCHAR_PROTOTYPE  {  //Send data code return ch;  }    The message identifier "FILE" is undefined appear
[Microcontroller]
STM32F103ZET6 general timer single pulse mode experiment
Because the core board I bought before always had power supply problems, my current project has changed to the F103ZET6 microcontroller I used before! 1. Purpose of the experiment 1) Generate a single pulse with adjustable pulse width (within the allowed range) 2. Hardware: General Timer 3, General Timer 4 3. In
[Microcontroller]
STM32F103ZET6 general timer single pulse mode experiment
【STM32F103】Key detection (GPIO input)
Button hardware circuit: Analysis circuit: When button K1 is pressed, the high level 3.3V is connected. In order to protect GPIO, a current limiting resistor (R7) is added. When it is not pressed, it is grounded and the rising edge is input. PA0 has the function of automatic wake-up (must be awakened by rising ed
[Microcontroller]
【STM32F103】Key detection (GPIO input)
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号