Welcome to our site! EDAboard.com 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.
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: .)
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
while(1);
return 0;
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.