Sunday, 15 April 2012

PWM tutorial

What is PWM ?

PWM or Pulse Width Modulation is an important concept in electronics. This is a way of generating an analog signal out of digital data. PWM converts digital signals to aproximate analog signal. With the little help of filtering real analog signal can achieved. Speed control of dc motor, brighness control of  LED or interfacing speaker with digital data can be done using PWM.

The analog signal is converted to Pulse Width Modulated Digital signal

   Generating PWM signal with ATmega series:

In this tutorial ATmega16 is used to generate PWM. ATmega16 has four PWM channels. They are as follows:
ATmega16

  1. Channel 0 : This is a 8 bit PWM channel
  2. Channel 1 : It consists of two channels channel1A and channel1B,both are 16 bit channel.
  3. Channel 2: This is 8 bit channel.
Lets start with simple one.

Channel 0:

There are 3 basic registers associated with channel 0:
  1. TCCR0 (Timer Counter Control Register): This is 8 bit register. By setting different bits of this register mode of operation can be selected.
  2. TCNT0 (): This is a 8bit counter register.
  3. OCR0 (Output Compare Register): The counter register TCNT0 is compared with this register. This is a 8bit register. Maximum value that can be stored in this register is 0xFF or 256;
The output pin for channel 0 is OC0.

How PWM is achieved through channel 0?

    Suppose value on OCR0 is 64, TCNT0 counter counts from 0,Initially OC0 pin is high. When TCNT0 counts 64 OC0 pin gets low but TCNT0 counts upto 255, after 255 count by TCNT0, TCNT0 is set to 0.