Search code examples
interruptavrusart

avr USART interrupt not working


I am using the following code to test the usart interrupt working in ATtiny2131 microcontroller :

#include<avr/io.h>        // for reading data to I/O ports
#include<util/delay.h>    // for setting delays
#include<avr/interrupt.h> // for setting interrupt service routines


void USART_initialize() {

    //int baud_rate = (unsigned int)(((16000000)/(16*baud_value))-1);
    UCSRC &= ~(1<<UMSEL);                       // MODE OF COMMUNICATION (SYNCHRONOUS --> (USMEL = 1), ASYNCHRONOUS --> (USMEL = 0))
    UBRRH = (unsigned char)(103 >> 8);         // SETTING BAUD
    UBRRL = (unsigned char)(103);              // RATE
    UCSRA &= ~(1<<U2X);                        // NORMAL ASYNCHRONOUS MODE OF COMMUNICATION

    UCSRB = (1<<RXEN)|(1<<TXEN);               // ENABLE RECEIVER & TRANSMITTER
    UCSRC |= (3<<UCSZ0);                       //
    UCSRB &= ~(UCSZ2);                         // 8-BIT DATA
    //UCSRC |= (1<<UPM1);                      // EVEN PARITY
    //UCSRC &= ~(1<<UPM0);
    UCSRC &= ~(1<<UPM1);
    UCSRC &= ~(1<<UPM0);
    UCSRC |= (1<<USBS);                        // 1 STOP BIT

    sei();                                     // ENABLE GLOBAL INTERRUPT
    UCSRC |= (1<<7);                           // ENABLE RECEIVE COMPLETE INTERRUPT
}


int main(void) {
    USART_initialize();
    while(1) {
        UCSRA |= (1<<RXC);
        _delay_ms(1000);
    }
    return(0);   
}

ISR(USART0_RX_vect) {
    PORTD |= (1<<5);
    _delay_ms(1000);
    PORTD &= ~(1<<5);
    _delay_ms(1000);
    int data = UDR;
}

the code should blink the LED on PD5 every second, but it isnt doing anything and I cant understand why.

Please Help.


Solution

  • RXCIE is in UCSRB, not UCSRC.