Lesson 6: Part 2: USART - Receiving Data From PC

In previous tutorial I am showing STM32F030 sending data and receive by PC. This tutorial will be working on the other way: PC sending data and receive by STM32F030. As USART is full duplex, while PC is sending data, STM32F030 also can send data simultaneously.

I have expand the code from previous tutorial to include the receiving section. And in this tutorial, we will be perform a character echo. Characters receive from PC will be transmit back to PC. Thus, we should expect character that we type in transmit section will be appear on receiving window of the software.

SFR Configurations

Refer to source code here for SFR configurations, all the related SFR on receiving is grouped in function name IntUSARTInit_RX in source file name int_usart.c.

Source Code Explanation

To avoid a blocking read that would occupied the whole MCU cycles, this example demonstrate the usage of interrupt (without DMA) on character receiving. 

When character is being received, interrupt routine ISR name USART1_IRQHandler will be called and this routine will read the character from buffer (USART1->RDR) and save into local buffer. Then the pointer PtrProd will be increment. The method use here is the producer/consumer model.

In the main program, the program will constant check if a data is available in buffer. When there is data available, a read from buffer will perform, then data read will be transmit out (using past tutorial).

Below is the video showing the code is running


No comments:

Post a Comment