0
我正在使用Arduino Mega2560,我已經在標誌溢出2ms後使用timer2作爲中斷,但不知何故它只能工作一次。我已經使用了串行監視,你也可以看到,這告訴我,定時器中斷被調用,但之後中斷不再被調用,程序控制也不會返回到循環中,因爲它只顯示「13」和「22」一次。在定時器中斷被調用後,至少它應該連續顯示「22」。可以有人告訴我爲什麼定時器中斷不再被調用,爲什麼中斷後它不會返回到循環。Arduino定時器2中斷和程序衝擊
#include <avr/io.h>
#include <avr/interrupt.h>
int eraser=0; //0=000
ISR(TIMER2_OVF_vect)
{
TIMSK2=0x00;
TCCR2B=0x00;
TCCR2A=0x00;
TCNT2=0;
TIFR2=0x00;
Serial.println(33);
TCNT2= 131; //reset timer count to 125 out of 255.
TCCR2B=0x6; //using a prescaler of 6 to use divisor 256.
TIMSK2=1; //timer2 Interrupt Mask Register. Set TOIE(Timer Overflow Interrupt Enable).
}
void setup()
{
/*Timer0 would not be used as it is used for other functions.
Timer 5(pin D47) for encoder and interrupt 2(pin 21) for encoder.
Timer2 is 8 bit we'll see if it can be used for time interval between two encoder counts.
This leaves us with Timers 1,3,4,5.
Timer 3(5,3,2) and 4(8,7,6) will be used for motors.*/
Serial.begin(9600);
TCCR2B&=eraser;
TCNT2= 131; //reset timer count to 125 out of 255.
TIFR2= 0x00; //timer2 interrupt flag register: Clear Timer Overflow Flag.
TIMSK2=0x01; //timer2 Interrupt Mask Register. Set TOIE(Timer Overflow Interrupt Enable).
TCCR2B=0x6; //using a prescaler of 6 to use divisor 256.
/*
so it takes 62.5ns per tick in a timer and having set
divisor of 256 so it will take 16usecs per inc and setting
timer2 to 125 would make it count 250 times so
time=62.5nsec * 256 * 125=2msec.
clkTn
TCNTn Timer Counter Register
TCCRnA has LSB of WGM(wave generation mode bits)
TCCRnB Timer Counter Control Register: has LSB of CSn2,CSn1,CSn0.*/
}
void loop()
{
Serial.println(1);
while(1)
{
Serial.println(22);
}
}