Arduino中断输出比较寄存器无法正常工作

时间:2016-04-11 01:13:01

标签: arduino arduino-uno

我正在尝试使用中断来使用Arduino Uno切换引脚。这就是我到目前为止所做的:

const int outputPin = 12;

void setup() {
    pinMode(outputPin, OUTPUT);
    // initialize Timer0
    noInterrupts(); // disable all interrupts

    TCCR0A = 0;
    TCCR0B = 0;
    TCNT0 = 0;

    OCR0A = 255;
    TCCR0B |= (1 << WGM12); // CTC mode
    TCCR0B |= (1 << CS12); 
    TCCR0B |= (1 << CS10); // 1024 prescaler
    TIMSK0 |= (1 << OCIE1A); // enable timer compare interrupt
    interrupts(); // enable all interrupts
}

ISR(TIMER0_COMPA_vect){
  digitalWrite(outputPin,digitalRead(outputPin)^1); //Toggle pin
}

我正在使用示波器读取引脚上的电压,当我更改OCR0A的值时,没有任何变化。我不确定为什么没有变化。我还想补充说没有错误。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

首先,混合使用TIMER0端口和TIMER1位常数,使用WGM0x,CS0x等。您尝试将CTC模式设置为TIMER1。对于TIMER0,设置TCCR0A端口的WGM01位:

TCCR0A  = 1 << WGM01;
TCCR0B  = 1 << CS02 | 1 << CS00;
OCR0A   = <Max counter value>;

TIMSK0 |= 1 << OCIE0A;