cancel
Showing results for 
Search instead for 
Did you mean: 

Problem configuring USART transmitter, not showing data on hercules

Edizzon
Associate
I am a beginner, I am programming a stm32F411RE with Keil 5, when I was configuring the USART transmitter, Hercules and Arduino did not show any data, I reviewed several videos and I have the same problem, it has been several days. Thanks community.
#include <stm32f4xx.h>

void delay(int tiempo);
void USART2_Config();
void USART2_SendChar(uint8_t c);

int main(){
	USART2_Config();
	
	while(1){
	USART2_SendChar('A');
	delay(1000);
	}
	
}

void USART2_Config(){
	
	RCC->APB1ENR |= (1<<17); //Enable USART CLK
	RCC->AHB1ENR |= (1<<0);  //Enable GPIOA CLK
	
	GPIOA->MODER |= (2<<4);  //Alternate Function for Pin PA2 
	GPIOA->MODER |= (2<<6);  //Alternate Function for Pin PA3 
	
	GPIOA->OSPEEDR|=(3>>4)|(3>>6); // High Speed for PIN PA2 and PA3
	
	GPIOA->AFR[0] = (7<<8); // AF7 Alternate function USART2 at Pin PA2
	GPIOA->AFR[0] = (7<<12); // AF7 Alternate function USART2 at Pin PA3
	
	USART2->CR1 |= 0x00;   //  clear all
	USART2->CR1 |= (1<<13);//  USART ENABLE
	
	USART2->CR1 |= ~(0>>12); //M = 0; 8 bits word length
	
	USART2->BRR |= (8<<0)| (325<<4);  //  baud rate register 9600   
	USART2->CR1 |= (1<<2); //  RX
	USART2->CR1 |= (1<<3); //  TX
}


void USART2_SendChar(uint8_t c){
	USART2 -> DR = c;
	while(!(USART2->SR & (1<<6)));
}


void delay(int tiempo){
	int i;
	for(;tiempo>0;tiempo--){
		for(i=0;i<=3195;i++);
	}
}

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

Hello,

As you're a beginner, start by using HAL provided in STM32CubeF4 and inspire from this example https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM32F411RE-Nucleo/Examples/UART/UART_Printf

Then if all are good, switch to the optimization approach by directly access to the registers as you're doing as it could be a tricky approach from the beginning (forget to set a bit, wrong shift etc ..).

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

3 REPLIES 3

@Edizzon wrote:
Hercules and Arduino

How are the "Hercules and Arduino" connected to the stm32 ?

Have you used an oscilloscope to see if there is any activity at all at the TX pin?

Is the stm32F411RE on a standard ST board, or your custom design?

 

TDK
Guru

> GPIOA->OSPEEDR|=(3>>4)|(3>>6); // High Speed for PIN PA2 and PA3

This does nothing. 3 >> 4 and 3 >> 6 are both 0. You probably want << and not >>.

> USART2->CR1 |= 0x00; // clear all

This does not clear anything.

> USART2->BRR |= (8<<0)| (325<<4); // baud rate register 9600 

Seems off, if your system clock is 16 MHz. Should be 26667 if my math is correct.

 

Software-based delay loop is unpredictable and can be optimized out entirely. Marking the counter as "volatile int i" will help a bit.

If you feel a post has answered your question, please click "Accept as Solution".
SofLit
ST Employee

Hello,

As you're a beginner, start by using HAL provided in STM32CubeF4 and inspire from this example https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM32F411RE-Nucleo/Examples/UART/UART_Printf

Then if all are good, switch to the optimization approach by directly access to the registers as you're doing as it could be a tricky approach from the beginning (forget to set a bit, wrong shift etc ..).

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.