# Toggle OC1A, OC1B and OC1C using timer in CTC mode

Most AVR microcontrollers got three dedicated pins for each timer (for example, OC1A, OC1B, and OC1C) that can be toggled automatically when the counter reaches the so called top value in the corresponding three registers (OCR1A, OCR1B, and OCR1C).

I'm not sure how can those three dedicated pins/registers operate in CTC mode for different independent top values, and this is what I want to accomplish. I want to toggle OC1A, OC1B and OC1C (though "output compare", not through interrupt or something like that) at different intervals (to see the having three dedicated registers and pins).

Let's say I want to program the AVR timer to toggle the pins OC1A, OC1B and OC1C at different intervals (for example 1 second, half second, quarter a second), but it does not work as I expect. I set compare match register A and B and C with the three different value, having the greatest value on A (I also tried having it on C).

Only OC1A is toggled at the needed frequency.

To me it seems that if they made only OC1A top clears the counter then it would be possible to have the three pins work at different three independent values. Is this right?

Here is the segment I put in my Arduino Lenoardo's (based on ATmega32U4) setup. I left loop() empty.

  // Initialize timer1
noInterrupts();           // Disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCCR1C = 0;
TCNT1  = 0;

OCR1A = 62500;            // Compare match register 16 MHz/256/1 Hz
OCR1B = 31250;            // Compare match register 16 MHz/256/2 Hz
OCR1C = 15625;            // compare match register 16 MHz/256/4 Hz

// Toggle OC1A, OC1B and OC1C => (on Lenoardo) PB5, PB6 and PB7 => digital 9, 10, 11
// TCCR1A = 0b01010100;
TCCR1A |= (1 << COM1C0) | (1 << COM1B0) | (1 << COM1A0);
// TCCR1B = 0b1100
TCCR1B |= (1 << WGM12);   // CTC mode
TCCR1B |= (1 << CS12);    // 256 prescaler

interrupts();             // Enable all interrupts

• Which AVR are you dealing with? – Ignacio Vazquez-Abrams Nov 30 '13 at 20:11
• What is the needed frequency and what is the actual frequency? – jippie Nov 30 '13 at 21:01
• @jippie it's not about a specific frequency nor chip, it's about how to use any AVR to have pins OC1A and OC1B automatically periodically toggling at different values for OCR1A/OCR1B – Muayyad Alsadi Dec 1 '13 at 7:12
• They're still tied to the same timer. I'm not sure why you think that they can have different periods. – Ignacio Vazquez-Abrams Dec 1 '13 at 8:35
• @ignacio-vazquez-abrams 3 different registers and 3 different pins, what a waste if they can have anything different. can they have different phase ? any thing? – Muayyad Alsadi Dec 1 '13 at 8:40