AnsweredAssumed Answered

Uart1 not waiting to receive all characters

Question asked by costella.david on Jul 14, 2014
Latest reply on Jul 14, 2014 by Clive One
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;        
}

Outcomes