AnsweredAssumed Answered

acknowledge pressing of enter button to stop receiving from usart

Question asked by kazi.adeem on Oct 5, 2015
Latest reply on Oct 6, 2015 by kazi.adeem
hi all,
ive written this sample code for receving data and storing in buffer
how do i stop receiving data when enter  button is encountered
#include <stdio.h>
#include "main.h"
 
static const char String1[] = "ENTER THE OPERATION\r\n";
static const char String2[] = "YOUR OPERATION IS:";
static const char String3[] = "NUMBER 1 IS:";
static const char String4[] = "NUMBER 2 IS:";
static const char ErrorString[]= " YOUR CHOICE IS INCORRECT\r\n"; 
 
void USART1Configuration(void);
void OutString(const char *s);
char GetChar(void);
 
void CALCULATOR(void);
void START(void);
void RECEIVE(void);
void TEMP(void);


int IN[100];
int i=0;


int main(void)
{
  if (SysTick_Config(SystemCoreClock / 1000))
  {
    while(1);
  }
 
  STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
 
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
  STM_EVAL_LEDInit(LED5);
  STM_EVAL_LEDInit(LED6);
 
  USART1Configuration();
 
  while(1)
  {
    CALCULATOR();
  }
}
 
void USART1Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
 
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
 
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9 | GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  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;
  USART_Init(USART1, &USART_InitStructure);
 
  USART_Cmd(USART1,ENABLE);
}
 
void OutString(const char *s)
{
  while(*s) // Not NUL
  {
    if (USART_GetFlagStatus(USART1, USART_FLAG_TXE) != RESET) // if it's empty
      USART_SendData(USART1, *s++); // Out char, and advance
  }
}


void OutInt(const int *s)
{
  while(*s) // Not NUL
  {
    if (USART_GetFlagStatus(USART1, USART_FLAG_TXE) != RESET) // if it's empty
      USART_SendData(USART1, *s++); // Out char, and advance
  }
}


char GetChar(void)
{
  char ch;
 
  while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); // wait while no data
 
  ch = (char)USART_ReceiveData(USART1);
 
  return(ch);
}
 
     
 
void CALCULATOR(void)
{
 START();
 RECEIVE();
 TEMP();
}


void START (void)
{
     OutString(String1);
}


void RECEIVE (void)

     while(1)
     {
     IN[i++] = GetChar();
          if ((IN[i]== '0x0d') || (IN[i]== '0x0d'))
               return;
}
}
void TEMP (void)
{
     OutString(String2);
     OutInt(IN);
}
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
  ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  /* Infinite loop */
  while (1)
  {
  }
}
#endif

Outcomes