cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to use the STM32F303R8 Nucleo board USART2 for receiving data is not working.

JSamu.2
Associate II

In the code I wrote, it is very straightforward being initialising USART2, receiving a character from a serial monitor and turning on/off an LED based on the character received. The LED does not turn on, in this case when I type the letter 'y' on putty or any other terminal.

With this being said, if I write a simple echo program using USART2 to receive a character from the terminal and send the same character back it works. So i'm not sure why when i'm trying to receive a char without transmitting anything it doesn't work.

#include "stm32f303xe.h"
 
void USART2_Init(void);
void GPIO_LED_Init(void);
char USART_Read(void);
 
int main ()
{
	//int ch = 0;
	USART2_Init();
	GPIO_LED_Init();
	
	while (1)
	{
		//ch = USART_Read();
		if (USART_Read() == 'y')
			GPIOA->BSRR = (1 << 5);
		else
		{
			//GPIOA->ODR ^= (1 << 5);
			GPIOA->BSRR = (1 << 21);
			for (int i=0; i<5000; i++);
		}
	}
}
 
void USART2_Init(void)
{
	// Enable USART 2
	RCC->APB1ENR |= (1 << 17);
		
	// Turn on Port A clock for USART pins PA2 (TX) and PA3 (RX)
	RCC->AHBENR |= (1 << 17);
	
	// Set PA2 (TX) and PA3 (RX) as Alternate Functions
	GPIOA->MODER |= (0x2 << 6) | (0x2 << 4);
	
	// Conifgure Alternate Function Register for PA2 and PA3
	GPIOA->AFR[0] |= (0x7 << 12) | (0x7 << 8);	
	
	USART2->BRR = 0x341;			// Set baud rate to 9600 (8Mhz/9600)
	USART2->CR1 |= (1 << 2);	// Enable reciever
	USART2->CR1 |= (1 << 0);	// Enable USART2
}
 
void GPIO_LED_Init(void)
{
	// Turn on clock for GPIO A (LED is connected to PA5)
	//RCC->AHBENR |= (1 << 17);
 
	// configure PA5 to General Purpose Output Mode 
	GPIOA->MODER |= (~(1 << 11)) | (1 << 10);
	
}
char USART_Read(void)
{
	while (!(USART2->ISR & (1 << 5))){} // Check to see if recieve data register is empty
		return USART2->RDR; // Load TX data register with ch
}

1 REPLY 1
KiptonM
Lead

I believe USART2 is connected to the debugger. None of mine work either which seems weird they would use that port for the debugging and put it as the UART for the Arduino Shield.