The previous few articles introduced the three parts of clock, GPIO, and interrupt. Next, we will introduce the commonly used debugging serial port configuration:
1. Hardware interface introduction: USART1 is used as the printing serial port, where PA9--------TX
PA10------RX
3. Set the interrupt grouping. For details on how to set the interrupt grouping, please refer to the previous article.
http://blog.csdn.net/u014449366/article/details/52717299
void UART_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //Channel is set to serial port 1 interrupt
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Interrupt response priority 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable interrupt
NVIC_Init(&NVIC_InitStructure); //Initialization
}
4. The interrupt configuration function mainly turns on the corresponding clock, configures the multiplexed pins, sets the baud rate and data format, turns on the receiving interrupt, and enables the serial port.
/****************************************************
Function name: USART1_Configuration
Input:
Output:
Function description:
Initialize serial port hardware device and enable interrupt
Configuration steps:
(1) Turn on GPIO and USART1 clock
(2) Set the GPIO mode of two pins of USART1
(3) Configure USART1 data format, baud rate and other parameters
(4) Enable USART1 receive interrupt function
(5) Finally enable USART1 function
********************************************************/
void USART1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Step 1: Turn on the clock of GPIO and USART components*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Step 2: Configure the GPIO of USART Tx to push-pull multiplexing mode*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Step 3: Configure the GPIO of USART Rx to floating input mode
Since the GPIO defaults to floating input mode after CPU reset, the following step is not necessary.
However, I still recommend adding it for easy reading and to prevent other places from modifying the setting parameters of this port line
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Step 3 has been done, so this step can be skipped
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
*/
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Step 4: Configure USART1 parameters
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 115200;
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;
USART_Init(USART1, &USART_InitStructure);
/* If the receive data register is full, an interrupt is generated*/
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Step 5: Enable USART1, configuration completed*/
USART_Cmd(USART1, ENABLE);
/* The following statement solves the problem that the first byte cannot be sent correctly*/
USART_ClearFlag(USART1, USART_FLAG_TC); // Clear flag
}
5. Write a function to send bytes through the serial port
void Uart1_PutChar(u8 ch)
{
USART_SendData(USART1, (u8) ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
6. Write the serial port data receiving interrupt function
void USART1_IRQHandler(void) //In the interrupt service routine, since the host does not know which interrupt source issued the interrupt request when responding to the interrupt, the interrupt source must be identified in the interrupt service routine and then processed separately. Of course, if only one interrupt request is involved, the above identification is not necessary. But no matter what the situation, it is a good habit to make the above identification
{
u8 dat;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //If the receive data register is full
{
dat = USART_ReceiveData(USART1);
if(dat == 0x00)
{
dat = 0;
GPIO_ResetBits(GPIOB, GPIO_Pin_0); //Light up LED0
Uart1_PutChar(0x00);
}
}
}
7. In the actual test, when 00 is sent in hexadecimal through the serial port tool, the LED is lit and the host computer receives 00.
Previous article:Detailed explanation of STM32 system clock settings
Next article:STM32 interrupt configuration
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
- Application of servo motor in automatic control
- MSP430G2452 implements heart rate monitoring based on ECG
- 【BLE 5.3 wireless MCU CH582】10. BLE broadcaster role
- Complete Flash Recording in CCS v5
- The problem of open circuit at the input end of the same-phase amplifier circuit and output oscillation
- Dear hardware engineers, can you solve differential equations?
- Temperature Problem Solved for You (V) Efficient Cold Chain Management through Scalable Temperature Sensors
- ARM CORTEX-M3 core architecture understanding summary
- Introduction to TI's three-phase Vienna PFC solution
- FPGA PLL Loss of Lock