How to measure OCR1A timer frequency

I programmed and initialized Timer1 in CTC mode. This is the code for it:

TCCR1A = 0;                     //register reset
TCCR1B = 0;                     //
TCNT1 = 0;                      //

OCR1A = 199;                    //Timer1 80kHZ 
TCCR1B |= (1 << WGM12);         //CTC mode
//TCCR1B |= (0 << CS12) | (0 << CS11) | (1 >> CS10); //no prescale
TCCR1B |= (1 << CS10);          // no prescaler
TIMSK1 |= (1 << OCIE1A);        //
}

ISR(TIMER1_COMPA_vect){         //Interrupt
}

How can I best measure whether the set frequency really comes out?

If you don't trust your hardware nor your code then use a scope to check the output frequency.

1 Like
TCCR1A = B01000000;
DDRB |= B00000010;

this will set pin#15, (arduino #9) PB2, to toggle on compare match, which you can measure against using a frequency counter, or watch on an oscilloscope. the first line turns on the output toggle in the register. The second line sets the pin to output. This is for the ATmega328P

2 Likes

@Perehama thank you. Do you know the pin for a Arduino micro?

according to the datasheet linked on the Micro's product page, OC1A is a pin function of PB5, or arduino pin#9. so I would write the above line as below and take my measurement on "9".

TCCR1A = B01000000;
DDRB |= B00010000;

Looking at TCCR1A register more closely, you can break it up into 8 bits. 4 different features of Timer/Counter1 are programmed using this register's bits in pairs. The 8th and 7th bit set OC1A to toggle on compare match, 8th set to 0 and 7th to 1. OC1B is set in the same way by bits 6 and 5. OC1C is set by bits 4 and 3. OC1A is a pin function of PB5, OC1B is a pin function of PB6(arduino 10). OC1C is a pin function of PB7(arduino 11), so you can turn all 3 to toggle and set all three as outputs as follows:

TCCR1A = B01010100;
DDRB |= B01110000;

Or, if you have a use for one of these pins elsewhere you can have either A, B or C toggle.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.