cancel
Showing results for 
Search instead for 
Did you mean: 

Uart1 not waiting to receive all characters

dvcosta43
Associate II
Posted on July 14, 2014 at 15:14

This is my first post here as I'm really stuck. I'm using a stm32F103 device but I cannot seem to get the usarts configured correctly to receive data. Transmit is fine, and on the receive side I do see characters. I want the user to enter a correct string terminated with a '\r' before the main app continues. However the uart only waits for me to type in some of the characters. It never waits to see in carraige return is pressed.

What I need is for the app to wait in the character receive loop until CR is pressed. If the key is invalid, then the user is prompted to re-enter the key. If not move on.

Thanks in advance

Dave

    void Usart1Init(void)

    {

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

        

NVIC_InitTypeDef NVIC_InitStructure;      

        

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);    

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

      

  /*-------------------------- GPIO Configuration ----------------------------*/

      

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;             

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);           

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;            

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

        

        

  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_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    

  COMInit(COM1, &USART_InitStructure);

  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);        

    }

void USART1_IRQHandler(void)

{

  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)

  {          

         printf(''\r\nEnter Key \r\n'');        

         res = Check_Key();                     

         if (res == 1)

             {             

           printf(''\r\nCorrect Key \r\n'');     

           }        

    }

}

uint8_t Check_Key(void)

{

char rxbyte;  

static uint8_t val = 0;    

static uint8_t char_count= 0;

static char str_fr[20];

        

   while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

   rxbyte = USART_ReceiveData(USART1);                        

   str_fr[char_count] = (uint8_t)rxbyte;

   printf(''*'');

     char_count++;

    

    

if (rxbyte == '\r')  // Detect a newline character

  {

       

  if (!strcmp(str_fr, ''Hello+Welcome\r''))

        {        

        val = 1;

      }                    

     else

        {

          printf(''\n\rInvalid Key\n\r'');

            char_count= 0;    

            }

    }                

   return val;        

}
4 REPLIES 4
Posted on July 14, 2014 at 15:36

Ok, have your interrupt receive one character at a time, and don't output multiple characters via printf(), etc.

Understand that it the time it takes to send a character you can also receive one, if you send a dozen and don't service the received data you will likely miss them.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
dvcosta43
Associate II
Posted on July 14, 2014 at 16:32

Hello Clive,

I thought I was receiving only one character at a time. Where in the code states otherwise? Removing all printfs make no difference. Should UART not wait in the receive loop until CR is pressed?

Dave

dvcosta43
Associate II
Posted on July 14, 2014 at 17:08

Clive,

I think I'm sorted on this one. Thanks for your help.

Dave

Posted on July 14, 2014 at 17:15

I scanned the code, the while loop seemed problematic, watch that the debugger doesn't clear the peripheral status.

Make sure you NUL terminate strings before using library functions against them.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..