Ardino Usart1中断

时间:2016-11-12 12:43:13

标签: arduino atmel atmelstudio

我正在使用arduino bootloader在atmel studio 7的aurdino mega2560上工作 起初我使用uart 0(tx0和rx0)中断,它运行良好 但是,当我使用带有u1_rx中断的uart1时不起作用

#include <avr/io.h>
#include <util/delay.h>
#include<avr/interrupt.h>
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif

#define USART_BAUDRATE0 115200
#define USART_BAUDRATE1 57600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE0 * 16UL))) - 1)
volatile char ReceivedByte1;
void initUART(int BRR)
{//|(1 << TXCIE0)
    UCSR0A=(0<<RXC0)|(0<<FE0)|(0<<DOR0)|(0<<UPE0)|(1<<U2X0)|(0<<MPCM0);
    UCSR0B = (1 << RXCIE0)  |(1 << UDRIE0)|(1 << RXEN0) | (1 << TXEN0)|(0<<UCSZ02)|(0<<RXB80)|(0<<TXB80);   
    UCSR0C =(0<<UMSEL01)|(0<<UMSEL00)|(0<<UPM00)|(0<<UPM01)|(0<<USBS0)|(1 << UCSZ00) | (1 << UCSZ01)  ;
    UBRR0H=(BRR >> 8);
    UBRR0L=BRR;
    sei ()  ;
}
void initUART1(int BRR)
{//|(1 << TXCIE0)
    UCSR1A=(0<<RXC1)|(0<<FE1)|(0<<DOR1)|(0<<UPE1)|(1<<U2X1)|(0<<MPCM1);
    UCSR1B = (1 << RXCIE1)  |(1 << UDRIE1)|(1 << RXEN1) | (1 << TXEN1)|(0<<UCSZ12)|(0<<RXB81)|(0<<TXB81);
    UCSR1C =(0<<UMSEL11)|(0<<UMSEL10)|(0<<UPM10)|(0<<UPM11)|(0<<USBS1)|(1 << UCSZ10) | (1 << UCSZ11)  ;
    UBRR1H=(BRR >> 8);
    UBRR1L=BRR;
    sei ()  ;
}
ISR(USART0_RX_vect)
{
    char ReceivedByte;
    //ReceivedByte = UDR0; // Fetch the received byte value into the variable "ByteReceived"
    //UDR0 = ReceivedByte; // Echo back the received byte back to the computer
    while ((UCSR0A & (1 << RXC0)) == 1) {}; // Do nothing until data have been received and is ready to be read from UDR
    ReceivedByte = UDR0; // Fetch the received byte value into the variable "ByteReceived"
    while ((UCSR0A & (1 << UDRE0)) == 1) {}; // Do nothing until UDR is ready for more data to be written to it
    UDR0 = ReceivedByte; // Echo back the received byte back to the computer
}
ISR(USART1_RX_vect)
{
    //while ((UCSR1A & (1 << RXC1)) == 1) {}; // Do nothing until data have been received and is ready to be read from UDR
    ReceivedByte1 = UDR1; // Fetch the received byte value into the variable "ByteReceived"
    PORTB=0xff;

//  while ((UCSR0A & (1 << UDRE0)) == 1) {}; // Do nothing until UDR is ready for more data to be written to it
    UDR0 = ReceivedByte1; // Echo back the received byte back to the computer
}
ISR(USART1_UDRE_vect)
{

}
ISR(USART0_UDRE_vect)
{

}
ISR(USART0_TX_vect)
{
}
int main(void)
{
    initUART1(34);
    initUART(16);//Boudratecalc);
    DDRB = 0xFF;
    PORTB=0x00;

    while (1) 
    {

    }
}

1 个答案:

答案 0 :(得分:0)

当UDRIE0启用并仍然触发时,控制器冻结 所以不会去UART1 我使用TX中断和它的工作