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

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
Up vote any posts that you find helpful, it shows what's working..

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
Associate II

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) { }

}

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
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal

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.

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.

Use real hardware​

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