2024-03-11 05:36 PM
#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++);
}
}
Solved! Go to Solution.
2024-03-12 07:03 AM - edited 2024-03-12 07:30 AM
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 ..).
2024-03-12 03:08 AM - edited 2024-03-12 03:09 AM
@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?
2024-03-12 06:45 AM - edited 2024-03-12 06:46 AM
> 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.
2024-03-12 07:03 AM - edited 2024-03-12 07:30 AM
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 ..).