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!

The main difference between **Timer 1** and **Timer 0** or **Timer 2** is that it is a **16 bits** timer(instead **8 bits** the others) so Timer 1 can count up to **65355** insteat to **255**. This thing means that Timer 1 has a higher resolution or it can count longer.

Let’s talk about Timer 1 a little! As we mentioned in the previos post Timer 1 manage pins 9 and 10 and for that we must work with some specific registers like:

–**TCCR1A** it has two of four bits needed to choose the mode of the PWM(fast pwm ,phase correct pwm etc) and the bits who manage the mode to set pins high or low

–**TCCR1B** it has the bits needed to choose the prescaler and the other two bits neded to choose the mode of PWM

–**OCR1A and OCR1B** registers with bits for the duty cycle of the PWM signals for **pin 9**(OCR1A) and **pin 10**(OCR1B)

Next we will take Timer 1 and put it in the **phase correct pwm mode**.

In this mode Timer 1 **count** from a **BOTTOM** value to a **TOP**(which can be max in top of the triangle signal or set by OCRnx) value after that it not overflow it **countdown** from TOP value to BOTTOM value and repeat(triangle wave). This mode has half of fast pwm mode frequency.It is preferred in **motors control**(pictures from atmega datasheet).

If in the fast pwm mode we have a counter which is only upcounting and the duty cycle can be adjust only on a half of the triangle signal in this mode the value of the duty cycle is copied also on the other half of the triangle and the signal is preserved more than in the fast pwm mode, so you can adjust much better the signal, higher resolution, but at a low frequency.

For Timer 1 the fast pwm mode is like for timer 0 but with other posibile frequencies because we have the modes with **9 and 10 bits**.

As you can see the pin is put in high state when the counter is downcounting and in the low state when it is upcounting in **non-inverted mode**.

First let’s see the TCCR1A and the TCCR1B registers and the bit description for phase correct mode in the images below(from atmega datasheet):

Before to choose one of these modes in TCCR1A register are four bits **COM1A1,COM1A0,COM1B1 and COM1B0** which controls the pins 9(COM1A) and 10(COM1B).

In the picture below we see the modes of this bits(picture from atmega datasheet):

From these picture if we have all four bits 0 the pins are disconected-not work;

If COM1A1 and COM1B1 are 1 that means we generate a signal which frequency is given by the formula f_{OCnx}=f_{clk}/2*N*TOP, where f_{clk}=16000000Hz, N is the prescaler(given by the CS12,CS11 and CS10 from the TCCR1B register-explain later) and TOP takes the value from the waveform generation mode bit description image(multiple situations).

If all four bits are 1 we have the situation from previos case with the difference that he signal is inverted(the pin is set high on upcounting and low when downcounting)

If the COM1A0 and COM1B0 are 1 that means OC1B is diconnected(pin 10), and pin 9(OC1A) will generate a pwm signal wich frequency is dependent by OCR1A. In this case the TOP value take the OCR1A value(it can be between 0 and 65355).

Before examples we must say that the modes which TOP is equal with ICR1 will not be studied in this post because are particular cases(can work with external signals).

With examples we start with this final combination of COM1A0 and COM1B0, both 1, because in the waveform generation mode bit description image has only two situation for phase correct pwm:

So OCR1A0,OCR1B0 are 1, OCR1A1,OCR1B1 are 0 and WGM13,WGM11, WGM10 are 1 and WGM12 are 0(mode 11 in image) with no prescaler:

The program is(for example OCR1A=128 and no prescaler):

void setup() {

pinMode(9, OUTPUT);

pinMode(10,OUTPUT);

TCCR1A=0;//reset the register

TCCR1B=0;//reset the register

TCNT1=0;

TCCR1A=0b01010011;//COM1A0,COM1B0 are 1, COM1A1, COM1B1 are 0

//also WGM11, WGM10 are 1

TCCR1B=0b00010001;//WGM13 is 1 and WGM12 is 0 with no prescaler CS10 is 1

OCR1A=128;// compare value

}

