Mega328 timer 2 interrupt

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As soon as I set the TIMSK to enable timer 2 interrupt, my program stops working, so obviously, I've overlooked something:

 

Setup routine:

void setup()
{
  pinMode(steadyPin, OUTPUT);
  pinMode(flashingAPin1, OUTPUT);
  pinMode(flashingBPin1, OUTPUT);
  pinMode(flashingAPin2, OUTPUT);
  pinMode(flashingBPin2, OUTPUT);
  pinMode(servoPin, OUTPUT);
  pinMode(detectLeftPin, INPUT_PULLUP);
  pinMode(detectMiddlePin, INPUT_PULLUP);
  pinMode(detectRightPin, INPUT_PULLUP);
  pinMode(IRLED, OUTPUT);
  pinMode(12, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  digitalWrite(speakerPin,LOW);
  pinMode(speakerPin, OUTPUT);

  pinMode(A0, INPUT_PULLUP);
  pinMode(A1, INPUT_PULLUP);
  pinMode(A2, INPUT_PULLUP);
  pinMode(A3, INPUT_PULLUP);
  pinMode(A4, INPUT_PULLUP);
  pinMode(A5, INPUT_PULLUP);
  pinMode(A6, INPUT_PULLUP);
  pinMode(A7, INPUT_PULLUP);

  gates.attach(servoPin);
  gates.write(GatesUp);

        // Timer 2 does 38KHz output to IR LEDs on D11
        // and Sound on D3
        TCCR2A = _BV(WGM20) | _BV(WGM21) | _BV(COM2A0) | _BV(COM2B1) ;
        TCCR2B = _BV(CS20) | _BV(WGM22) ;
        OCR2A = (F_CPU/(IRFrequency*2L)-1);
        OCR2B = 0;
       // TIMSK2 = _BV(TOIE2);

  State = StateClear ;
}

And my ISR:

 

volatile uint8_t InterruptCount = 0 ;
ISR(TIMER2_TOIE_vect)
{
  InterruptCount += 1 ;
}

 

(Everything that could complicate the test has been stripped).

 

If I don't set TIMSK2, the loop function runs normally. I thought maybe I had he wrong vector, but that looks right to me. I get the 38KHz output with the interrupt enabled or disabled.

 

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead. 

Last Edited: Mon. Jul 3, 2017 - 04:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which timers does Arduino use for its own use then?

 

EDIT: OK so found this in the core Arduino files...

 

https://github.com/arduino/Ardui...

 

That seems to suggest it uses 1 and 2 for PWM?

Last Edited: Mon. Jul 3, 2017 - 04:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Torby wrote:
I thought maybe I had he wrong vector, but that looks right to me.
TOIE in the vector name? I doubt that being correct.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

TOIE in the vector name? I doubt that being correct.

+1

 

Should be:

ISR(TIMER2_OVF_vect)

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bingo! Stefan wins the banana.

 

HAD to be something simplistic.

 

ISR(TIMER2_OVF_vect)

 

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And now, with one other mistake found, my sketch works!

 

Detects train with IR detectors. That's why the 38KHz signal.

Flashes the lights.

Lowers the gate using the servo library. That's why I'm using Timer 2 for the sound too.

Goes DING DING DING DING while arm is down and lights are flashing.

 

All in 17% of the Arduino's memory, including the sound clip of the ding.

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think that you want to use

TIMER2_COMPA_vect

 

The overflow interrupt triggers when the timer2 count goes from 0xff to 0x00.   I believe that you want the interrupt to happen when the count reaches the compare register value.  The interval is the 1/16MHz * ( the value in the compare register) starting from zero.    The interrupt interval period is 62.5 nanoSeconds  (or 1/16,000,000) * 256 (timer 2's max count) when using TIMER2_OVF_vect because timer 2's counter is never set to a value from which it counts upwards to the overflow.
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:
The overflow interrupt triggers when the timer2 count goes from 0xff to 0x00.
Nope. At mode 7 it triggers when the timer reaches TOP (set by OCR2A).

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta, he's using Mode 7:

        TCCR2A = _BV(WGM20) | _BV(WGM21) | _BV(COM2A0) | _BV(COM2B1) ;
        TCCR2B = _BV(CS20) | _BV(WGM22) ;

 

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm using mode 7.

 

Even if I have it wrong, it's working nicely

 

 

Arduino has 3 timers.

 

0 for the time base.

1 for the servo library.

2 for the 38KHz flashing IR led.

 

Oops, what do I use for the sound? OCRB of channel 2. It can only go 0 - 209, so I had a C# program scan the wave file, extract and scale the samples into C to copy/paste into the source. Since my interrupt happens at 76KHz, I count off 9, then 10 interrupts to fetch the next sample and put it to OCR2B.

 

 

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Torby wrote:
HAD to be something simplistic.
Did the compiler not emit a warning about "misspelled vector name" ? This just shows why you should never ignore any warning until you are certain it is benign.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did the compiler not emit a warning about "misspelled vector name" ? This just shows why you should never ignore any warning until you are certain it is benign.

The Arduino IDE emits the warning, but doesn't use -Werror when building, so it proceeds to program the target.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]