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.
Regards.
(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: .)