go_away

Author Topic: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU  (Read 11199 times)

0 Members and 1 Guest are viewing this topic.

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« on: January 18, 2008, 10:11:38 AM »
Once again, please help, please

Offline robonoob

  • Robot Overlord
  • ****
  • Posts: 149
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #1 on: January 18, 2008, 10:31:30 AM »
u only have a chip? or a whole controll board?
if u have a controll board then search internet for how to make a circuit board with your atmega as its brain and then you can connect ur servos there.
else if u have a control board then where is the problem? just connect the servos where needed and programme it and you are all done :)
but if u know all the hardware thing(like where to connect it and stuff) and you have done this all (like connecting and powering servo) but you only have problems with servo control code, then you should read some examples of servo controll and read tutorials about controlling the servo with the code you want to use.
anyway internet is a good friend of yours :) use it

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,663
  • Helpful? 169
    • Society of Robots
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #2 on: January 18, 2008, 11:01:56 AM »
Can you tell us what you have working and what isn't working?

The $50 Robot will tell you exactly how to do it.

Are you sure you have 16MHz?

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #3 on: January 18, 2008, 11:39:09 AM »
Well, this is what i have done.

--i have my mcu board which runs my line detecting code
--i modified my servo for continuous rotation using the admin tutorial. it worked. servo stopped at 1.5ms

this is what i used for delay
courtesy sparkfun blink_iMhz code

//General short delays
void delay_ms(uint16_t x)
{
  uint8_t y, z;
  for ( ; x > 0 ; x--){
    for ( y = 0 ; y < 90 ; y++){
      for ( z = 0 ; z < 6 ; z++){
        asm volatile ("nop");
      }
    }
  }
}

--but i added an oscillator t(16mhz and set the fuse bits) to my mcu after . my code ran perfectly but faster, which is what i wanted
--but now when i try to control the servo with 1.5ms using the above code, it doesnt stop instead it moves slower
--so if anyone can help me with a servo control code, i'll be really grateful
« Last Edit: January 18, 2008, 11:41:03 AM by rox2007 »

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #4 on: January 18, 2008, 11:56:25 AM »
actually it reverses when it gets that 1.5ms
maybe it recaliberated itself
i should probably run a code from 0ms to 2ms to find the new stop point

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #5 on: January 18, 2008, 12:46:11 PM »
perhaps when you added on the 16mhz oscillator you messed up the pulse.

A program that controls a servo @ 4mhz will not work correctly control a servo @ 16mhz

But that is based on my experience in assembly and BASIC , though not in C , thats just a guess.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #6 on: January 18, 2008, 12:50:21 PM »
yeh, i guess so
i'll just run a code that runs the servo from 0ms to 2ms, and find that centerpoint

Offline rgcustodio

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 0
  • Use "Search" and ye might find answers!
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #7 on: January 18, 2008, 12:50:38 PM »
check you F_CPU settings make sure you've set it up properly for the new oscillator, then don't forget to recompile your code.
The best thing one can do when it's raining is to let it rain. - H. W. Longfellow

understanding is the path to enlightenment

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #8 on: January 18, 2008, 12:50:53 PM »
at what oscillator speed did the servo work correctly?
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #9 on: January 18, 2008, 12:55:55 PM »
at 4mhz i think

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #10 on: January 18, 2008, 12:56:42 PM »
check you F_CPU settings make sure you've set it up properly for the new oscillator, then don't forget to recompile your code.

where do i find this

Offline rgcustodio

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 0
  • Use "Search" and ye might find answers!
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #11 on: January 18, 2008, 12:58:06 PM »
check you F_CPU settings make sure you've set it up properly for the new oscillator, then don't forget to recompile your code.

where do i find this

it should be a define in one of your source or header files.

you should find something like:
Code: [Select]
// make sure the following is the CPU frequency in Hertz
#define F_CPU 1600000UL
in your code.
« Last Edit: January 18, 2008, 01:04:01 PM by rgcustodio »
The best thing one can do when it's raining is to let it rain. - H. W. Longfellow