void loop() {

// put your main code here, to run repeatedly

}

The result for this program is in the image below:

In this mode because of the dual slope particularity the frequency is half the frequency obtain from the formula **f _{OCnx}=f_{clk}/2*N*TOP**, where the TOP is the OCR1A value;

Let’s say we want a **1kHz** frequency from the formula the value for the TOP(which is OCR1A) is 8000, but with that value on oscilloscope appear only **500Hz**, so for 1kHz signal we use a value for TOP quals with **4000**.

The program is:

void setup() {

pinMode(9, OUTPUT);

pinMode(10,OUTPUT);

TCCR1A=0;//reset the register

TCCR1B=0;//reset the register

TCNT1=0;

TCCR1A=0b01010011;//COM1A0,COM1B0 are 1, COM1A1, COM1B1 are 0

//also WGM11, WGM10 are 1

TCCR1B=0b00010001;//WGM13 is 1 and WGM12 is 0 with no prescaler CS10 is 1

OCR1A=4000;// compare value

}

void loop() {

// put your main code here, to run repeatedly

}

And the result on oscilloscope:

In this mode a special case appear when **OCR1A is equal to 0** because it will generate a signal at **8 MHz** like the frequency value from the formula, and not half.

Next COM1A1, COM1B1 are 1 , COM1A0, COM1B0 are 0 and WGM13 must be 0 we will have a non-inverted signal. So because mode 1 is like for timer 0 we will make an example with mode 3 phase correct and 10 bits(mode 2 is like 3 but TOP value is 511).

For this mode the TOP value take the 0x03FF value which means 1023 and with the formula **f _{OCnx}=f_{clk}/2*N*TOP** and no prescaler the resulting frequency is

**7820Hz**.

The program for this is(we choose **OCR1A=512** for a **50%duty cycle**)

void setup() {

pinMode(9, OUTPUT);

pinMode(10,OUTPUT);

TCCR1A=0;//reset the register

TCCR1B=0;//reset the register

TCNT1=0;

TCCR1A=0b10100011;//COM1A0,COM1B0 are 0, COM1A1, COM1B1 are 1

//also WGM11, WGM10 are 1

TCCR1B=0b00000001;//WGM13 and WGM12 are 0 with no prescaler CS10 is 1

OCR1A=512;// duty cycle value

}

void loop() {

// put your main code here, to run repeatedly

}

And on the oscilloscope we have:

With an **OCR1A =768** we have a **75% duty cycle** like:

Also for this mode the lowest frequency we can obtain is if we use **1024 prescaler** and with formula f_{OCnx}=f_{clk}/2*N*TOP the resulting frequency is **7.63 Hz**

The program:

void setup() {

pinMode(9, OUTPUT);

pinMode(10,OUTPUT);

TCCR1A=0;//reset the register

TCCR1B=0;//reset the register

TCNT1=0;

TCCR1A=0b10100011;//COM1A0,COM1B0 are 0, COM1A1, COM1B1 are 1

//also WGM11, WGM10 are 1

TCCR1B=0b00000101;//WGM13 and WGM12 are 0 with 1024 prescaler CS10 and CS12 is 1

OCR1A=768;// duty cycle value

}

void loop() {

// put your main code here, to run repeatedly

}

The result on oscilloscope:

The **phase and frequency correct PWM mode** is almost like **phase correct PWM** but as you can see in the first two images(**timing diagrams**) the value of OCR is uptated in the **top of the triangle signal** so if you have a TOP value which is changing the output signal will be **unsymmetrical**, for phase correct mode but for the other mode the value of OCR is updated at** the bottom of triangle signal**, so the same value give the lenght of rising slope and falling slope the signal beeing **symmetrical**. So if you have a changing TOP value use the phase and frequency mode.

As we can see, the formula for the frequency contains a **N value** named prescaler which is given from the last 3 bits of the TCCR1B register. The values of the bits associated with the value of the prescaler is in the image(picture from atmega datasheet):