cancel
Showing results for 
Search instead for 
Did you mean: 

Connecting 2x STM32 MCUs using UART

RSy.1
Associate III

Hello guys good day. I am trying to learn how to interface 2 STM32F103C8 MCUs using UART. Basically I would like to connect a push button to MCU-A then when this push button is pressed, the LED controlled by MCU-B will be activated. I am trying to search for some examples online but I could not find one. Can you give me some hints on how to do this? Thank you.

2 REPLIES 2

> I am trying to learn

Great.

Read the first 10 chapters in RM plus the USART chapter, read the datasheet and the errata. (USART=UART, i'll use the latter below)

Then, write blinky, ie. software which

  • enables GPIO port clock in RCC
  • configures the pin on which the LED is, to GPIO output
  • in a loop, sets the pin, calls a loopdelay long enough so you can see the blinking, clears the pin, and calls the loopdelay again

Then modify blinky so that it stops blinking when you press a button, i.e.

  • make sure you have in RCC enabled GPIO port clock of the GPIO port you use for the button
  • configure the pin on which the button is, as GPIO input, enable pullup on that pin
  • in the loop, when the GPIO input is cleared, wait

Then write software which configures UART and transmits some byte repeatedly, i.e.

  • enable in RCC clock for GPIO port where the UART pins are located
  • enable UART clock in RCC
  • in GPIO, set the UART Tx pin to AF, make sure in AFIO that it's properly remapped or not, as needed (AFIO remap is 'F1-specific, it's different in other STM32)
  • configure UART for required baudrate, enable Tx and enable UART
  • in a loop, check if Tx register is empty (by checking UART_SR.TXE bit) write some byte to the UART data register, and then call the loopdelay
  • using oscilloscope or logic analyzer, observe the UART Tx pin

Then write software which - on the second mcu, or on the same mcu in an other UART - receives data, i.e

  • perform the same steps as above to enable and configure the UART Rx pin and UART itself, enabling UART
  • perfrom the steps from the first post to configure GPIO pin on which the LED is located
  • in a loop, wait until UART_SR RXNE is set
  • read UART_DR, decide upon it whether LED has to be set to on or not

Or, you can click in CubeMX. or have a look at https://github.com/STMicroelectronics/STM32CubeF1/tree/master/Projects/STM32F103RB-Nucleo/Examples/UART/UART_TwoBoards_ComPolling (which is probably not CubeMX-generated). I don't use Cube.

JW

As always thank you Sir for your help :)