cancel
Showing results for 
Search instead for 
Did you mean: 

About usart in stm32?

Sm.16
Associate II

I coded for usart communication in stm32f103r8.In this i'm getting the garbage value as output.

given below is my program,kindly help me to correct my program.

****

#include "stm32f10x.h"

#include "stm32f10x_usart.h"

void usartSetup (void);

int SendChar ();

int GetChar (void);

void main()

{

 usartSetup();

 SendChar();

  GetChar();

}

void usartSetup (void) {

 // make sure the relevant pins are appropriately set up.

 RCC->APB2ENR |= 0x00000004;       // enable clock for GPIOA

 GPIOA->CRH  |= (0x0BUL << 4);         // Tx (PA9) alt. out push-pull

 GPIOA->CRH  |= (0x04UL << 8);         // Rx (PA10) in floating

 RCC->APB2ENR |= 0x00004000;      // enable clock for USART1

 USART1->BRR = 72000000L/9600L;/*115200L;*/        // set baudrate

 USART1->CR1 |= 0x00000008 | 0x00000004; // TX enable,Rx enable

 USART1->CR1 |= 0x00002000;          // USART enable

 }

int SendChar () {

  

 while (!(USART1->SR & 0x00000080));

  USART1->DR = 1;

}

int GetChar (void) {

 while (!(USART1->SR & 0x00000020));

 return ((int)(USART1->DR));

}

16 REPLIES 16

"Garbage" usually means incorrect baudrate, and that often means, that the STM32 is not running at the clock used in baudrate calculation.

Are you sure it is running at 72MHz?

JW

Sm.16
Associate II

yes sir! it is 72MHz only.

Sm.16
Associate II

Hi,

Now im getting the perfect output for usart transmit and recieve,but now i got an other issue were my idea is communicating my stm32 with a pic microcontroller.Is that possible? if possible means how can i do?

After a huge effort im recieving the value from stm32 transmitter to pic reciever after recieving led in pic should toggle but it isn't happening,Why?

S.Ma
Principal

Don't use blocking function for receiving data bytes. First step if coming from 8 bit world is to process RX as interrupt service routine (aka ISR).

If you want to avoid ISR, use DMA with cyclic buffer for polling from the main loop.

Your choice.

The CMOS level USART/UART communication between a pair of micro-controller should be relatively straight forward.

Make sure to get the setting the same, check signals with a scope, etc.

In your original code fragment you drop out the bottom of the main() function, you should perhaps avoid that, and either loop doing something with the USART, or have a while(1) loop to prevent it exiting.

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

THANK YOU! I'm trying on it.now i got a new issue which is when i enable USART1 it is working but after that i did the same procedure to enable the USART3 but it isn't responding,is there any special step to work on USART3. Please help me with this.

If USART3 is on APB1 the clock to divide down from should be 36 MHz​

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

The 1st one is for transmiting the data from stm32

***

#include "stm32f10x.h"

void usartSetup (void);

void SendChar();

void main()

{

 //RCC->APB1ENR = 0x00040000;

 //GPIOB->CRH  = 0x00000900;

  

 for(;;)

 {

 usartSetup();

 SendChar();

 }

}

void usartSetup (void) 

{

 RCC->APB2ENR = 0x00000004;

 GPIOA->CRH  = 0X00000900;

 RCC->APB1ENR = 0x00040000;

 RCC->CR = 0X00000001;

 USART1->BRR = 0x340;

 USART1->CR1 = 0x00002008;

}

void SendChar ()  

{

 while (!(USART1->SR & 0x00000080));

 USART1->DR = '2';

}

the 2nd one is for pic recieving

**

Sm.16
Associate II

unsigned char stmvalue;

char usart_Rx();

void blink();

void main()

{

   

  TRISB4=0x00;

  PORTB=0x00;

  TRISCbits.RC7=1;

  SPBRG1=51;

  TXSTA1=0x04;

  RCSTA1=0x90;

   

   

    while(RC1IF==0);

    stmvalue=RCREG1;

    blink();

   

}

void blink()

{

  if(stmvalue==0x41)

  {

    PORTBbits.RB4=1;

  }

}

so, this is my code is there anychanges means please suggest me.