【问题标题】:Arduino interrupt output compare register not working as expectedArduino中断输出比较寄存器没有按预期工作
【发布时间】:2016-04-12 06:24:45
【问题描述】:

我正在尝试使用中断来切换与 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 的值时,没有任何变化。我不确定为什么什么都没有改变。我还想补充一点,没有错误。

感谢您的帮助!

【问题讨论】:

    标签: arduino arduino-uno


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢你,但是当我改变 OCR0A 的值时,示波器上的周期没有改变,所以我的问题仍然存在。
    猜你喜欢
    • 2019-11-27
    • 2018-01-21
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多