arduino Usart1中断

问题描述 投票:-1回答:1

我正在使用arduino引导程序在atmel Studio 7的aurdino mega2560中工作起初,我在中断时使用了uart 0(tx0和rx0),并且运行良好但是,当我将uart1与u1_rx的中断一起使用时不起作用

#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) 
    {

    }
}
arduino atmel atmelstudio
1个回答
0
投票

当启用UDRIE0时控制器冻结,并且仍在触发所以不会去UART1我使用TX中断,就可以了

© www.soinside.com 2019 - 2024. All rights reserved.