1. Before using UART, the corresponding peripheral clock must be started, which mainly uses the RCC_APBnPeriphClockCmd function of the firmware library.
Enable UART1: Use RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE)
Enable UART2: use RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE)
2. To use interrupts for UART operations, you need to configure NVIC and set the interrupt priority. For example:
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
3. Configure the corresponding GPIO port.
If the system's UART needs to be remapped, the GPIO_PinRemapConfig function needs to be used for remapping, such as: GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Note: Rx is floating, Tx is the second function pull-up.
Configure Rx to floating input mode and Tx to the second power mode with pull-up. And initialize it with GPIO_Init() function. For example:
/* Configure USART2 Rx PA3 input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx (PA.09) as alternate push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
4. Configure UART
After configuring the correct external crystal oscillator in the conf file, directly write the UART baud rate, communication frame length, mode, hardware communication control, and transceiver mode into the structure defined by USART_InitTypeDef. Then use USART_Init() to initialize. For example:
USART_InitStructure.USART_BaudRate = 9600;
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;
/* Configure USART1 */
USART_Init(USART1, &USART_InitStructure);[page]
Then enable the transmit and receive interrupts. For example:
/* Enable USART1 Receive and Transmit interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
// USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
Note: Generally, the TXE interrupt is not enabled. Once this interrupt is enabled, if the UART send buffer is empty, the UART interrupt will be entered immediately. Therefore, the TXE interrupt can be enabled in the program where data needs to be sent. Use USART_SendData() to send data in the UART interrupt.
After enabling the interrupt, you also need to enable the UART port:
like:
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
/* Enable the USART2 */
USART_Cmd(USART2, ENABLE);
The interrupt program (stm32f10x_it.c) can complete the sending as follows: Note that all serial port interrupts need to determine the interrupt source in the interrupt service program for separate processing.
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1);
if(RxCounter1 == NbrOfDataToRead1)
{
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); //After sending, disable RXNE.
}
}
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
USART_SendData(USART1, TxBuffer1[TxCounter1++]);
if(TxCounter1 == NbrOfDataToTransfer1)
{
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
}
}
Previous article:How to set the timer in STM32
Next article:Interrupt mechanism of STM32 microcontroller
Recommended ReadingLatest update time:2024-11-16 22:33
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
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
- PLC online monitoring control cloud configuration software SCADA
- Is this dime-sized millimeter-wave sensor evaluation board worth trying?
- Xintang M2354 Fault Injection Attack Protection
- Date in spring - go to the beach and relax
- Sub-library: BlueCoin Development Platform
- [Atria AT32WB415 Review] 4. Implementation of printf under AT32 IDE (printf redirection)
- Power amplifier driving capacitive load case sharing
- Power supply zero and live wire detection
- RSL1 Bluetooth characteristic value reading and writing
- Sapphire on PCB---DFM about optical positioning points