Please let us in the comment zone any suggestions that you think will improve the article!

If you like the article **click the follow button** from social media to stay in touch with us!

Like in the previous posts we start with **Timer 2**, mentioning that it is a **8 bit** timer, so it can count up to **255**, like Timer 0. Timer 2 manage **pin 3 and 11** and to configure it we must make changes in the registers.

The specific registers for Timer 2 are:

**TCCR2A-**it has two of three bits needed to chose the mode of the PWM(fast pwm, phase correct pwm etc) and the bits who manage the mode to set pins high or low;

**TCCR2B-**it has the bits needed to choose the prescaler and the other one bit needed to choose the mode of PWM

**OCR2A and OCR2B** registers with bits for the duty cycle or the count value for pins 11(OCR2A) and 3(OCR2B).

Next we will take Timer 2 and put it in the **CTC mode** and in the image below is the timing diagram for the CTC mode(from atmega datasheet):

In this mode Timer 2 counts until reach the OCR2A value after that it is cleared to 0, so the OCR2A takes the **TOP value**. In this mode if you want to make something fast you can use an interrupt and with the value of OCR2A you can control that event very precise(let’s say if you want to change a value at every 1 ms, must calculate the OCR2A to have a signal with 1 ms period).

In the images below we have the TCCR2A, TCCR2B registers and the **bit mode description for CTC mode**(from atmega datasheet)

So to put Timer 2 in CTC mode we must make the **WGM21 bit** from the TCCR2A register equals 1. Also, to have a signal on pin 11 the bit **COM2A0** from the TCCR2A must be 1.

To calculate the output frequency we use this formula **f _{OC2A}=f_{clk}/2*N(1+OCR2A)**, where

**OC2A**represent the digital pin 11.

For first example we have **OCR2A=255** (max because it’s a 8 bits timer) and with the formula above we should obtain a **f=31250Hz**.

Next program do that:

void setup() {

pinMode(3, OUTPUT);

pinMode(11,OUTPUT);

TCCR2A=0;//reset the register

TCCR2B=0;//reset the register

TCNT2=0;

TCCR2A=0b01010010;//COM2A1, COM2B1 are 0, COM2A0, COM2B0 are 1

//also WGM21 is 1

TCCR2B=0b00000001;//WGM22 is 0 with no prescaler

OCR2A=255;// compare match value

}

void loop() {

// put your main code here, to run repeatedly

}

On the oscilloscope we have:

Now if we make **OCR2A=0**, from the formula we get exact **8Mhz**.

The program:

void setup() {

pinMode(3, OUTPUT);

pinMode(11,OUTPUT);

TCCR2A=0;//reset the register

TCCR2B=0;//reset the register

TCNT2=0;

TCCR2A=0b01010010;//COM2A1, COM2B1 are 0, COM2A0, COM2B0 are 1

//also WGM21 is 1

TCCR2B=0b00000001;//WGM22 is 0 with no prescaler

OCR2A=0;// compare match value

}

void loop() {

// put your main code here, to run repeatedly

}

Result from oscilloscope:

But what is the lowest frequency we can obtain in this mode from timer 2???

Well N the prescaler(image below from atmega datasheet) can take a maximum value equal with **1024** and using the formula with an OCR2A=255 to count for much time the resulting frequency is **f=30.517Hz**.

The program to obtain the frequency above is:

void setup() {

pinMode(3, OUTPUT);

pinMode(11,OUTPUT);

TCCR2A=0;//reset the register

TCCR2B=0;//reset the register

TCNT2=0;

TCCR2A=0b01010010;//COM2A1, COM2B1 are 0, COM2A0, COM2B0 are 1

//also WGM21 is 1

TCCR2B=0b00000111;//WGM22 is 0 with 1024 prescaler

OCR2A=255;// compare match value

}

void loop() {

// put your main code here, to run repeatedly

}

The result:

So far we modified the OCR2A value but how we do if we have the frequency and we need the value for OCR2A???

Ok if we rearrange the formula we have **OCR2A=( f _{clk}/2*N* f_{OC2A})-1**, and if you introduce the frequency you obtain the OCR2A value.

For example let’s say we want a 100Hz frequency, first we try the formula without prescaler and we have OCR2A=(16000000/2*1*100)-1, the result is OCR2A=79999. This is not work because the OCR2A is a 8 bits register and max value for it is 255.

This means we need a prescaler and choose 256, the result is OCR2A=311.5 also too big.

Only option left is 1024 and with that value OCR2A =77.125.

For a final remark about CTC mode we must say that with **Timer 1** we can obtain many others frequency because it is a 16 bits register and it counts from **0 to 65355** not from **0 to 255** like **Timer 0 or Timer 2**.