cancel
Showing results for 
Search instead for 
Did you mean: 

UART receive interrupt

DAyva
Associate

I'm trying to take input from user via serial port. I use STM32's USART1_IRQHandler and sscanf functions. Although I can print message on the port by using sprintf, I can't take input with sscanf. It doesn't wait for input and assigns a value I can't understand. I shared the code below. How may I solve this issue?

char uartTxBuffer3[50];

memset(uartTxBuffer3, 0, 50);

sprintf(uartTxBuffer3, "\nEnter frequency: \n");

BSP_Uart_Transmit(uartTxBuffer3, sizeof(uartTxBuffer3));

char buffer [50];

char freqaddress[50];

int inputfreq;

USART1_IRQHandler();

//USART_ClearITPendingBit(USART1, USART_IT_RXNE);

sscanf(buffer,"%s %i",freqaddress,&inputfreq);

char uartTxBuffer4[100];

memset(uartTxBuffer4, 0, 100);

sprintf (uartTxBuffer4, "Frequency: %i",(int)inputfreq);

3 REPLIES 3

You generally don't call USART1_IRQHandler() in-line from your code. That's not how interrupts work. The micro-controller calls the handler when the interrupting device signals.

You'd need the handler to accumulate data, that arrives a byte at a time.

You'd need to wait for it to actually fill a string buffer against which to run sscanf(), the data would need to be NUL terminated.

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

Thank you for your answer. I wonder how can I call the handler in the main function? My handler function is attached below, written by another engineer and I need to set it right to establish the communication.

void USART1_IRQHandler(void) //usart irq

{

  char chr;

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

  {

    uartRxBuffer[uartIndex] = USART_ReceiveData(USART1);

    uartIndex++;

    if(uartIndex == 342)

    {

      uartIndex = 0;

      uartReceiveCallBack(uartRxBuffer);

    }

       

  }

  if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET)

  {

  }

}

I can't figure out how to use this function in the main to use scanf. Please help.

You'd probably want to have the interrupt code check for patterns and resync, at some point a 342 byte byte might get misaligned.

The current routine calls a callback function with the data.

You'd need to copy the data to a buffer that isn't transient, and also make sure it is NUL terminated.

You could then flag the availability of data via a volatile variable, which the main() loop could wait on.

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