cancel
Showing results for 
Search instead for 
Did you mean: 

Usart 2 does not work

anton23
Associate II
Posted on June 11, 2017 at 20:31

I am trying to use 2 usart in my program. When i am trying send_to_uart2('a'); nothing happens. In debug mode USART2->SR is always 0x000. Why?

#include 'stm32f10x.h'

#include 'stm32f10x_gpio.h'

#include 'stm32f10x_rcc.h'

void Delay(void)

{

volatile uint32_t i;

for (i = 0; i != 0x140000; i++)

;

}

void send_to_uart1(uint8_t data)

{

while (!(USART1->SR & USART_SR_TC))

;

USART1->DR = data;

}

void send_to_uart2(uint8_t data)

{

while (!(USART2->SR & USART_SR_TC))

;

USART2->DR = data;

}

uint8_t uart_data;

int main(void)

{

GPIO_InitTypeDef PORTA_init_struct;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB1Periph_USART2, ENABLE);

PORTA_init_struct.GPIO_Pin = GPIO_Pin_9;

PORTA_init_struct.GPIO_Speed = GPIO_Speed_50MHz;

PORTA_init_struct.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &PORTA_init_struct);

PORTA_init_struct.GPIO_Pin = GPIO_Pin_10;

PORTA_init_struct.GPIO_Speed = GPIO_Speed_50MHz;

PORTA_init_struct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &PORTA_init_struct);

PORTA_init_struct.GPIO_Pin = GPIO_Pin_2;

PORTA_init_struct.GPIO_Speed = GPIO_Speed_50MHz;

PORTA_init_struct.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOB, &PORTA_init_struct);

PORTA_init_struct.GPIO_Pin = GPIO_Pin_3;

PORTA_init_struct.GPIO_Speed = GPIO_Speed_50MHz;

PORTA_init_struct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &PORTA_init_struct);

USART_InitTypeDef uart_struct1;

uart_struct1.USART_BaudRate = 9600;

uart_struct1.USART_WordLength = USART_WordLength_8b;

uart_struct1.USART_StopBits = USART_StopBits_1;

uart_struct1.USART_Parity = USART_Parity_No;

uart_struct1.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

uart_struct1.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1, &uart_struct1);

USART_Cmd(USART1, ENABLE);

USART_InitTypeDef uart_struct2;

uart_struct2.USART_BaudRate = 9600;

uart_struct2.USART_WordLength = USART_WordLength_8b;

uart_struct2.USART_StopBits = USART_StopBits_1;

uart_struct2.USART_Parity = USART_Parity_No;

uart_struct2.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

uart_struct2.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART2, &uart_struct2);

USART_Cmd(USART2, ENABLE);

while (1) {

send_to_uart2('a');

Delay();

send_to_uart1('b');

Delay();

}

}
3 REPLIES 3
Posted on June 11, 2017 at 20:48

>>Why?

USART2 is on APB1, not APB2, enable the right clock

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 11, 2017 at 21:20

I chaged to 

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

But it do not work. value  USART2->DR do not change.

Posted on June 11, 2017 at 22:33

The question asked about USART2->SR, which presumably now isn't zero?

Reading USART2->DR shows the content of the Reception register, it isn't the same thing as the Transmission register you write too, it is not a memory cell.

Avoid sitting the debugger over the USART, it will alter its behaviour, reading the DR will clear bits in the SR

If you want to read the same data back in DR, then loop the TX and RX together externally.

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