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.
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
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Internet of Things Development Technology Based on C Language (Edited by Gao Song)
- The STM32 MCU drives the BMP280 absolute pressure sensor program and has been debugged
- DigiKey \"Smart Manufacturing, Non-stop Happiness\" Creative Competition - Small Weather Station - STM32F103 Sensor Driver Code
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Award-winning live broadcast: Application of Infineon gate driver chips and corresponding solutions of Avnet are now open for registration...
- Characterization of Integrated RF Hardware in 5G Applications
- How does TTL inverter work?
- Giant microbit (giga:bit)
- Renesas CPK-RA6M4 Development Board Review + Unboxing and Building Environment
- Has anyone used the ionizing radiation sensor module? Discuss some technical issues
- RS-485 communication interface encoder
- FPGA Implementation of Fully Parallel FFT
- RT-Thread has been ported to W600
- MakeCode now supports STM103