Skip to main content
Sm.16
Associate II
May 29, 2019
Question

About usart in stm32?

  • May 29, 2019
  • 16 replies
  • 3242 views

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));

}

This topic has been closed for replies.

16 replies

waclawek.jan
Super User
May 29, 2019

"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
Sm.16Author
Associate II
May 29, 2019

yes sir! it is 72MHz only.

Sm.16
Sm.16Author
Associate II
May 31, 2019

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?

Tesla DeLorean
Guru
May 31, 2019

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
May 31, 2019

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.

Sm.16
Sm.16Author
Associate II
June 1, 2019

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.

Tesla DeLorean
Guru
June 1, 2019

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

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Sm.16
Sm.16Author
Associate II
June 1, 2019

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

**

Tesla DeLorean
Guru
June 1, 2019

You should perhaps OR clock enables rather than blind write, and mask on the GPIO configurations more completely.

Review settings in debugger view of RCC, GPIO and USART1

Check signals with a scope

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Sm.16
Sm.16Author
Associate II
June 3, 2019

The above program is perfectly working for USART1, but if i adjust the registers for USART3 it is not working there is no signal passing. Is there any specific clock division or registers to enable.

Sm.16
Sm.16Author
Associate II
June 1, 2019

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.

Sm.16
Sm.16Author
Associate II
June 6, 2019

Hi Sir,

As far now i researched many examples and topics related to this ,at the end i didn't get the usart initialize.

Below is my program for usart3 to Tx/Rx data. If you don't mind please validate my program and rectify it, or please send me a program for usart3.

*************

void Usart3Init(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

USART_ClockInitTypeDef USART_ClockInitStructure;

//enable bus clocks

//RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

//Set USART1 Tx (PA.09) as AF push-pull

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure);

//Set USART1 Rx (PA.10) as input floating

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOB, &GPIO_InitStructure);

USART_ClockStructInit(&USART_ClockInitStructure);

USART_ClockInit(USART3, &USART_ClockInitStructure);

USART_InitStructure.USART_BaudRate = 9600;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No ;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

//Write USART1 parameters

USART_Init(USART3, &USART_InitStructure);

//Enable USART1

USART_Cmd(USART3, ENABLE);

}

void Usart3Put(uint8_t ch)

{

   USART_SendData(USART3, (uint8_t) ch);

   //Loop until the end of transmission

   while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)

   {

   }

}

uint8_t Usart3Get(void){

   while ( USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);

    return (uint8_t)USART_ReceiveData(USART3);

}

int main(void)

{

Usart3Init();

Usart3Put('A');

Usart3Put('R');

Usart3Put('M');

while (1) { }

}

Tesla DeLorean
Guru
June 6, 2019
void Usart3Init(void)
{
 GPIO_InitTypeDef GPIO_InitStructure = {0};
 USART_InitTypeDef USART_InitStructure = {0};
 
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
 
 //Set USART3 Tx (PB.10) as AF push-pull
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOB, &GPIO_InitStructure);
 
 //Set USART3 Rx (PB.11) as input floating
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 GPIO_Init(GPIOB, &GPIO_InitStructure);
 
 USART_InitStructure.USART_BaudRate = 9600;
 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
 USART_InitStructure.USART_StopBits = USART_StopBits_1;
 USART_InitStructure.USART_Parity = USART_Parity_No;
 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
 
 //Write USART3 parameters
 
 USART_Init(USART3, &USART_InitStructure);
 
 //Enable USART3
 
 USART_Cmd(USART3, ENABLE);
}
 
void Usart3Put(uint8_t ch)
{
 while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
 USART_SendData(USART3, (uint8_t) ch);
}
 
uint8_t Usart3Get(void)
{
 while ( USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
 
 return (uint8_t)USART_ReceiveData(USART3);
}
 
int main(void)
{
 Usart3Init();
 
 Usart3Put('A');
 Usart3Put('R');
 Usart3Put('M');
 
 while (1) Usart3Put(0x55);
}

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Sm.16
Sm.16Author
Associate II
June 8, 2019

Hi Sir,

Thank you so much for your valuable concern and program. The program given by you is executing properly,but here when i try this hex file in proteus v8

i didn't get the response. Is there any function you have to make usart3 work in proteus. And one more thing is using your program i got the perfect output in usart1 in the same proteus v8.

Tesla DeLorean
Guru
June 8, 2019

Use real hardware​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
June 6, 2019

This shows that Cube USART examples are yet to be application friendly.

A polling version with DMA and an interrupt version using SW FIFO would make life easier for most users.

Console ASCII demo being the default example.

Implementation of AT command handler example being a useful plus.