Hello, i'm trying to get use the timer 2 for launch an interrupt on a 328p. Is being impossible. I don’t know what I’m doing wrong but i’m becoming crazy this issue (I did read and re-read the datasheet, the part of WGMS's is not enough clear for timer 3, the table talks about WGM2,1,0, and not for WGM22,21 or 20, beign WGM21 and 210 on TCCR2A and WGM22 on TCCR2B). I think that all is fine setting, but the preescaler don’t works.
If I modify the the CS22|20, I always obtain the base frequency: 35.97 Khz
Please can anyone help me?
// Toggle LED on pin 7 in frecuency
#include <avr/io.h>
#include <avr/interrupt.h>
#define LEDPIN 7
void setup() {
pinMode(LEDPIN, OUTPUT);
cli(); //Disable global interrupts
TCCR2A = 0;
TCCR2B = 0;
OCR2A = 1; // match value
//CTC mode ON
TCCR2A |= (1 << WGM21); //I suppose that the operation means set to 1 WGM21 bit.
// Configuring preescaler dividing by 128
TCCR2B &= ~(1 << CS20);
TCCR2B &= ~(1 << CS21);
TCCR2B |= (1 << CS22);
// enable interrupt on bit
TIMSK2 |= (1 << OCIE2A);
sei(); //enable global interrupts
Serial.begin(9600);
Serial.print("the preescaler don't works");
}
void loop() {
}
ISR(TIMER2_COMPA_vect)
{
digitalWrite(LEDPIN, !digitalRead(LEDPIN));
}
Greetings.