Skip to main content
MRamy.1
Associate II
December 12, 2019
Solved

Can't send data through the USART Tx Pin in the STM32F411E-Disco Board

  • December 12, 2019
  • 1 reply
  • 1687 views
#include "stm32f4xx.h" // Device header
 
void USART2_Init(void);
void USART2_Write(int ch);
void delayMS(int delay);
 
int main(void){
	
	USART2_Init();
	while(1){
		USART2_Write('H');
		USART2_Write('i');
		delayMS(1000);
	}
}
 
void USART2_Init(void){
	
	RCC->APB1ENR |= 0x00020000;
	RCC->AHB1ENR |= 0x00000001;
	
	GPIOA->AFR[1] = 0x00000070;
	GPIOA->MODER |= 0x00000020;
	
	USART2->BRR = 0x342;
	USART2->CR1 |= 0x00000008;
	USART2->CR1 |= 0x00002000;
}
 
void USART2_Write(int ch){
	
	while(!(USART2->SR & 0x0080)){
		USART2->DR = (ch & 0xFF);
	}
}
 
void delayMS(int delay){	
	int i;
	for(; delay>0; delay--){
		for(i = 0; i < 1000; i++);
	}
}

I am trying to Initialize and send data through PA3 (USART2_TX). When i connect the pin to a Serial to USB module to see the data sent on the terminal (CoolTerm) I get an error and doesnt' work. I set the baud rate based on that I have an 8MHz crystal on the board and still nothing is working. What is wrong in the code?

This topic has been closed for replies.
Best answer by Tesla DeLorean

You might want to add comments about the pins, clocks and expectations.

Baud rate looks to be ~9600 baud from 8 MHz APB1

AFR[1]= 0x00000070; // Is NOT PA2

AFR[0] = (AFR[0] & 0xFFFFF0FF) | 0x00000700; // PA2:AF7

1 reply

Tesla DeLorean
Guru
December 12, 2019
  1. void USART2_Write(int ch){
  2. while(!(USART2->SR & 0x0080)) {} // Wait on TXE
  3.  
  4. USART2->DR = (ch & 0xFF); // Then send
  5. }

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
MRamy.1
MRamy.1Author
Associate II
December 12, 2019

I edited the code but there is a still a problem. I get the following error in CoolTerm "103 BreakCondition". I search and I found out that it is usually due to Hardware detected a break condition. Usually due to a signal rate mismatch. Is it a problem with the baud rate ?

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
December 12, 2019

You might want to add comments about the pins, clocks and expectations.

Baud rate looks to be ~9600 baud from 8 MHz APB1

AFR[1]= 0x00000070; // Is NOT PA2

AFR[0] = (AFR[0] & 0xFFFFF0FF) | 0x00000700; // PA2:AF7

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..