Professional Documents
Culture Documents
Example
The STM32 USART_Irq example program shows how to configure and use the USART1 of
STMicroelectronics STM32F103xx microcontroller in interrupt driven mode.
The configuration of USART1 is 9600 Baud, 8 data bits, 1 stop bit, no parity and no flow control.
Retargetting is used to read a character over the serial input and to print out a string to the serial
output. A buffer handling is implemented for transmit / receive data.
The configuration of the clocks and USART is done using the Configuration Wizard in
file STM32_Init.c.
Two Vision targets are available:
Connect TX:
GPIO_PinAFConfig (GPIO X, GPIO_PinSource Y, GPIO_AF);
Connect RX:
GPIO_PinAFConfig (GPIO X, GPIO_PinSource Y, GPIO_AF);
4 GPIO configuration of the TX and RX:
TX configuration:
GPIO_OType = GPIO_OType_PP;
GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Mode = GPIO_Mode_AF;
GPIO_Pin = GPIO_Pin_ XX;
GPIO_Speed ??= GPIO_Speed_50MHz;
RX configuration:
GPIO_Mode = GPIO_Mode_AF;
GPIO_Pin = GPIO_Pin_ XX;
5, the serial port initialization:
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 (USARTx, & USART_InitStructure);
6, such as the need to use an interrupt, you need to interrupt priority configurations:
The following is configuration UART5 interrupt 0 priority group, to seize the priority and
sub-priority 0.
void NVIC_Configuration (void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/ * Configure the NVIC Preemption Priority Bits * /
/ * Configure one bit for preemption priority * /
/ * Priority group illustrates the preemption priority number of bits used, and child priority number of
bits used
1, 7 * /
NVIC_PriorityGroupConfig (NVIC_PriorityGroup_0);
/ * Enable the USART1 Interrupt * /
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; / / set the serial port 5 interrupt
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; / / preemption
priority 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; / / sub-priority 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; / / Enable
NVIC_Init (& NVIC_InitStructure);
}
7, select and enable the corresponding interrupt:
USART_ITConfig (USART1, USART_IT_RXNE, ENABLE); / / enable receive interrupt
Etc.;
8 Enable USART:
USART_Cmd (USARTx, ENABLE);