cancel
Showing results for 
Search instead for 
Did you mean: 

How to escape USART1 interrupt?

wbarkley
Associate II
Posted on August 06, 2012 at 22:51

Below is my USART1 handler:

void USART1_IRQHandler(void)

{

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

        USART_SendString(USART1, ''\r\n======Blow ME======\r\n'');

        USART_ITConfig( USART1, USART_IT_RXNE, DISABLE );

    }

}

I want it to print Blow Me on hyperterminal once, then go back to executing.  I can print stuff to the screen, but as soon as i press a key to trigger receive interrupt, program gets stuck repeatedly spitting out Blow Me.  The only way I found to print only once is to disable the interrupt, but then it never works again (perhaps put an enable in the main loop?)

Sure this is simple, please help.
4 REPLIES 4
Posted on August 06, 2012 at 23:05

You have to read the data register to clear the RXNE flag, otherwise the interrupt will keep re-entering. Not that sending a string one character at a time under interrupt is good plan.

void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ReceiveData(USART1); // Clear RXNE
USART_SendString(USART1, ''

==BITE ME==

'');
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
wbarkley
Associate II
Posted on August 07, 2012 at 15:53

You are the man Clive!  Although, the latter part of your reply confuses me a bit.  Are you saying the way the code is currently (w/ your fix) spits out the string one character a time?  Seems like it should shoot out the whole string.

Posted on August 07, 2012 at 16:39

You don't present your USART_SendString() code, but I imagine it spends thousands of cycles spinning waiting for TXE to assert after each byte. This is NOT how to write interrupt code. You should buffer outgoing data, and service it a byte at a time with the TXE interrupt.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
wbarkley
Associate II
Posted on August 08, 2012 at 16:46

You're right:

void USART_SendChar(USART_TypeDef* USARTx, uint8_t Data)

{

  /* Check the parameters */

  assert_param(IS_USART_ALL_PERIPH(USARTx));

 

  /* Transmit Data */

  USART_SendData(USARTx, Data);

    while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);

}

void USART_SendString(USART_TypeDef* USARTx, uint8_t *s)

{

    /* Check the parameters */

    assert_param(IS_USART_ALL_PERIPH(USARTx));

 

    while (*s != '\0')

        {

            USART_SendChar(USARTx, *s);

            s++;

        }

}

How do you trigger the TX interrupt since it is internally generated by program?

Cannot pass variables into handler function, so related variables would have to be global to be used in TX interrupt handler?