Society of Robots - Robot Forum

Software => Software => Topic started by: Strikeskids on August 23, 2009, 01:58:50 PM

Title: C code for a servo pulse
Post by: Strikeskids on August 23, 2009, 01:58:50 PM
What is the C code for a servo pulse.
I tried using the AVR library but the code there didn't work very well.
The 1500 us pulse works well but i don't know the code for it.
Help Please!!!! ??? ??? ??? ???
Title: Re: C code for a servo pulse
Post by: SmAsH on August 24, 2009, 12:45:33 AM
what code were you using?
this is using pwm right?
Title: Re: C code for a servo pulse
Post by: Strikeskids on August 24, 2009, 04:05:39 AM
I used admin's hex to modify my servo and that code seemed to work better than the servo header in the avrlib.
I am using pwm
Title: Re: C code for a servo pulse
Post by: SmAsH on August 24, 2009, 04:21:40 AM
can you provide the section of code you are using?
generally something like this will work for pwm:
Code: [Select]
while(1)
{
portb = 0xff; // turn all b ports high
delay_us(1500);
portb = 0x00; // turn all b ports low
delay_us(20000);
}
Title: Re: C code for a servo pulse
Post by: Strikeskids on August 24, 2009, 04:53:19 AM
How do you specify a specific port? I have 3 servos, 2 modified and one not for my robot and i don't want them all moving at the same time.
Title: Re: C code for a servo pulse
Post by: SmAsH on August 24, 2009, 05:00:17 AM
Code: [Select]
while(1)
{
portb.5 = 0xff; // turn pb5 high
delay_us(1500);
portb.5 = 0x00; // turn pb5 low
delay_us(20000);
}
Title: Re: C code for a servo pulse
Post by: Strikeskids on August 24, 2009, 05:08:12 AM
Perfect so,
Code: [Select]
u08 i;
for (i=50; i-->0;)
     portd.0 = 0xFF
     portd.1 = 0xFF
     delay_us(1700)
     portd.0 = 0x00
     portd.1 = 0x00
     delay_us(20000)
}
should turn 2 unmodified servos in ports pd1 and pd2 forward for a second.
Title: Re: C code for a servo pulse
Post by: SmAsH on August 24, 2009, 05:14:27 AM
i don't know :P
try it!
normally when i move servos i use:
Code: [Select]
void servo_left(signed long int speed)
{
PORT_ON(PORTD, 0);
delay_cycles(speed);
PORT_OFF(PORTD, 0);
delay_cycles(200);
}
void servo_right(signed long int speed)
{
PORT_ON(PORTD, 1);
delay_cycles(speed);
PORT_OFF(PORTD, 1);
delay_cycles(200);
}
but im not much of a programmer :P
Title: Re: C code for a servo pulse
Post by: Webbot on August 24, 2009, 05:16:26 AM
The examples the others have given use software to generate a pulse. Your original post mentioned PWM. If you want to use hardware PWM then checkout my members tutorial at http://www.societyofrobots.com/member_tutorials/node/228 (http://www.societyofrobots.com/member_tutorials/node/228) which discusses PWM and gives an example for driving a servo.
Title: Re: C code for a servo pulse
Post by: Strikeskids on August 24, 2009, 06:51:03 AM
Neither the PORT_ON code nor the PORTD = 0xFF code work
Title: Re: C code for a servo pulse
Post by: Strikeskids on August 24, 2009, 09:48:16 AM
Webbot, your code works perfectly, but I had to modify my circuit board first. Also there is a much less complicated way of explaining it shown here: http://www.societyofrobots.com/robotforum/index.php?topic=1827.0 (http://www.societyofrobots.com/robotforum/index.php?topic=1827.0). Thanks everybody.
:) ;) :D ;D 8) ::) :P :-*