简体   繁体   English

Arduino Timer 2中断和程序触发

[英]Arduino Timer 2 Interrupt and Program Struck

I am using Arduino Mega2560, i have used timer2 as interrupt after 2ms on flag overflow, but somehow it works only once. 我正在使用Arduino Mega2560,在标志溢出2毫秒后使用了timer2作为中断,但是以某种方式它只能工作一次。 I have used serial monitoring as you can also see and this tells me that timer interrupt is invoked but then interrupt is not invoked again and program control does not go back in the loop also since it displays "13" and "22" only once. 您还可以看到,我使用了串行监视,它告诉我定时器中断被调用,但是随后中断不再被调用,程序控制也不会返回循环,因为它只显示一次“ 13”和“ 22”。 At least it should have displayed "22" continously after timer interrupt was called.Can anybody tell me why timer interrupt does not get invoke again and why it does not return to loop after interrupt.? 至少在调用计时器中断后它应该连续显示“ 22”。有人可以告诉我为什么计时器中断不会再次被调用,为什么在中断之后它不会返回循环。

#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);
  }
}

Have you tried taking the 您是否尝试过

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).
}

block and putting some or all of it into the place where you'd like to start the timer again? 阻止并将其全部或部分放入您想重新启动计时器的地方?

setup only runs once, so my guess is you need to start the timer again. setup只运行一次,所以我的猜测是您需要重新启动计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM