# [ Curiosity,Experimentation ]

• ## Blog Stats

• 193,982 Visits

• ## Subscribe

 appusajeev on Writing a Linux character Devi… Căn hộ Sunshine Rive… on Writing a Linux character Devi… varnik on Writing a Linux character Devi… gsvijay on Writing a Linux character Devi… Vijay Thakkar on Writing a Linux character Devi…

## PWM in AVR

Posted by appusajeev on September 30, 2010

In the last post, we discussed about timers in AVR microcontrollers and how to go about programming them. Presented here is a how-to on generating PWM waves in AVR. PWM waves of required duty cycle can be generating by configuring the timers with suitable values. PWM can be used to control servo motors, perform DAC (Digital to Analogue Conversion) etc.
PWM waves will be available on the OC1A pin (pin 15 on ATMEGA8,  19 on ATMEGA16) once it is setup.

In PWM, we generate square waves whose duty cycle can be varied. Duty cycle refers to the fraction of the time period of the wave for which the signal is in high state (or simply ON state).
For example, for a square wave of period 100ms, if the duty cycle=50%,
the signal will be in high state for precisely 50 ms and in low state for the next 50ms that make up the period.
If the duty cycle =20 %, the signal  will be high for 20ms and low for the remaining 80ms.

An interesting aspect of PWM is that we can actually perform DAC (Digital to Analogue Conversion), the output voltage is proportional to the duty cycle we set. i.e. for example, if the duty cycle is 50%, the average voltage available on the OCA1 pin will be 2.5V  and for duty cycle = 30 %, the output voltage= .3x 5 V = 1.5 V and so on.

### PWM modes

There are several modes of PWM generation provided by AVR  which decides the shape of the square wave generated (the placement of the high section in each cycle). The modes are Fast PWM, Phase correct PWM and Phase and Frequency Correct PWM. Each of these is explained with figures below.

#### Fast PWM

In Fast PWM, the timer starts form zero, sets OC1A pin high and starts counting up. When the count equals the compare value (set in the OCR1A register), the OC1A pin is pulled low and the timer continues counting till TOP, resets to zero and repeats the whole cycle again. This is the normal non-inverted mode of operation. In inverted mode, OC1A is pulled low when counter resets and pulled high when compare match occurs.

Fast PWM mode

#### Phase Correct PWM

In this mode, when the timer count reaches TOP, it doesn’t reset to 0. Instead, it starts counting down towards zero. While counting up, when the count equals the compare value, the OC1A pin is pulled high and while downcounting, when the count again becomes equal to compare value, the OC1A pin is pulled low. The process repeats again when the count becomes zero.

Phase Correct PWM mode

#### Phase and frequency correct PWM

This is essentially the same as Phase Correct PWM but has the added feature that we can change the frequency of the square waves at any instant as opposed to the above modes where frequency is constant defined by the relation given in the section below.

### Starting PWM generation

First, we need to configure the clock source for the timer. As discussed in the earlier post, a suitable prescaler may be chosen by setting the CS10,CS11,CS12 bits in the TCCR1B register.
The value of prescaler is a factor in the frequency of waves generated. The frequency of generated square wave (independent of the duty cycle) is given by

PWM formula

N is the value of prescaler and TOP is value to which the counter counts and then resets to zero.
In this discussion, we use the timer in mode 5 (refer datasheet), i.e. 8 bit Fast PWM with TOP = 255. This is a fairly simple mode and can be used to control the speed of motors on the go.
This mode can be selected by setting WGM10 bit in TCCR1A register and WGM12 bit in TCCR1B register. 9 bit and 10 bit PWM with TOP=511 and TOP=1023 can be selected by setting suitable WGM (Waveform Generation Mode) bits. Refer datasheet for the values.
Next we need to choose between normal or non-inverted PWM and inverted PWM (described above) . Inverted PWM is nothing but here duty cycle determines the off time of the waveform. Here we choose normal PWM by setting COM1A1 bit in TCCR1A register.

The duty cycle is given by setting a value between 0 and 255 in the OCR1A register (similarly, 0 to 1023 for 10 bit PWM and 0 to 511 for 9 bit PWM). Value of 255 corresponds to a duty cycle of 100 % and value of 0 in OCR1A corresponds to 0 % duty cycle.
ie OCR1A= 255 x duty cycle.
For example, for duty cycles 20 %, 37 %, 70 %, set OCR1A = 51, 94,  178 respectively
In our example given at the bottom, the frequency of waveform generated  = 46.875 KHz

### An example

A simple straightforward way to demonstrate PWM is to hook up an LED to the OC1A pin. In PWM generation, since the average voltage is proportional to the duty cycle (DAC in effect), the brightness of the LED is proportional to the duty cycle with the LED being maximum bright at 100% duty cycle (OCR1A=255).
The following program increases the brightness of the LED connected to OC1A pin gradually from zero to maximum and then from maximum to zero and so on. Observe the power LED of your laptop while sleeping (laptop, not you :D). We are reproducing the same.

PWM demo code

1. ### Vìðýä luvin my lyf..:) happy..said

dude i think more explanation about application needed,
gud jod!!
🙂

2. ### Parth Gajjarsaid

hey can you provide me with code for motor speed control using depending upon temperature of object….
if temperature is heigh motor speed is high

• ### appusajeevsaid

Hey, its a trivial task. I think you can do that yourself. Just read the temperature from the temperature sensor(LM 35, for instance) using ADC and depending on the ADC value, adjust PWM !!

3. ### deepaksaid

thank you very very much..it really hepled me in understanding the timer concept,which i was studying for the past two weeks..indians rocks..

4. ### petersaid

Very nice tutorial.thank u