cancel
Showing results for 
Search instead for 
Did you mean: 

BAUDERATE problem by USART generated code in STM32

Nbkjh.1
Associate II

Hello,I have defined the system as The APB1 CLK is defined by 45MHz as shown bellow.

The Usart2 periphery is on the APB1 bus.

By the full code attached bellow the USART2 and gpio settings are as following by the printscreen from the reference manual.

https://www.st.com/resource/en/reference_manual/dm00031020-stm32f405-415-stm32f407-417-stm32f427-437-and-stm32f429-439-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf

The OSPEEDR is set to be fast.

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

USART2->CR1&=~(1UL<<12);

By the USART2 CR1 register bit 12 the data packet is 8 bit long.

USART2->BRR=(7<<0)|(24<<4);

The most critical thing is BRR i put 7 in location 0 and 24 at location 4 as shown in the printscreen bellow.

My USART CLK is 45MHz which is divided by 16*24.7

45000000/(16*24.7)=113866

In reality i get the following Scope waveform which is not the calculated frequency.

the distance of a single bit as you can see between the cursors is 2.4uS

Where did i get wrong why i got such low frequency?

The full code is shown bellow.

Thanks.

0693W000008GVheQAG.jpg 

0693W000008GVfxQAG.jpg 

 *     The system Clock is configured as follow : 

 *      System Clock source      = PLL (HSE)

 *      SYSCLK(Hz)           = 180000000

 *      HCLK(Hz)            = 180000000

 *      AHB Prescaler         = 1

 *      APB1 Prescaler         = 4

 *      APB2 Prescaler         = 2

 *      HSE Frequency(Hz)       = 8000000

 *      PLL_M             = 4

 *      PLL_N             = 180

 *      PLL_P             = 2

 *      VDD(V)             = 3.3

 *      Main regulator output voltage = Scale1 mode

 *      Flash Latency(WS)       = 5

0693W000008GVbvQAG.jpg0693W000008GVezQAG.jpg0693W000008GVgRQAW.jpg 

#include "rcc_config.h"
void uart2config(void);
void uart2_sendchar(uint8_t );
 
 
void uart2config(void)
{
   RCC->APB1ENR|=RCC_APB1ENR_USART2EN;
   RCC->AHB1ENR|=RCC_AHB1ENR_GPIOAEN;
   //why in EFR32 we haveoutput but in stm32 we have alternate function for GPIO
   //GPIO speed i did not use in EFR32 what is speed in GPIO
	GPIOA->MODER|=(1UL<<7);//pin A3
   GPIOA->MODER&=~(1UL<<6);//pin A3	
	  GPIOA->MODER|=(1UL<<5);//pin A2
   GPIOA->MODER&=~(1UL<<4);//pin A2
	
	GPIOA->OSPEEDR|=(1UL<<7);//pin A3
   GPIOA->OSPEEDR&=~(1UL<<6);//pin A3	
	  GPIOA->OSPEEDR|=(1UL<<5);//pin A2
   GPIOA->OSPEEDR&=~(1UL<<4);//pin A2
	
	//8.4.9 285 why we need 
	GPIOA->AFR[0]|=(7<<8);
	GPIOA->AFR[0]|=(7<<12);
	USART2->CR1|=(1<<13);
	USART2->CR1&=~(1UL<<12);
	USART2->BRR=(7<<0)|(24<<4);
	USART2->CR1|=(1<<2);
	USART2->CR1|=(1<<3);
	
}
//bitwise and
void uart2_sendchar(uint8_t c)
{
	USART2->DR=c;
	while(!(USART2->SR&(1<<6)));
}
 
int main(void)
{
	uart2config();
	
	
  while(1)
  {
		
		uart2_sendchar(0b10101000);
		delay(10000);
  }	
}

4 REPLIES 4

Which STM32?

Check the APB speed e.g. by running PWM on a timer on the same bus. Read out and check/post the relevant RCC and USART registers content.

JW

Could you please say more about this PWM method?

Thanks.

Check HSE_VALUE define and PLL​ settings.

Set up a TIM to toggle a pin, toggle a pin in SysTick interrupt, or export internal clock via MCO PA8 pin.

SPEEDR setting controls slew rate of pin, ie how aggressively it is driven and by how many transistors. ​

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