Society of Robots - Robot Forum

Software => Software => Topic started by: greywanderer012345 on January 22, 2011, 06:32:52 PM

Title: Basics of PWM $50 robot
Post by: greywanderer012345 on January 22, 2011, 06:32:52 PM
I'm trying to learn the basics of using pwm. The Tutorial seems to suggest to use pwm on PORTB. Can pwm be used on PORTD, because I've built the $50 robot circuit. I'm wanting to learn about pwm by using it to control my servos. Can anyone help me out? I'm using the atmega328p. Thanks.
Title: Re: Basics of PWM $50 robot
Post by: Webbot on January 23, 2011, 02:50:00 PM
Hardware PWM for servos is best done by a 16 bit timer. The ATMega8, ATMega168 and ATMega328P only have one 16 bit timer: Timer 1. Since the PWM is done in hardware then the output is done to fixed pins on Port B.

Writing your own PWM software (or using WebbotLib, say) allows you to output PWM to (almost) any pin - ie increased flexibility at the expense of extra code (and CPU usage).

Why does it need a 16 bit timer?
Well servos require a pulse every 20ms and the 'position' pulse is normally 1ms to 2ms long (ie a range of 1ms out of 20ms so about 1/20th of the total). An 8 bit timer would only give (2^8 = 256) 256 time intervals for 20ms or 12 (256/20) different position settings for the servo. Most servos can cope with much better positional accuracy than just steps across their entire range.


Title: Re: Basics of PWM $50 robot
Post by: greywanderer012345 on January 23, 2011, 04:53:03 PM
Thanks. That clears up a few things. Can anyone tell me or link me to how to use/access the timer? I'm using the atmega328p and the latest versions of avr studio and winavr. Right now, I have fiugred out how to program my servos to a certain position(not modified yet) using a method similar to the servo_left() etc functions in admin's SoR_Utils.h, but I'm using a for() loop as a delay and have figured out that a pulse of 30-135 cycles of that loop followed by a delay of so many loops. I didn't know that the pulse should be sent every 20ms before. I would rather use the timer and a delay out of good practice. I don't know how to access the timer or how to make the mcu "sleep" or wait. Can anyone clear any of this up for me? Thanks, again.
Title: Re: Basics of PWM $50 robot
Post by: don bunot on February 16, 2011, 05:08:51 AM
hi have you guys tried using a PWM program on DC MOTOR like TAMIYA TWIN MOTORS, using microcontrollers?
Title: Re: Basics of PWM $50 robot
Post by: Webbot on February 16, 2011, 12:55:55 PM
Absolutely - but unlike servos you MUST use a hardware motor controller to drive the motors as a microcontroller doesn't have the power to drive a motor directly
Title: Re: Basics of PWM $50 robot
Post by: joe61 on February 16, 2011, 06:51:27 PM
Thanks. That clears up a few things. Can anyone tell me or link me to how to use/access the timer? I'm using the atmega328p and the latest versions of avr studio and winavr.

There are a number of good tutorials on AVR Freaks
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=50106 (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=50106)
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=search&mode=results (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=search&mode=results)

Not to mention a very good tutorial here:
http://www.societyofrobots.com/member_tutorials/node/228 (http://www.societyofrobots.com/member_tutorials/node/228)

Also, the Atmel site has some application notes too. They've changed their site around though, so I don't know where things are again yet. Search for timer or pwm at http://www.atmel.com (http://www.atmel.com).

Joe