understanding is the path to enlightenment

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #12 on: January 18, 2008, 01:03:32 PM »
at 4mhz i think

so try to send it a pulse of 6 ms( 4 times 1.5ms)  in your confused code. Once upon a time , I ran servo code at 8 mhz instead of the 4mhz which worked before. So all I did was double the 1.5  ms pulse , so the microcontroller though it was sending a 3ms pulse , but since it was at 8mhz it ended up giving 1.5 ms.


P.S. If you try to do the 6 ms pulse , have an on/off switch for the power supply , because if you leave it on and it gives a real 6ms pulse i think the servo will turn to the extreme angle and the gears will grind. or maybe the servo won't accept the pulse at all.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #13 on: January 18, 2008, 01:39:30 PM »
this just might work thanx.

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #14 on: January 18, 2008, 03:00:34 PM »
well did it work?
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #15 on: January 18, 2008, 03:24:32 PM »
nope, dont know what happened.....is it possible to loop through numbers and find the right milliseconds for stopping it
actually i think i was running it at 1mhz
« Last Edit: January 18, 2008, 04:13:19 PM by rox2007 »

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #16 on: January 18, 2008, 03:36:13 PM »
how do you modify the delay_cycles code from atmega8 to atmega16

about the fcpu thing, i dont have it declared anywhere. shouldi declare it?
« Last Edit: January 18, 2008, 04:00:40 PM by rox2007 »

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #17 on: January 18, 2008, 04:30:53 PM »
it looks like for any value 10ms and above the servo doesnt move, ignore this...it doesnt work
« Last Edit: January 18, 2008, 04:41:53 PM by rox2007 »

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,663
  • Helpful? 169
    • Society of Robots
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #18 on: January 18, 2008, 04:40:26 PM »
Wait . . . atmega16 or atmega168?

Anyway, you don't have to modify the delay code, its exactly the same.

What changes is the frequency you are running at . . . does your hardware have a crystal built in? Are you using the Arduino or the $50 Robot? What are your fuse frequency settings? Are you using AVRlib?

Here is a great way to determine your frequency for servo use (if you don't have an oscilloscope):
Code: [Select]
turn on LED
loop 10 times:
  delay_cycles(65500);
turn off LED
loop 10 times:
  delay_cycles(65500);
repeat

Now this means that 65500*10*2 cycles occur for every flash of the LED.

Now get out your watch and count the number of times your LED turns on in one minute.

Lets say its 30 times, count.

That means:
65500*10*2*count/(60 seconds) = cycles per second

or

655000 cycles/second

but what you want is to know how many cycles it takes for 1.5ms - the servo control time . . .

so:

655 cycles/ms   ->   655*1.5=982.5

so to get your servo to stop moving, you'd want to send a signal of 1.5ms long, or 982.5:

turn servo on
delay_cycles(982);
turn servo off

Your servo moving one direction would be 655*1 (for 1ms), and the other way is 655*2 (for 2ms).
« Last Edit: January 18, 2008, 05:46:40 PM by Admin »

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #19 on: January 18, 2008, 04:43:31 PM »
i'll try this..

Thanx alot admin, you are still the best

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,663
  • Helpful? 169
    • Society of Robots
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #20 on: January 18, 2008, 04:46:57 PM »
thanks :)

I think Ill turn that last post into a tutorial . . . it saved me tons of timing frustration when I figured that out last summer . .  .

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #21 on: January 18, 2008, 04:53:31 PM »
that wont be a bad idea

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #22 on: January 18, 2008, 04:55:49 PM »
-----i am using a 16mhz external crystal with an atmega168

-------built like the $50 Robot

fuse frequency settings= i got this from sparkfun

----avrdude -p m168 -P com1 -c ponyser -U lfuse:w:0xE6:m
i am using AVRlib?

