arduino Usart1 中断

Posted

技术标签:

【中文标题】arduino Usart1 中断【英文标题】:arduino Usart1 Interrupt 【发布时间】:2016-11-12 12:43:13 【问题描述】:

我正在 atmel studio 7 的 aurdino mega2560 使用 arduino 引导加载程序工作 起初我使用带有中断的 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) 
    

    

【问题讨论】:

只有当 uart 0 被禁用时,uart 1 才有效 你的代码不对称。 【参考方案1】:

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

【讨论】:

以上是关于arduino Usart1 中断的主要内容,如果未能解决你的问题,请参考以下文章

Arduino-中断函数介绍和使用

Arduino定时器中断采样

Arduino-中断

ARDUINO 中断

Python 串行读取线是不是会中断 Arduino 循环?

Arduino如何处理中断?