Hello, everybody!
I am trying to set up the timer2 on my arduino, and flip a pin in the interrupt service routine, but what i am reading on the oscilloscope is not what i expect to (I would expect a square wave). I attach th reading to this post. Please could somebody help me to find my bug?
#include <avr/io.h>
#include <avr/interrupt.h>
void setup() {
pinMode(2, OUTPUT);
// setup CTC
TCCR2A &= ~(_BV(WGM20));
TCCR2A |= _BV(WGM21);
TCCR2B &= ~(_BV(WGM22));
// set prescaler to 1
TCCR2B &= ~(_BV(CS22) | _BV(CS21));
TCCR2B |= _BV(CS20);
// set output compare register
OCR2A = 128;
// enable the timer interrupt
sei();
TIMSK2 |= _BV(OCIE2A);
}
void loop() {
}
ISR(TIMER2_COMPA_vect) {
PORTD ^= _BV(2); // flip pin 2 on port D
}
Thanks in advance.
