Author Topic: PWM on atmega168 timer0 phase correct  (Read 3332 times)

0 Members and 1 Guest are viewing this topic.

Offline javilaTopic starter

  • Jr. Member
  • **
  • Posts: 31
  • Helpful? 0
PWM on atmega168 timer0 phase correct
« 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??

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
    • dunk's robot
Re: PWM on atmega168 timer0 phase correct
« Reply #1 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
(great page for everything AVR timer related.)

dunk.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: PWM on atmega168 timer0 phase correct
« Reply #2 on: June 19, 2010, 07:14:04 AM »
http://www.societyofrobots.com/member_tutorials/node/228

Especially the reference sheets in section 4
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk