2012-08-06 01:51 PM
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.2012-08-06 02:05 PM
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==
'');
}
}
2012-08-07 06:53 AM
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.
2012-08-07 07:39 AM
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.
2012-08-08 07:46 AM
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?