avr USART 中断不工作

avr USART interrupt not working

我正在使用以下代码来测试在 ATtiny2131 微控制器中工作的 usart 中断:

#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;
}

代码应该使 PD5 上的 LED 每秒闪烁一次,但它什么也没做,我不明白为什么。

请帮忙。

RXCIEUCSRB,而不是 UCSRC

UCSRARXC 位是只读的。它由接收缓冲区的状态设置和清除。您不能手动写入它来创建自己的中断。