Timer2, Undestanding OCR2A and OCR2B (Setting 36kHz frequency)

That's my first post and question, so, Hello :slight_smile:

I built simple remote and receiver based on two Unos. Actually that's the same like in my latest video.

I used IRRemote lib to support IR transmission from remote to receiver. But I wanted to learn more and wanted to implement this lib by myself just to work with RC-5 standard. Everything is nice, and sending works on digital pin 3, but when it comes to configuring Timer2 I am a bit confused about it.

So, here is how it look like in my implementation based on IRRemote lib (emitter needs 36kHz frequency):

// Initialize Timer2
TCCR2A = 0;
TCCR2B = 0;

// Set mode 5, PWM phase correct mode, counts up and down
bitSet(TCCR2A, WGM20);
bitSet(TCCR2B, WGM22);

// Set up 1 prescale
bitSet(TCCR2B, CS20);

// Set frequency 36kHz
OCR2A = 222;

OCR2B = 74;

So, what I understand from it now is:

  1. TCCR2A and TCCR2B are timer counter control registers A and B

  2. Setting WGM20 and WGM22 based on this document is setting PWM Phase Correct mode, which is causing atmega328p (am I right?) to generate triangular wave. So it goes from low to high and to low, and it takes twice more time than Fast PWM.

  3. Prescale is set to 1 and OCR2A is set to 222 to make timer work in 36kHz frequency.
    16MHz / 1 / 222 / 2 (because of PWM Phase Correct) = 36 036

  4. OCR2B set to 74. Without this my IR emitter diode sends some wrong values and receiver cannot decode it.

Question 1: What is OCR2B used for? Setting OCR2A to 222 gives me nice 36kHz (and IMO it should work for pin 3 or pin 11 if it is responsible for one of these pins, If not then I missed something and could you help me understand that?).
Question 2: Is OCR2A used to set frequency on pin 3 and OCR2B to set pin 11? If not, what I misunderstood?

Thank you in advance for answers!

Some additional info that might be important:

On the receiver I have set Timer2 to this:

// Some #defines
// MICROSECONDS_PER_TICK 50
// TIMER_CLOCK_CYCLES_LIMIT = 16Mhz * MICROSECONDS_PER_TICK / 1000000Β  // this is 800

TCCR2A = _BV(WGM21);
TCCR2B = _BV(CS21);
OCR2A = TIMER_CLOCK_CYCLES_LIMIT / 8; // this is 100
TCNT2 = 0;

I also tried to undrstand this document but I don't know why we need to set anything in OCR2B.

Also I read these documents already (and probably many more):
https://arduino-info.wikispaces.com/Timers-Arduino

https://withinspecifications.30ohm.com/2014/02/20/Fast-PWM-on-AtMega328/
https://sites.google.com/site/qeewiki/books/avr-guide/pwm-on-the-atmega328

http://forum.arduino.cc/index.php?topic=379240.0
http://www.avrfreaks.net/forum/timer2-phase-correct-pwm-mode-atmega168

In the mode you selected, OCR2A is the 'TOP' value at which the timer starts counting down. This sets the frequency. In the mode selected, OCR2B is the PWM value. When the timer is below OCR2B the output pin is HIGH and when the timer is above OCR2B the output pin is LOW. You set it to 1/2 of TOP (111) to get a square wave. Setting it to 77 will give you shorter ON pulses.

If you want two PWM outputs from the timer you have to use a different register for TOP or use one of the fixed values.

@johnwasser, thank you for answer! I have some following thoughts and questions that you might help me with :slight_smile:

I took a look in datasheet of Atmega328p this time, and read that OCR2A is compared with TCNT2 which is the counter of Timer2. In datasheet I see the OCR2B has identical description like OCR2A.

I conducted an experiment and see that no matter what I set between 3-200 works fine.

Also, I forgot to mention that when the high state need to be set on pin 3 for IR emitter, I do it like this:

TCCR2A |= _BV(COM2B1); // set 1 on COM2B1 bit

And disables with:

TCCR2A &= ~(_BV(COM2B1))

And what I see is that in this moment following flags are in following state:
COM2A1 = 0, COM2A0 = 0, COM2B1 = 1, COM2B0 = 0.
Which means: Clear OC2B on Compare Match when up-counting. Set OC2B on Compare Match when down-counting. (OC2B is PD3 which states for digital pin 3)

  1. What does it mean in practice? What clear and set means? What is set on this OC2B output? Is this equal to: Low and High state, so there is 0V and 5V (or rather 0% and 222.0 / 255.0 which is 87% duty cycle?)

  2. Also, what you described about being below and above OCR2B value look like the generated wave is inverted? Am I right? Shouldn't it be "when timer is above OCR2B output is high"? - This is what I understood from this article about PWM modes.

