cancel
Showing results for 
Search instead for 
Did you mean: 

I need help to transmitt through the USART3 port without interruptions in a 16 MHz clock with 9600 baud rate

JAmed.1
Associate

void usartConfig(){

USART3->CR1=0x00;

//Configurar UE

USART3->CR1|=(1<<13);

//Configurar M=0

USART3->CR1 &=~(1u<<12);

//Configurar STOP (00)=1

USART3->CR2 &=~(1u<<12);

USART3->CR2 &=~(1u<<13);

//Configurar DMAT

USART3->CR3 &=~(1u<<7);

//Configurar baud rate

USART3->BRR = (3<<0) | (104<<4);

//Configurar TE

USART3->CR1|=(1<<3);

/*//Configurar parity

USART3->CR1 &=~(1u<<10);

*/

//Configurar RE

// USART3->CR1|=(1<<2);

}

void senData(uint8_t data){

//Pasar datos

USART3->SR &=~(1u<<6);

USART3->DR =data;

while(!(USART3->SR & (1<<6)));

}

4 REPLIES 4

Looks a mess, doing multiple RMW on the registers is inefficient.

What STM32 part?

What pins?

Where's the code to enable the UART clock and pins?

void senData(uint8_t data){

while(!(USART3->SR & (1<<7))); // Front test TXE

USART3->DR = data; // Add data when empty

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
MM..1
Chief II

DMA ?

void enableGPIO(){

activarGPIO_RCC_AHB1(3);

definirModoGPIO(GPIOB,10,GPIO_Mode_AF);//TX enviar

definirModoGPIO(GPIOB,11,GPIO_Mode_AF);//RX recibir

//AF7

GPIOB->AFR[1] &=~(1u<<15);

GPIOB->AFR[1] |=(1<<14);

GPIOB->AFR[1] |=(1<<13);

GPIOB->AFR[1] |=(1<<12);

GPIOB->AFR[1] &=~(1u<<11);

GPIOB->AFR[1] |=(1<<10);

GPIOB->AFR[1] |=(1<<9);

GPIOB->AFR[1] |=(1<<8);

//Configurar los puertos

//Max speed

/*GPIOB->OSPEEDR |=(1<<23);

GPIOB->OSPEEDR |=(1<<22);

GPIOB->OSPEEDR |=(1<<21);

GPIOB->OSPEEDR |=(1<<20);*/

// GPIO_PinAFConfig(GPIOB,10,7);

// GPIO_PinAFConfig(GPIOB,11,7);

}

void configureUART(){

enableUSART_RCC_APB1(18);

USART3->CR1=0x00;

//Configurar UE

USART3->CR1|=(1<<13);

//Configurar M=0

USART3->CR1 &=~(1u<<12);

//Configurar STOP (00)=1

USART3->CR2 &=~(1u<<12);

USART3->CR2 &=~(1u<<13);

//Configurar DMAT

USART3->CR3 |=(1<<7);

//Configurar DMAR

USART3->CR3 |=(1<<6);

//Configurar baud rate

USART3->BRR = (3<<0) | (104<<4);

//Configurar TE

USART3->CR1|=(1<<3);

//Configurar RE

USART3->CR1|=(1<<2);

}

gregstm
Senior III

Not sure if this will help you, but this is how I initialised my USART3 on the STM32L496. I write direct to registers but using the symbols provided to make the code readable/maintainable.

void Init_UART3 (void)

{

 // Reset USART3

 RCC->APB1RSTR1 |= RCC_APB1RSTR1_USART3RST;

 RCC->APB1RSTR1 &= ~RCC_APB1RSTR1_USART3RST;

 // Enable USART3 clock

 RCC->APB1ENR1 |= RCC_APB1ENR1_USART3EN;

 // MUST write BRR before enabling UART

 // Set the baud rate

 USART3->BRR = (sys_clock_freq/9600);

 //USART defaults to:

 // - 8 data bits

 // - no parity

 // - 1 stop bit

 // - 16 times oversampling (better for less accurate clocks)

 // Enable parity (defaults to even parity)

 USART3->CR1 |= USART_CR1_PCE;

 // Enable 9 data bits if we want to use parity (ninth bit becomes parity bit)

 USART3->CR1 |= USART_CR1_M0;

 // Enable the USART

 USART3->CR1 |= USART_CR1_UE;

 // Enable the transmitter

 USART3->CR1 |= USART_CR1_TE;

 // Set PC10 to AF7

 // AFR[1] = AFRH, AFRH is used for GPIO bits 8..15

 // AFR[0] = AFRL, AFRL is used for GPIO bits 0..7

 GPIOC->AFR[1] |= (7<<((10-8)*4));

 // Make PC10 (TX) alternate function, push-pull output

 GPIOC->MODER &= ~GPIO_MODER_MODER10;

 GPIOC->MODER |= GPIO_MODER_MODER10_1;

               // Set speed to 2 MHz

// GPIOC->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR10_0;

 // Enable the receiver

 USART3->CR1 |= USART_CR1_RE;

 // Set PC11 to AF7

 // AFR[1] = AFRH, AFRH is used for GPIO bits 8..15

 // AFR[0] = AFRL, AFRL is used for GPIO bits 0..7

 GPIOC->AFR[1] |= (7<<((11-8)*4));

 // Make PC11 (RX) alternate function

 GPIOC->MODER &= ~GPIO_MODER_MODER11;

 GPIOC->MODER |= GPIO_MODER_MODER11_1;

}