# Atmega8 PWM control(frequency,polarity,duty cycle) tutorial with example program

If You come there from internet search i assume You already know what is PWM and where it can be used, so in this post I’ll only explain how to use 8 bit timer in Atmega8 to generate PWM signal and control frequency, polarity and duty cycle.

PWM can be generated from 16-bit Timer/Counter1 or 8-bit Timer/Counter2 but this time I’ll only explain 8-bit Timer/Counter2 Fast PWM Mode.

In this picture from Atmega8 documentation Fast PWM mode is explained.  Counter(8bit) counts from 0×00 to 0xFF and restarts from 0×00. In not inverting mode OC2 is cleared when counter match value in OCR2 register and set at 0×00. In inverting mode OC2 is set when counter match value in OCR2 register and cleared at 0×00.

From this all turns out that PWM duty cycle depends on OCR2 register. In not inverting mode duty cycle = OCR2/256*100% and it’s  50% if OCR2 is 0×80(middle between 0×00 – 0xFF).

In every PWM period counter must count 256 steps, so frequency of signal is 256 times lower than counter clock from prescaler. PWM frequency = Atmega clock frequency/timer prescaler(1,8,32,64,128,256)/256. With 4 MHz crystal maximum(without prescaler) PWM frequency is 15 625Hz.

Register setup

For example 15 625 Hz, 50 % duty cycle, non-inverting mode PWM signal generation.

1. OCR2=0×80(128); As mentioned before duty cycle = OCR2/256*100%

2. TCCR2=0×69;

3. DDRB=0×08; PWM signal is outputted by toggling  level of PORTB pin 3.

DDRB sets it as output.

All together:

```#include <iom8.h> int main( void ) { DDRB=0x08;```

` `

```OCR2=0x80; TCCR2=0x69; while (1) {}; }```

Result:

~10% duty cycle, 61 Hz.

```#include <iom8.h> int main( void ) { DDRB=0x08; OCR2=0x1A; // 256/10=25,6 26 in hex = 1A; TCCR2=0x6E; // 256 prescaler ```

` `

```while (1) {}; }```

Result:

And there is demo program and *.hex file for PWM demo seen in video.

Atmega8 PWM demo program (1.8 KiB, 3,935 hits)

• Lmcgill2

What type of scope is that? Just curious…

• http://www.electronicsblog.net/ Darius
• Gauarvgupta

how to generate the pwm of 40 hz signal…….?

• Gurubaran M

Thanks for your attitude to teach others

• riki yanto

How about wanting to 50Hz inverter using OC2 pinb.3

• David

thanks for this explanations about the mikrocontroller

I just used the sourcecodeexample of

http://www.electronicsplanet.ch/mikrocontroller/avrcodesammlung/atmega16timer0fastpwm.htm

and it worked. but thanks to your explanations i underrstand, why it works

What is the model of your oscilloscope? And how to enter the writing in the movie? Thanks

• sathish

In atmega8 ,how to detect the different dutycyles of pulses?.. and also want to glow a each LED for each duty cycles?…

Here i wrote a program in atmega8 for generating DC pulses with different dutycycles. Now i want to write a program for detecting it ie If 10% dutycyle is received then LED 1 will be glown, if 15% dutycycle is received then LED 2 will be glown, etc…

Could you people help me??..

• sathishkumar

Hai friends..

I hope you people only help to me in this simplest project.!!!

I wrote a program for generating 10%, 20%, 30%… dutycycles of pulses in atmelstudio6 for atmega8 MC. Now i wanna to detect that pulses and need to glow the LED respective to that dutycycles.

For example, i took one input pin and 6 output pin. when 10% of duty cycle is received at input pin, Led1 will be glow which is connected in (PD0) first output pin. If i give a 20% dutycycle pulses as a input then LED 2 will be glow (PD1), etc… Like wise the 6 Led want to be glow with respect to different dutycycles of pulses..