cancel
Showing results for 
Search instead for 
Did you mean: 

acknowledge pressing of enter button to stop receiving from usart

adeemkazi92
Associate II
Posted on October 05, 2015 at 13:24

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

2 REPLIES 2
Posted on October 05, 2015 at 14:40

You probably want to test for 0x0A and 0x0D, not 0x0A twice.

You must properly NUL (0x00) terminate strings passed to other string functions.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
adeemkazi92
Associate II
Posted on October 06, 2015 at 09:01

the 0x0d twice was a typing mistake sir,

however the main issue was writing the hex values 0x0d and 0x0a inside the braces. '0x0d '

and not simply 0x0d.

figured that out yesterday.

thanx for the help though sir.

just one more query.

do you have the code for rtc initialisation for stm32.

wat i have in the snippets packet is based on register level programming.

wat i want is the initialisation based on library functions

thanks in advance