Society of Robots - Robot Forum

Software => Software => Topic started by: superchiku on February 24, 2008, 02:42:20 PM

Title: pwm help
Post by: superchiku on February 24, 2008, 02:42:20 PM
iam using the atmega16 mcu and i need to control motors but i dont know how to do pwm using atmega16 (registers,functions etc to be used) so i decided to go in a simple way as the admin wrote in some tutorial in which i need to check  the delay of my mcu so i made a sample program and loaded in the mcu but instead of blinking it continuously lit on the source code of my program is as follows
# include<avr/io.h>
# include<util/delay.h>
 int main(void)
{
 unsigned long int i,j;
DDRD=0x40;
while(1)
 {
 
 PORTD=0x40;
   for(i=0;i<1000;i++)
   {
    for(j=0;j<65000;j++){}

 }
PORTD=0x00;
  for(i=0;i<1000;i++)
   {
    for(j=0;j<65000;j++){}

 }
}

return(1);
}
plzz tell me how to rectify this code inorder to know the no of cycles it takes for 1 ms pause so that i can use pwm or u can tell me how i can use pwm the traditional way like wat registers to initialize and wat functions to use(plzz write a small cource code as an example if u decide to say abt pwm)

plzz help me

Title: Re: pwm help
Post by: hazzer123 on February 24, 2008, 02:57:21 PM
How about using this software - http://www.avrprojects.net/articles.php?lng=en&pg=67 (http://www.avrprojects.net/articles.php?lng=en&pg=67)

It creates assembly code which delays for the specified amount of time.

To place assembly inside C code, you have to use inline assembly tags - http://www.nongnu.org/avr-libc/user-manual/inline_asm.html (http://www.nongnu.org/avr-libc/user-manual/inline_asm.html)

Hazzer123
Title: Re: pwm help
Post by: Admin on February 27, 2008, 08:24:11 PM
Why not just get a motor driver instead? Controlling those are much easier.

If you do a search for PWM in the ATmega16 manual, you will learn more about how to use it.

If you want code, check out AVRlib:
http://www.mil.ufl.edu/~chrisarnold/components/microcontrollerBoard/AVR/avrlib/

and look at the timer.c pwm commands:
http://www.mil.ufl.edu/~chrisarnold/components/microcontrollerBoard/AVR/avrlib/docs/html/group__timerpwm.html