cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with send function in USART1 - STM32VL Discovery

favero
Associate
Posted on July 05, 2012 at 01:52

Hello.

I'm facing a problem when trying to send data with function USART_SendData(USART1, (uint16_t)resultat) (resultat is a int 32 bits).

When i step by step debugged it, i discovered that my program just enters in an infite loop, due that the flag TC is never set to SET - my conclusion is that the data could not be sent.

Testing with scope, discovered that the PA9 pin stays in a HIGH (or LOW) lvl constantly... 

void envoi_data(int resultat){

        USART_SendData(USART1, (uint16_t)resultat);

        while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)

        {

          }

 }

Here you can find my inits....

void my_gpio(){

  GPIO_InitTypeDef  GPIO_InitStructure;

    

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO |  RCC_APB2Periph_GPIOA , ENABLE);

    

  /* Configure the pin */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOB, &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);

}

void my_usart(){

    USART_InitTypeDef USART_InitStructure;

    USART_ClockInitTypeDef USART_ClockInitStructure;

  

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

   

    USART_ClockStructInit(&USART_ClockInitStructure);

    USART_ClockInit(USART1, &USART_ClockInitStructure);

      

    USART_InitStructure.USART_BaudRate = 9600;

    USART_InitStructure.USART_WordLength = USART_WordLength_8b;

    USART_InitStructure.USART_StopBits = USART_StopBits_1;

    USART_InitStructure.USART_Parity = USART_Parity_No;

    USART_InitStructure.USART_Mode = USART_Mode_Rx;

    USART_InitStructure.USART_HardwareFlowControl =       USART_HardwareFlowControl_None;

    //Write USART1 parameters

    USART_Init(USART1, &USART_InitStructure);

    USART_Cmd(USART1, ENABLE);

    

}

I'm using IAR workbench....

Thank you!

#usart #stm32vl-usart-problem
3 REPLIES 3
Posted on July 05, 2012 at 05:00

Isn't going to send anything with

USART_InitStructure.USART_Mode = USART_Mode_Rx;

try

USART_InitStructure.USART_Mode = USART_Mode_Tx;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
favero
Associate
Posted on July 05, 2012 at 05:45

Thank you for helping me, Clive.

Posted on July 05, 2012 at 06:27

You're welcome.

You might want to do a test for TXE before sending a character, instead of TC afterward. Waiting for TC (ie last bit actually sent on wire) is the slowest possible method. Waiting for TXE lets you fill the holding register as soon as the first bit of the previous byte is sent.

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