- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1
 
Timer2
Timer2 is used to generate a 500kHz clock source for the CCD. As that is a pretty high frecuency a small timer can be used, that is why the 16-bit Timer1 is leaved for the longer SI line toggling.
The code distinguish from big and small ATmega because output A from Timer2 has a different pin number in each of them.
WGM20,21,22 = 1: selects mode 7 that generates PWM, the timer starts from 0 and overflows when reaching the OCR2A we set.
OCR2A = 15: sets the value we want the timer to overflow.
COM2A0 = 1: each time the timer overflows, pin A from Timer2 toggles, thus we get a fixed 50% duty cycle PWM.
CS20 = 1: sets the prescaler to 1, so it divides the microcontroller frequency (16MHz for Arduino) by 1.
These registers are all we need to configure and the output frequency will be:
[F_{out}=\frac{\frac{f_{clk_I/O}}{prescaler}}{2(OCR2A+1)}=\frac{\frac{16MHz}{1}}{2(15+1)}=0.5MHz]
More information on Timer2 and its registers can be found in the ATmega datasheet from page 187 to 191.