Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

help needed in generating pwm signal using atmega8

Not open for further replies.


Newbie level 6
Jun 17, 2009
Reaction score
Trophy points
Activity points
atmega8 pwm

Hi guys,

I am using atmega8 µc. I want to generate a pwm of 1MHz frequency.
So can any one help me in programing in assembler.


pwm atmega

Hello nandhu015,

thankyou for the reply. But the thing is i need 1MHz as output frequency. I am not able to generate it. If you can help me it will be greatfull.

pwm signal programming using atmega8

Hi pepakpan.
You can use the Timer0, if you just need a simple pwm signal.

Supose you want a pwm in steps of 255 values (8 bits), with a duty cycle betwen 0% and 100%. You first set a counter variable in 0 (for example count), and in other variable store the desired pwm value (for example pwm_value).

Initialize the Timer0 Counter with an apropiate value, and turn it on. (You must make some calculations to get the correct value, it involves the crystal frecuency, the clock source of timer0, and the desired frecuency of the pwm signal)
Turn on a pin port (for example C0) and turn on the Timer0 (set a clock source)

Each time the Timer0 generates an overflow interrupt you must do some things:
if (count>pwm_value) then (turn off C0)
increase the variable count (count=count+1)
if count =255, then (count=0 and turn on C0).

This is the basic idea, and I'm using it to make nice colors with an RGB led.

(I hope this image helps with the idea. Maybe there are some errors with the values, but it is just to show you the main idea, and I'm at work right now :wink: .)

pwm programming using atmega8

hai pepakpan

I think with atmega 8 it is impossible, because the max frequency is 16MHz. You can generate 1MHz base frequency for pwm, but variying pwm is not possible.



hey guys,
thankyou for the response. By the way I got the requires frquency PWM this is how I made it:

#include <avr/io.h>
int main()
//Set up the PWM signal (page 118-119, ATmega8(L))
//The PWM signal is triggered when Timer/Counter register = Output Compare register. (TCNT2 = OCR2)
//attemtp to make the signal 1MHz
// Clk/(2*Dividor*(1+OCR2))
// 3.6MHz/(2*1*(1+1)) = 1MHz
OCR2 = 0x01; //set OCR2 to overflow at 1
// FOC2 = 0;
// WGM20 = 0; //CTC mode. Reset the timer each overflow of OC2 and toggle the PWM value
// COM21 = 0; //
// COM20 = 1; //Toggle on overflow
// WGM21 = 1; //
// CS22 = 0; //
// CS21 = 0; //
// CS20 = 1; //clk/(no prescaling)
TCCR2= 0x19; //set TCCR2 to the above values (0x00011001b)
DDRB = 0x08; //as of right now, only enable the PWM pin as output
return 0;

Thankyou once again.


Not open for further replies.

Part and Inventory Search

Welcome to