admin, one question:
loop 10 times:
turn on
loop 10 times:
turn off

do you mean i should run it in a for loop like

for (j = 0; j < 10; j++)
      {
          PORTC = 0xFF;
         PORTB = 0xFF;
         PORTD = 0xFF;
         
           delay_cycles(65500);
         PORTC = 0x00;
         PORTB = 0x00;
         PORTD = 0x00;
         loop 10 times:
           delay_cycles(65500);
      }


OR

   for (j = 0; j < 10; j++)
      {
          PORTC = 0xFF;
         PORTB = 0xFF;
         PORTD = 0xFF;
      }   
      for (j = 0; j < 10; j++)
      {
         delay_cycles(65500);
         PORTC = 0x00;
         PORTB = 0x00;
         PORTD = 0x00;
         
         delay_cycles(65500);
      }
« Last Edit: January 18, 2008, 05:18:41 PM by rox2007 »

Offline rgcustodio

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 0
  • Use "Search" and ye might find answers!
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #23 on: January 18, 2008, 05:14:50 PM »
regarding the F_CPU thing nope i guess it isn't needed in your case, i think you're not using avrlibc. you can ignore the F_CPU thingy.
The best thing one can do when it's raining is to let it rain. - H. W. Longfellow

understanding is the path to enlightenment

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,663
  • Helpful? 169
    • Society of Robots
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #24 on: January 18, 2008, 05:52:35 PM »
you want to do that first for loop you posted, but probably just for your LED pin . . . I'm also doing 16MHz on another AVR, so if you did your fuse settings properly, you will find your delay should be around ~800.

since its just a quickie test, I just do:

Code: [Select]
while(1)
{
LED_on();
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
LED_off();
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
delay_cycles(65500);
}

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #25 on: January 18, 2008, 06:03:23 PM »
i got 521, with the code i wrote. i'll try what you wrote. and was this correct for a 16mhz external oscillator

avrdude -p m168 -P com1 -c ponyser -U lfuse:w:0xE6:m
« Last Edit: January 18, 2008, 06:05:37 PM by rox2007 »

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #26 on: January 18, 2008, 06:10:43 PM »
maybe i didnt do the fuse settings right, how can i do it right

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #27 on: January 18, 2008, 06:22:30 PM »
Read Osc.Calibration Byte successful: 0xA8 (168)

i got 22 counts
i checked my fuse bits and they are correct

    *  CKDIV8 = 1
    * CKOUT = 1
    * SUT1 = 1
    * SUT0 = 0
    * CKSEL3 = 0
    * CKSEL2 = 1
    * CKSEL1 = 1
    * CKSEL0 = 0
« Last Edit: January 18, 2008, 06:51:55 PM by rox2007 »

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,663
  • Helpful? 169
    • Society of Robots
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #28 on: January 19, 2008, 08:31:47 AM »
Quote
maybe i didnt do the fuse settings right, how can i do it right
I use AVR Studio to do my fuse settings.

Quote
i checked my fuse bits and they are correct

    *  CKDIV8 = 1
    * CKOUT = 1
    * SUT1 = 1
    * SUT0 = 0
    * CKSEL3 = 0
    * CKSEL2 = 1
    * CKSEL1 = 1
    * CKSEL0 = 0
how do you know thats correct? are you sure you want to divide the clock by 8 (making it 1/8th the speed)? ;)

Offline rox2007Topic starter

  • Full Member
  • ***
  • Posts: 95
  • Helpful? 0
Re: Please, how do i control my servo with an ATMEGA 168, 16Mhz MCU
« Reply #29 on: January 19, 2008, 09:21:28 AM »
actually, i got that from sparkfun. I am using the serial dongle and thats what i saw on sparkfun's tutorial. well i just skimmed through the tutorial, maybe i missed something. I dont know how to do it with AVR though. If you could help me out that would be great.

 


Get Your Ad Here