You are on page 1of 5

STM32 USART (interrupt mode)

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:

Simulator where the program runs in the software simulator.


MCBSTM32 where the program runs from internal Flash located on the microcontroller.

A series STM32F2 USART learning


Tag: up to, cmd, function the, c, STM32 learning Category: C Author: EVERLEY Date: 2010-03-22
The F2 Series USART initialization pay attention:

Need peripheral File:


stm32f2xx_usart.c; / / serial port corresponding function
stm32f2xx_gpio.c; / / GPIO initialization
stm32f2xx_rcc.c; / / clock initialization
misc.c; / / interrupt initialization
The initialization mainly include eight parts.
1 Enable GPIO clock:
RCC_AHB1PeriphClockCmd (GPIO X, ENABLE);
2, enabling the UART clock:

RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART X, ENABLE);


3 GPIO pins connected to the USART X:
Original function: GPIO_PinAFConfig (GPIO_TypeDef * GPIOx, the uint16_t GPIO_PinSource,
uint8_t GPIO_AF);
Optional follows GPIO_AF:
GPIO_AF_RTC_50Hz;

GPIO_AF_MCO; GPIO_AF_TAMPER; GPIO_AF_SWJ;

GPIO_AF_TRACE; GPIO_AF_TIM1; GPIO_AF_TIM2; GPIO_AF_TIM3;


GPIO_AF_TIM4; GPIO_AF_TIM5; GPIO_AF_TIM8; GPIO_AF_TIM9;
GPIO_AF_TIM10; GPIO_AF_TIM11; GPIO_AF_I2C1; GPIO_AF_I2C2;
GPIO_AF_I2C3; GPIO_AF_SPI1; GPIO_AF_SPI2; GPIO_AF_SPI3;
GPIO_AF_USART1; GPIO_AF_USART2; GPIO_AF_USART3; GPIO_AF_UART4;
GPIO_AF_UART5; GPIO_AF_USART6; GPIO_AF_CAN1; GPIO_AF_CAN2; GPIO_AF_TIM12;
GPIO_AF_TIM13; GPIO_AF_TIM14; GPIO_AF_OTG1_FS; GPIO_AF_OTG2_HS; GPIO_AF_ETH;
GPIO_AF_FSMC; GPIO_AF_OTG2_FS; GPIO_AF_SDIO; GPIO_AF_DCMI; GPIO_AF_EVENTOUT;

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

USART_ITConfig (USART1, USART_IT_TXE, ENABLE); / / Enable the transmit buffer


empty interrupt

USART_ITConfig (USART1, USART_IT_TC, ENABLE); / / Enable the transmit complete


interrupt

Etc.;
8 Enable USART:
USART_Cmd (USARTx, ENABLE);

The following procedure is the official website for initialization.


void STM_EVAL_COMInit (COM_TypeDef COM, USART_InitTypeDef *
USART_InitStruct)
{
GPIO_InitTypeDef GPIO_InitStructure;
/ * Enable GPIO clock * /
RCC_AHB1PeriphClockCmd (COM_TX_PORT_CLK [COM] | COM_RX_PORT_CLK
[COM], ENABLE);
if (COM == COM1)
{
/ * Enable UART clock * /
RCC_APB1PeriphClockCmd (COM_USART_CLK [COM], ENABLE);
}
/ * Connect PXx to USARTx_Tx * /

GPIO_PinAFConfig (COM_TX_PORT [COM], COM_TX_PIN_SOURCE [COM],


COM_TX_AF [COM]);
/ * Connect PXx to USARTx_Rx * /
GPIO_PinAFConfig (COM_RX_PORT [COM], COM_RX_PIN_SOURCE [COM],
COM_RX_AF [COM]);
/ * Configure USART Tx as alternate function * /
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = COM_TX_PIN [COM];
GPIO_InitStructure.GPIO_Speed ??= GPIO_Speed_50MHz;
GPIO_Init (COM_TX_PORT [COM], & GPIO_InitStructure);
/ * Configure USART Rx as alternate function * /
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = COM_RX_PIN [COM];
GPIO_Init (COM_RX_PORT [COM], & GPIO_InitStructure);
/ * USART configuration * /
USART_Init (COM_USART [COM], USART_InitStruct);
/ * Enable USART * /
USART_Cmd (COM_USART [COM], ENABLE);
}

You might also like