EDIT2SOLUTION 1 I've commented the line cont=0; in the if statement and I get the following output.Thanks to Gerben
Setupvolatile completedint cont = 0;
void ticksetup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
// Initialize Timer
cli(); // disable global interrupts
TCCR3A = 0; // set entire TCCR3A register to 0
TCCR3B = 0; // same for TCCR3B
// set compare match register to desired timer count: 1800 Hz
OCR3A = 20; // First20; output//800Hz 5; // 3 Hz
tick // turn on CTC mode:
TCCR3B |= (1 << WGM32);
tick // Set CS10 and CS12 bits for 1024 prescaler:
TCCR3B |= (1 << CS30) | (1 << CS32);
tick // enable timer compare interrupt:
TIMSK3 |= (1 << OCIE3B);
// Secondenable global interrupts:
tick sei();
Serial.println("Setup completed");
}
void loop()
{
delay(1000);
Serial.println(cont);
cont = 0;
}
ISR(TIMER3_COMPB_vect)
{
cont++;
}
SOLUTION 2 Thanks to BrettM
volatile int cont = 0;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
// Initialize Timer
cli(); // disable global interrupts
TCCR3A = 0; // set entire TCCR3A register to 0
TCCR3B = 0; // same for TCCR3B
// set compare match register to desired timer count: 1800 Hz
tick OCR3B = 20; //800Hz 5; // 3 Hz
// turn on CTC mode:
//TCCR3B |= (1 << WGM32);
tick // Set CS10 and CS12 bits for 1024 prescaler:
TCCR3B |= (1 << CS30) | (1 << CS32);
// Third
enable ticktimer compare interrupt:
TIMSK3 |= (1 << OCIE3B);
tick // enable global interrupts:
1 sei();
Serial.println("Setup completed");
}
void loop()
{
Serial.println(cont);
cont = 0;
delay(1000);
}
ISR(TIMER3_COMPB_vect)
{
TCNT3 = 0;
cont++;
}
I don't know why the if statement prints 3 lines instead of one.