Home > OS >  Error occurs when i transmit data with USART
Error occurs when i transmit data with USART

Time:12-11

I am stuck on the following problem. Consider this code:

int main(void)
{
            SysTickInit();
            USART_GPIOInits();
            USART_Inits();
            char data[] = "hello\n";
            for(uint8_t i=0; i<10; i  )
            {
            HAL_UART_Transmit(&Usart1, (uint8_t*)data, strlen(data), 1000);
            }
                while(1){}
}

I try to send hello\n to Hercules in 10 times, but Hercules did not receive what i sent this is what Hercules got , it had þ every the first time I reset the MCU. But , when I used Debugger mode, it did not get any error.

enter image description here

below is transmit function

enter image description here

below is Init function

enter image description here

but want to communicate with fingerprint , but because of this wrong i cant communicate

CodePudding user response:

Change the order of initialization.

From

USART_GPIOInits();
USART_Inits();

to

USART_Inits();
USART_GPIOInits();

UART's default line state is logic high, logic low (start bit) launches a new transfer.

When GPIO is inialized first, with the corresponding peripheral module disabled, most likely you'll gen a logic low level on the TX pin, because there is no one to set it to a logic high (since UART is still disabled). When UART is initialized, it sets the TX line to a logic high (stop bit), and the terminal appication receives it as a broken byte.

Check your schematics

During and after reset CPU outputs are tri-stated. Most likely they'll stay at zero level until the configuration code will do it's job, leading to the same issue - receiving a garbage byte after the reset.

To prevent it, voltage levels on the interface pins must be defined during reset phase with an external pull-up resistor, like 10kOm, from TX and RX pins to VCC.

  • Related