LESSON 07: UART COMMUNICATION WITH STM32

posted in: Microcontroller | 1

1. Introduction to UART

Universal Asynchronous Receiver/Transmister (UART) is the asynchronous communication standard for microcontrollers and other peripherals.

UART is a point-to-point communication standard, that is, in a network, only one device plays the role of Transmister and one device plays the role of Reciver.

UART is an asynchronous communication protocol, meaning that there is no common clock, devices can understand each other by setting the same parameters.

UART is full duplex communication, meaning that it is possible to transmit and receive at the same time.

Frame Format:

  • Start is the first bit to be transmitted in a transmitted frame, this bit is used to notify the device that a data packet is about to be transmitted. Start is a required bit in the transmission frame, it is a low bit (0).
  • Data or data to be transmitted. The least significant bit (LSB – Least Significant Bit, right bit) of the data will be transmitted first and last is the most significant bit (MSB – Most Significant Bit, left bit).
  • Parity is the bit used to check whether the transmitted data is correct (relatively). There are two types of parity, even parity and odd parity. Even parity means that the number of “1s” in the data including the parity bit is always even. Conversely, the total number of “1s” in odd parity is always odd. Parity bit is not required bit and so we can remove this bit from transmission frame.
  • Stop bits are one or more bits that indicate to the receiving device that a packet has been sent. Stop bits are required bits, high bit (1).

STM32F103C8 has 3 UART sets with many operating modes, with many UART sets we can use many different transmission and reception applications.

In this article, I will use Hercules Terminal software and CP2102 USB 2.0 module to transmit signals from the led control computer on the Blue Pill board.

2 . UART configuration with ST library

  • Function to set parameters for UART in file uart.c
void USARTx_Init(USART_TypeDef* USARTx, USART_PinsPack_t pinspack, uint32_t baudrate) {

	USART_InitTypeDef 	USART_InitStruct;
	NVIC_InitTypeDef	NVIC_InitStruct;


	USART_InitStruct.USART_BaudRate = baudrate;
	
#ifdef USE_USART1
	if (USARTx == USART1) {
	USART1_InitPins(pinspack);
	NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
		
	USART_InitStruct.USART_HardwareFlowControl =                      USART1_HARDWARE_FLOW_CONTROL;
	USART_InitStruct.USART_Mode = USART1_MODE;
	USART_InitStruct.USART_Parity = USART1_PARITY;
	USART_InitStruct.USART_StopBits = USART1_STOP_BITS;
	USART_InitStruct.USART_WordLength = USART1_WORD_LENGTH;
	}
#endif
#ifdef USE_USART2
	if (USARTx == USART2) {
	USART2_InitPins(pinspack);
	NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;

	USART_InitStruct.USART_HardwareFlowControl = USART2_HARDWARE_FLOW_CONTROL;
	USART_InitStruct.USART_Mode = USART2_MODE;
	USART_InitStruct.USART_Parity = USART2_PARITY;
	USART_InitStruct.USART_StopBits = USART2_STOP_BITS;
	USART_InitStruct.USART_WordLength = USART2_WORD_LENGTH;
	}
#endif
#ifdef USE_USART3
	if (USARTx == USART3) {
	USART3_InitPins(pinspack);
	NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn;
	USART_InitStruct.USART_HardwareFlowControl = USART3_HARDWARE_FLOW_CONTROL;
	USART_InitStruct.USART_Mode = USART3_MODE;
	USART_InitStruct.USART_Parity = USART3_PARITY;
	USART_InitStruct.USART_StopBits = USART3_STOP_BITS;
	USART_InitStruct.USART_WordLength = USART3_WORD_LENGTH;
	}
#endif
	/* Disable */
	USART_Cmd(USARTx, DISABLE);
	/* Deinit first */
	USART_DeInit(USARTx);
	
	/* Init */
	USART_Init(USARTx, &USART_InitStruct);
	/* Enable */
	USART_Cmd(USARTx, ENABLE);
	
	/* Enable RX interrupt */
	USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);

	 /* Initialize NVIC*/
	 
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = USART_NVIC_PRIORITY;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority =                       USART_INT_GetSubPriority(USARTx);
	NVIC_Init(&NVIC_InitStruct);
}
  • Function to receive characters passed in file uart.c
uint8_t USART_Getc(USART_TypeDef* USARTx) {
	int8_t c = 0;
	USART_t* u = USART_INT_GetUsart(USARTx);
	
	/* Check if we have any data in buffer */
	if (u->Num > 0) {
		if (u->Out == u->Size) {
			u->Out = 0;
		}
		c = *(u->Buffer + u->Out);
		u->Out++;
		u->Num--;
	}
	return c;
}
  • Main program in function main.c hàm
 
 int main(void)
 {	
  u8 c=0;

    /* cau hình PC13 output */
   pinMode(PC13,OUTPUT);
   /* Cau hình UART 1*/
   USARTx_Init(USART1, Pins_PA9PA10, 115200);
  /* Bat Led*/		
  digitalWrite(PC13,LOW);
  while(1)
	{
         /* Gan ky tu nhan duoc cho c */
         c=USART_Getc(USART1);
  	 if(c == '1') 
	 {
	 digitalWrite(PC13,LOW);
	 }
	 else if (c == '0')
	 {
	 digitalWrite(PC13,HIGH);
	 }
        }
 }

Sample code link

Leave a Reply

Your email address will not be published.