Question

My program simply does nothing in loop. I set an interrupt for serial port (USART). When data comes, yes, it works and toggles the LED. But it just does it once. Once it got into the interrupt it doesn't come back to where it left off.

My code is here.

#include <avr/interrupt.h> 
#include <avr/io.h> 
volatile int state_Led = LOW;  

void setup()
{    
   pinMode(8, OUTPUT); 

   UBRR0H = 0; // Load upper 8-bits of the baud rate value into the high byte of the UBRR register 
   UBRR0L = 8; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register 

   UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);   // Turn on the transmission, reception, and Receive interrupt      
   interrupts();   
}

void loop()
{
  digitalWrite(8, state_Led); 
}

ISR(USART_RX_vect)
{  
  state_Led = !state_Led;  
}

Can anyone please help me to understand what's wrong with my code and what's going on with it? By the way, I am not an expert on interrupt vectors or architecture of a microprocessor etc. SO if you keep it simple I would definitely appreciate it.

Was it helpful?

Solution

The USART receive interrupt additionally requires the received data register to be read, before readying the interrupt for re-enabling from the return from ISR.

Try the following:

ISR(USART_RX_vect)
{  
  unsigned char c = UDR0;  // clear the USART interrupt
  // or UDRn, UDR0, UDR1, etc...
  state_Led = !state_Led;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top