stm32 - STM32F0 Discovery USART sends the wrong value -
i'm making first steps in stm32 world, , quite hard. run discovery board. when learnt how control gpio , interrupts them decided play usart. , here hell began. i've spent 2 evenings struggling such simple thing serial communications!
what have:
- configured gpio
- configured usart
- clock standard stm library - hsi 48 mhz
- i see characters in putty
- transmission parameters tested: 8n1 9600 , 115200
as can see code, i'm expecting see 'a' character while '_'. in opinion baud rate, wrong character. when change brr wrong value display junk characters.
any idea welcome!
#include <stm32f0xx.h> void putc(uint8_t character) { while(!(usart1->isr & usart_isr_txe)) ; usart1->tdr = character; } int main(void) { int i; rcc->apb2enr |= rcc_apb2enr_usart1en | rcc_apb2enr_syscfgcompen; rcc->ahbenr |= (rcc_ahbenr_gpiocen | rcc_ahbenr_gpioaen); //iosetup(); gpioa->moder &= ~(gpio_moder_moder9 | gpio_moder_moder10); gpioa->moder |= (gpio_moder_moder9_1 | gpio_moder_moder10_1); gpioa->ospeedr &= ~(gpio_ospeedr_ospeedr9 | gpio_ospeedr_ospeedr10); gpioa->ospeedr |= (gpio_ospeedr_ospeedr9_0 | gpio_ospeedr_ospeedr10_0); gpioa->pupdr &= ~(gpio_pupdr_pupdr9 | gpio_pupdr_pupdr10); gpioa->pupdr |= gpio_pupdr_pupdr9_0 | gpio_pupdr_pupdr10_0; gpioa->afr[1] |= (0x00000110); // usart usart1->brr = (uint32_t)(48000000/9600); // 9600 //usart1->cr2 = 0; //usart1->cr3 = 0; //usart1->gtpr = 0; usart1->cr1 = usart_cr1_ue | usart_cr1_te | usart_cr1_re; (i = 0; < 0x1000; i++) __nop(); = 0; for(;;) { putc('a'); } return 0; }
hsi not precise , not stable clock. has big error related temperature , frequency in "perfect" temperature not either. if "wrong" value few percent away value calculated, can 100% problem hsi accuracy.
your choice use external crystal. if "wrong" value multiple of calculated (like 2, 4, 8 times smaller/bigger), guess should take @ code in systeminit() sets clock.
Comments
Post a Comment