// #pragma GCC push_options
// #pragma GCC optimize ("-O3")
int counter = 0;
int* counter_ptr = &counter;
void setup() {
cli(); //stop interrupts for till we make the settings
/*1. First we reset the control register to amke sure we start with everything disabled.*/
TCCR1A = 0; // Reset entire TCCR1A to 0
TCCR1B = 0; // Reset entire TCCR1B to 0
/*2. We set the prescalar to the desired value by changing the CS10 CS12 and CS12 bits. */
TCCR1B |= B00000100; //Set CS12 to 1 so we get prescalar 256
/*3. We enable compare match mode on register A*/
TIMSK1 |= B00000010; //Set OCIE1A to 1 so we enable compare match A
/*4. Set the value of register A to 62500*/
OCR1A = 62500; //Finally we set compare register A to this value
sei(); //Enable back the interrupts
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(counter);
}
//With the settings above, this IRS will trigger each 1s.
ISR(TIMER1_COMPA_vect){
TCNT1 = 0; //First, set the timer back to 0 so it resets for next interrupt
*counter_ptr = *counter_ptr + 1;
}
// #pragma GCC pop_options