... continues from previous post
so by writing
sbi(TCCR2, COM21); //sets to 1 the COM21 bit inside the TCCR2 register
cbi(TCCR2, COM20); //sets to 0 the COM20 bit inside the TCCR2 register
you are performing the third option "Clear OC2 on Compare Match when up-counting. Set OC2 on Compare Match when downcounting."
now comes the difficult part...what the hell does that mean??

I'm not sure if I got it right...I read "Modes of Operation" (page 107-109)
and "Phase Correct PWM Mode" (page 111) and its diagrams, and I understood that, as you write in the comments
the Phase Correct PWM Mode is a safer mode for motors

(hehehe not much

hey)
I also found the OCR2 register quoted in this explanation...
the OCR2 that you set inside the timer2PWMSet() func
by reading the OCR2 definition - Output Compare Register in page 117 you discover that:
"The Output Compare Register contains an 8-bit value that is continuously compared
with the counter value (TCNT2). A match can be used to generate an Output Compare
interrupt, or to generate a waveform output on the OC2 pin."
This makes me think that (more or less

) the PWM function works by comparing a desired output value stored in the OCR2 register
with the value of the Timer TTCR2...is that right?
and that TTCR2 and OCR2 are reserved for the specific PWM2 pin
in fact that is more or less what you are doing in the analogWrite Function inside lib/targets/arduino/wiring.c:
analogWrite(int pin, int val) {
...
timer1PWMAOn();//activate the mode for pwm to the pwm specific pin (pin10 PWM1)
timer1PWMASet(val);//set the desired value to the pwm specific pin
...
}
//-----------------------
MODULO % EXPLANATION
//-----------------------
in the loop you write
i = (i+1) % 256;
the % symbol is called "modulo" and it's a C operator that is used here to loop the values from zero to maximum 256, once reached the maximum value the count starts again from 0.
from Wikipedia "Modulo operation" -
http://en.wikipedia.org/wiki/Modulo_operationGiven two numbers, a and n, a modulo n (abbreviated as a mod n) is the remainder, on division of a by n.
10 % 6 = 4 -> read 10 modulo 6 = 4
this is because 6 into 10 stays 1 time with the remainder of 4

(sorry for the italian version of it

)
but the cool thing is that "Some programming languages, such as ANSI C, don't define a result if either of n or a is negative" (quote from Wikipedia).
this means that if you write
i = (i+1) % 256;
for any i<256 the %256 part of the expression won't return any value,
but for i=256 the operation will return 0 (i = the remainder of 256/256 = 1 remainder 0 --> i=0;)
hence starting again the loop.
that is one cool function

I whished I'd knew that before

in the loop you are basically storing in the OCR2 register values from 0 to 255 increasing 1 every 10 milliseconds.
//---------------------------------
CBI and SBI & OPEN QUESTIONS
//---------------------------------
this is where I'm getting a bit lost...
I tracked down a few things...but I'm still missing the inner working of the _SFR_BYTE(sfr) function...
I've tracked down the functions inside
arduino03Dir/tools/avr/avr/include/avr/sfr_defs.h
and the numeric values for the TCCR2 WGM20 WGM21 COM21 COM20 constants
inside
arduino03Dir/tools/avr/avr/include/avr/iom8.h
I've gotten down the expression line to find out that _SFR_BYTE() does the following:
#define _SFR_BYTE(sfr) (*(volatile uint8_t *)((uint16_t) &((sfr+0x20))))
I'm not sure if that's right though

I've tryed different compiling combinations, trying to rewrite the SFR_BYTE func like that (including typedefs for uint8_t and uint16_t) but couldn't make it work
- can you please explain us how it is making what it does? or at least its general pourpose and usage that we can apply in the future.
the _SFR_BYTE is all over also in the DMX tutorial
http://arduino.berlios.de/index.php/Tutorial/DMXMasterand i think it could help everybody understand it better.
- is cbi setting a bit to 0 and sbi setting a bit to 1 in a specific register?
(I got a bit lost in all that ANDing &=, inverting ~, and shifting (1<<COM21) )
I know that all this trascends the pourpose of the forum...but I'm trying to learn C and microprocessors workings a bit better...it's such a fascinating world

thanks in advance
cheers
b.