[quote author=tomkowz date=1479058970 link=msg=2999266]1) What does it mean in practice? What clear and set means? What is set on this OC2B output? Is this equal to: Low and High state, so there is 0V and 5V (or rather 0% and 222.0 / 255.0 which is 87% duty cycle?)

"clear" means turn the pin off/LOW/0V
"set" means turn the pin on/HIGH/5V
If you use a PWM value of 77 then the output will be on for 78/223rds of the time, about 35% duty cycle.

tomkowz:
2) Also, what you described about being below and above OCR2B value look like the generated wave is inverted? Am I right? Shouldn't it be "when timer is above OCR2B output is high"? - This is what I understood from

If the pin was off below the value and on above the value the duty cycle would get LOWER as the PWM value got higher.

"Clear OC2B on Compare Match when up-counting." As the timer is counting up from 0 to TOP it turns the pin OFF when the timer matches Output Compare Register B.

"Set OC2B on Compare Match when down-counting." As the counter is counting back down from TOP to 0 it turns the pin ON when the timer matches Output Compare Register B.

@johnwasser, thanks for your answers!

johnwasser:
If the pin was off below the value and on above the value the duty cycle would get LOWER as the PWM value got higher.

Yeah, you're right. I just draw it on a paper and see how it behaves. Higher OCR2B means higher duty cycle.

johnwasser:
"Clear OC2B on Compare Match when up-counting." As the timer is counting up from 0 to TOP it turns the pin OFF when the timer matches Output Compare Register B.

"Set OC2B on Compare Match when down-counting." As the counter is counting back down from TOP to 0 it turns the pin ON when the timer matches Output Compare Register B.

Okay, I see this after I draw it. It makes sense now :slight_smile:

  1. probably last question, about OCR2B. In my case as I wrote before, I can set OCR2B between 3-200 and everything works fine with OCR2A set to 222. When is it worth to set some specific value for OCR2B? Could you give any example for that?

  2. I see you wrote in your answer 78 and 223 instead of 77 and 222. I read in this article that with PWM Phase Corrected there is no off-by-one. Is the article correct with that statement?

Phase-correct PWM divides the frequency by two compared to fast PWM, because the timer goes both up and down. Somewhat surprisingly, the frequency is divided by 255 instead of 256, and the duty cycle calculations do not add one as for fast PWM. See the explanation below under "Off-by-one".

And the last question. My IR emitter works on Timer2 configured to 36kHz, and I noticed the IR receiver has setup Timer2 on second circuit to work on 20kHz, but diode is 36kHz.

Is there any implication of having these timers work in different speed? Can it cause sometimes missing some signals emitted by IR emitter? I set timers on both circuits to work on 36kHz and I see it works correctly.

This configuration is taken from IRRemote library and now I am wondering why it was set this way.

tomkowz:

  1. probably last question, about OCR2B. In my case as I wrote before, I can set OCR2B between 3-200 and everything works fine with OCR2A set to 222. When is it worth to set some specific value for OCR2B? Could you give any example for that?

I assume different lengths of pulse will give different total power output and may change the usable distance.

tomkowz:
2) I see you wrote in your answer 78 and 223 instead of 77 and 222. I read in this article that with PWM Phase Corrected there is no off-by-one. Is the article correct with that statement?

Sorry that I did not do the research and just assumed there would be the same off-by-one thing. I'm not getting paid to do this. :slight_smile:

tomkowz:
And the last question. My IR emitter works on Timer2 configured to 36kHz, and I noticed the IR receiver has setup Timer2 on second circuit to work on 20kHz, but diode is 36kHz.

Is there any implication of having these timers work in different speed? Can it cause sometimes missing some signals emitted by IR emitter? I set timers on both circuits to work on 36kHz and I see it works correctly.

This configuration is taken from IRRemote library and now I am wondering why it was set this way.

You can't run the timer at two different speeds at the same time. If two parts of the code use the same timer for different things they will conflict and likely one (or both) won't work. I believe the IRremote library can only send or receive, not both at the same time. It is unlikely that you could send and receive at the same time without interference unless your sender and receiver are in different rooms.

Thanks for your answers!

The situation I described about two different frequencies is the case when you have two Arduinos, and one works with Timer2 set to 36kHz and receiver circuit has Timer2 set to 20kHz. So, one circuit sends signal and one receives signal. Now it should be clear :slight_smile:

On the sending side the timer is used to generate the carrier frequency: typically 36 to 40 kHz. On the receiving side it it used to sample the demodulated signal at 50 microsecond (20 kHz) intervals. Each pulse is tens or hundreds of samples (hundreds or thousands of microseconds). Two completely different time scales.

Thank you good sir :slight_smile: