avr USART中断无法正常工作

时间:2015-03-24 20:22:30

标签: interrupt avr usart

我使用以下代码测试在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,但它没有做任何事情,我不明白为什么。

请帮助。

2 个答案:

答案 0 :(得分:1)

RXCIE位于UCSRB,而不是UCSRC

答案 1 :(得分:1)

RXC的{​​{1}}位是只读的。它由接收缓冲区的状态设置和清除。你无法手动写入它来创建自己的中断。