I just want to toggle an LED when the USART finishes receiving, so I wrote the following for the
USART_RX_vect, but the LED never changes. However, the LED
does toggle if move the code from the ISR into the main loop and precede it with
while ( !(UCSR0A & (1<<UDRE0)) );Can anyone describe my error to me?
#define F_CPU 16000000UL
#define UBRR_VALUE 416 // baudrate 2400
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
ISR(USART_RX_vect)
{
char data = UDR0;
PORTB ^= (1 << PB0); // Toggle the LED
_delay_ms(500);
}
int main()
{
DDRB |= (1 << PB0); // Set LED as output
// Set baud rate
UBRR0H = (uint8_t)(UBRR_VALUE>>8);
UBRR0L = (uint8_t)UBRR_VALUE;
// Set frame format to 8 data bits, no parity, 1 stop bit
UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);
//enable reception and RC complete interrupt
UCSR0B |= (1<<RXEN0)|(1<<RXCIE0);
// Enable global interrupts
sei();
while (1)
;
}
Thank you for your assistance.