2
\$\begingroup\$

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
\$\endgroup\$
7
  • \$\begingroup\$ Which AVR are you dealing with? \$\endgroup\$ Commented Nov 30, 2013 at 20:11
  • \$\begingroup\$ What is the needed frequency and what is the actual frequency? \$\endgroup\$
    – jippie
    Commented Nov 30, 2013 at 21:01
  • \$\begingroup\$ @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 \$\endgroup\$ Commented Dec 1, 2013 at 7:12
  • 1
    \$\begingroup\$ They're still tied to the same timer. I'm not sure why you think that they can have different periods. \$\endgroup\$ Commented Dec 1, 2013 at 8:35
  • \$\begingroup\$ @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? \$\endgroup\$ Commented Dec 1, 2013 at 8:40

2 Answers 2

2
\$\begingroup\$

A timer in normal, Fast PWM, or CTC mode counts directly from BOTTOM to TOP if no external influence is applied. As it passes each OC value it emits an interrupt and/or frobs a pin, as it is configured to do. Each OC value is only crossed once, which means that all OCs for the same timer all run at the same frequency.

They can be configured to have different phases or to operate in differential pairs based on the values of OCRnr and the COMnrb bits, but the only way to directly have them use different frequencies is to manipulate their OCR values each time their ISR is invoked.

The easiest way to have the pins change at different frequencies would be to set the timer to the GCF/LCM of the various periods/frequencies desired, maintain a count of cycles elapsed, and then frob the pins when the appropriate number of cycles have passed.

\$\endgroup\$
1
  • \$\begingroup\$ To be clear, the statement "three dedicated pins for each timer (for example, OC1A, OC1B, and OC1C)" in the question is not true. OC1A, OC1B, and OC1C are associated with one and only one timer, timer1 (16-bit timer). \$\endgroup\$ Commented Mar 26, 2016 at 14:26
2
\$\begingroup\$

The timer module allows you to generate multiple outputs with different duty cycles, but they all will have the same fundamental frequency. If you want to generate different frequencies, you will need to use a different method. There are two main ways to do this. First method is to use three timers, each operating at a different frequency. If you are trying to generate relatively low frequency outputs (1 kHz or less) then you can use a reasonable timer tick and an interrupt routine. If you want to generate 1 Hz, 2 Hz, and 4 Hz, you can set a timer tick of at least 8 Hz and then count ticks and turn the pins on and off at the right time. With an 8 Hz timer tick, generating a 4 Hz output would require toggling the pin on every cycle, 2 Hz would be every other cycle, and 1 Hz would be every 4 cycles. I would suggest using a slightly higher tick frequency - 1 kHz is pretty reasonable and it would give you the ability to generate a wide range of different frequencies.

\$\endgroup\$
1
  • \$\begingroup\$ I'm trying to understand the use of 3 registers TCCR1A, TCCR1B and TCCR1C and having 3 corresponding pins (OC1A, OC1B and OC1C) to be toggled. why there are 3 dedicated pins, registers and values if only one would practically be used. \$\endgroup\$ Commented Dec 1, 2013 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.