cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103C8T6 USART wrong data

pjbumbur
Associate
Posted on October 23, 2016 at 12:29

Hello,

I have a problem with sending data from/to my STM32F103C8T6 using USART connection.

I'm using USB2TTL PL2303 converter.

I connected it to RX1 and TX1 on the board. I can send .hex files with STM Flash Loader Demonstrator and it works (like LED blink etc.).

This is how it looks like:

http://image.prntscr.com/image/0d03529e29c64d78b6c3fb08bbaf57f8.png

http://image.prntscr.com/image/0d03529e29c64d78b6c3fb08bbaf57f8.png

This is my code:

void

startUsart(){

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,

ENABLE

);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,

ENABLE

);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,

ENABLE

);

GPIO_InitTypeDef

GPIO_InitStructure;

GPIO_InitStructure.

GPIO_Pin

= GPIO_Pin_9;

GPIO_InitStructure.

GPIO_Mode

=

GPIO_Mode_AF_PP

;

GPIO_InitStructure.

GPIO_Speed

=

GPIO_Speed_50MHz

;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.

GPIO_Pin

= GPIO_Pin_10;

GPIO_InitStructure.

GPIO_Mode

=

GPIO_Mode_IN_FLOATING

;

GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitTypeDef

uart;

uart.

USART_BaudRate

= 115200;

uart.

USART_HardwareFlowControl

= USART_HardwareFlowControl_None;

uart.

USART_Mode

= USART_Mode_Rx | USART_Mode_Tx;

uart.

USART_Parity

= USART_Parity_No;

uart.

USART_StopBits

= USART_StopBits_1;

uart.

USART_WordLength

= USART_WordLength_8b;

USART_Init(USART1, &uart);

USART_Cmd(USART1,

ENABLE

);

}

void

send_char(

char

c)

{

while

(USART_GetFlagStatus(USART1, USART_FLAG_TXE) ==

RESET

);

USART_SendData(USART1, c);

}

int

main(

int

argc,

char

* argv[])

{

startUsart();

while

(1)

{

send_char(

'y'

);

timer_sleep(2000);

send_char(

'a'

);

timer_sleep(2000);

}

}

What I receive is:

http://image.prntscr.com/image/4c508ec85ff9476f99689ce8e1a5epng

I was looking for similar problem but couldn’t find any working solution.

I tried lower baudrate like 9600 but still not working.

stm32 usart stm32f103

3 REPLIES 3
Posted on October 23, 2016 at 15:00

You'd need to review HSE_VALUE used for the project vs whatever crystal your board uses. You could try just using the HSI clock rather than the HSE/PLL

Suggest you output a U characters, and look at the TX signal on a scope to verify/understand the bit clock it is using.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pjbumbur
Associate
Posted on October 23, 2016 at 17:57

My HSE_VALUE = 8000000.

I generated code with STM32Cube for this task and it works (characters received from the board are correct). I can see that there is the same HSE_VALUE.

I would prefer to have my own code rather than generated though but I'm note sure what to change in my project. Any tips?

Posted on October 23, 2016 at 19:53

Code doesn't look unreasonable, not sure of the tool chain used or if SystemInit is called. I'd scope the pin and review the clocks.

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