That's my first post and question, so, Hello
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:
-
TCCR2A and TCCR2B are timer counter control registers A and B
-
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.
-
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 -
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