Reading someone else's code, PWM and strange low-level code shenanigans!

So I'm doing some research in preparation for a new project and I found some relevant code to help. I'm a bit confused however about some syntax used in the code that looks pretty low-level. It starts with this in the setup():

// Setup PWM: f=38Khz PWM=0.5  
  byte v = 8000 / 38;
  TCCR2A = _BV(WGM20);
  TCCR2B = _BV(WGM22) | _BV(CS20); 
  OCR2A = v;
  OCR2B = v / 2;

What? Then later an LED attached to pin 3 needs to be flashed and to do so, the code looks a little something like this:

// "Header" sequence
void sendHeader() {

  TCCR2A |= _BV(COM2B1);
  delayMicroseconds(HEADER_DURATION);
  TCCR2A &= ~_BV(COM2B1);
  delayMicroseconds(HEADER_DURATION);
  TCCR2A |= _BV(COM2B1);
  delayMicroseconds(HIGH_DURATION);
  TCCR2A &= ~_BV(COM2B1);

}

It seems to be that TCCR2A |= _BV(COM2B1); is the same as digitally writing pin 3 HIGH and TCCR2A &= ~_BV(COM2B1); writes it low. But what the heck is this? Some alternative way to reference pins that can get around some weird PWM frequency or duty cycle restrictions? What's up with those bitwise operators? Anybody that could help decode and explain this for me would be of great help.

The I/O pins of the arduino are mapped to several byte-sized registers, and you can control the value of the pins by setting and clearing the corresponding bits in those registers.

Those names are names of registers inside the processor chip. If you want to see what they do then you have to read the processor's data sheet. These are the registers that control the timer and that can work in a few differant modes.

This is for some sort of IR remote control?

The first code segment looks like it sets up timer 2 to output a 38kHz (typical for IIRC) out of the associated PWM pin.

The second code segment looks like it turns the timer On (output 38kHz signal) and Off (output nothing) for time periods that are also typical of IRRC, by setting or clearing bits in the timer control register. So instead of turning the pin on and off, it's outputting 38kHz or not...

For exact explanations, you'll have to refer to the datasheet. For instance, the "v = 8000/38" line implies (to me) that it's expecting to run on an 8MHz board (8000 kHz = 8MHz), but there could be other factors involved.

westfw:
This is for some sort of IR remote control?

The first code segment looks like it sets up timer 2 to output a 38kHz (typical for IIRC) out of the associated PWM pin.

The second code segment looks like it turns the timer On (output 38kHz signal) and Off (output nothing) for time periods that are also typical of IRRC, by setting or clearing bits in the timer control register. So instead of turning the pin on and off, it's outputting 38kHz or not...

For exact explanations, you'll have to refer to the datasheet. For instance, the "v = 8000/38" line implies (to me) that it's expecting to run on an 8MHz board (8000 kHz = 8MHz), but there could be other factors involved.

Yes it is for IRRC. So essentially what you're saying is that timer 2 is controlling pin 3's PWM by pulsing it at 38khz for a time interval, then turning it off for a time interval, etc.? Why couldn't something like this be able to be done with a simple analogwrite()?

It could be but analogue write is not normally that fast and when you make it that fast you don't have the full 255 range. Also analogWrite is a bit slow, accessing the registers make the width affect syncronised with a whole number of cycles.

You could theoretically change the PWM frequency of one timer to 38kHz, and then use "analogWrite(pin, 0)" to turn it off an "analogWrite(pin, 128)" to turn it on, but analogwrite in general is for changing the duty cycle of the output wave form, and not its frequency or on/off-ness. Also, the Arduino analogWrite manages to get two outputs per timer by having the counters wrap around, which probably limits the frequencies obtainable to values that do not include 38kHz.

westfw:
Also, the Arduino analogWrite manages to get two outputs per timer by having the counters wrap around, which probably limits the frequencies obtainable to values that do not include 38kHz.

No, the two outputs per timer is just how the PWM hardware is, on Mega some timers have 3 PWM outputs,
analogWrite is just setting the duty cycle register for the relevant pin.

No, the two outputs per timer is just how the PWM hardware is

PWM ties one of the "output compare registers" to an output pin. In order to do PWM at arbitrary frequencies (instead of just (clock/prescaler/)256), you'd have to use one the the OCRs as "top" instead, making it unavailable for meaningful PWM.
(This gets annoying on chips like the MSP430, where all of the timers are 16bit, and you always need an OCR to get a useful PWM frequency; on a chip with "one TIMER_A with two CCRs" (like the chip in the original LaunchPad), you can only do one PWM output.)