# Baudrate in a STM32L152 Discovery

I have the following code:

#include "stm32l1xx.h"
#include "..\Library_SDM.h"
#include "..\Utiles_SDM.h"

int main(void){
unsigned char value = 'A';
Init_SDM();
Init_LCD();
// User button as Digital Input
GPIOA->MODER &= ~(1 << (0*2 +1));
GPIOA->MODER &= ~(1 << (0*2));
// No pull up-pull down (default)
GPIOA->PUPDR &= ~(11 << (0*2));
// Blue LED digital output
GPIOB->MODER |= (0x01 << (2*6+1));
GPIOB->MODER &= ~(0x01 << (2*6));
// Blue LED as USART
GPIOB->AFR[0] &= 0xF0FFFFFF;
GPIOB->AFR[0] |= 0x07000000;
// USART configuration
USART1->CR1 = 0x00000008;  // Oversampling by 16, 8-bit word, Parity control disabled, Transmission enabled
USART1->CR2 = 0x00000000;  // 1 stop bit
USART1->BRR = 0x00000D05;  // BRR= fck/(16*USARTDIV)=(32*10^6)/(9600)=0xD05
USART1->CR1 |= 0x01 << 13; // USART enable
LCD_Texto((unsigned char*)"PRESS");

while(1){
if ((GPIOA->IDR&0x00000001)!=0) {
while ((GPIOA->IDR&0x00000001)!=0){
wait(70000);
}

while ((USART1->SR & 0x0080)== 0); // While with no body (to wait
// for the transmission buffer to be ready)
USART1->DR = value;
value++;
}
} //End of infinite while
}


When you press the button provided in the discovery board, the letter A is shown in the computer terminal (using Tera Term) and if pressed again is shown letter B and so on. The device is programmed using a 9600 bps configuration with a frecuency of 32MHz.

If I change the BRR value to be 8AE instead of D05 the communication will be 14400 bps which is what I want, but the program does not work as expected (the program has to do exactly what it is said above but instead of having a rate of 9600 bps, at 14400 bps ). I don't know why it does not work at 14400 bps since I guess it is just how fast the information travels, or am I getting something wrong?

• "the program does not work as expected" How does it work then? Please describe the "unexpected" behaviour. And please indent your code properly, otherwise it's quite hard to see how all those while loops are related. – berendi Apr 13 '18 at 7:49
• And you could perhaps use symbolic constants defined in CMSIS (like USART_CR1_TE) instead of numerc ones. – berendi Apr 13 '18 at 7:54
• Get out a scope or logic analyzer and see what it does send. Also show the version that doesn't alongside the one that does. – Chris Stratton Apr 13 '18 at 16:22