Society of Robots - Robot Forum

Software => Software => Topic started by: javila on June 18, 2010, 11:37:19 AM

Title: PWM on atmega168 timer0 phase correct
Post by: javila on June 18, 2010, 11:37:19 AM
Hi: I am new at programing on the atmega168, I am trying to control a motor with phase correct PWM on timer 0
can somebody take a look at my code to see if I am doing it correctly please.:
Code: [Select]
TCCR0A=0; //disable all PWM on Timer 0
OCRA=200; //frequency is 20kHz with no prescaler
DDRD=0xFF; //set D port as output
PORTD=0x00; //pull-up resistors turned off
//configure timer 0 for phase correct mode via OCRA, with no prescaler
TCCR0A= (1 << WGM00);
TCCR0B = (1 << WGM02)

//enable PWM on port D5 and D6 in non-inverted compare mode
TCCR0A |= 2 << 6; //port D6
TCCR0B |= 2 << 4; //port D5

//change OC0A and OC0B from 0-200 to drive motors
I am trying to follow what the datasheet says but I am not sure if I got it correct. I read that 20Khz frequency was good for controlling motors Any advise??
Title: Re: PWM on atmega168 timer0 phase correct
Post by: dunk on June 18, 2010, 04:46:37 PM
this is the page i used the first time i did it:
http://members.shaw.ca/climber/avrtimers.html (http://members.shaw.ca/climber/avrtimers.html)
(great page for everything AVR timer related.)

dunk.
Title: Re: PWM on atmega168 timer0 phase correct
Post by: Webbot on June 19, 2010, 07:14:04 AM
http://www.societyofrobots.com/member_tutorials/node/228 (http://www.societyofrobots.com/member_tutorials/node/228)

Especially the reference sheets in section 4