I am testing with servo motor C40S brought from cytron, I followed the instruction from datasheet which setting pulse width 1.0ms for anticlockwise, 1.5ms for neutral position, 2.0ms for clockwise with 20ms pulse period.
But what weird is, when i set the pulse width to 1.0ms, servo turns clockwise, 2.0ms turns anticlockwise. And the 1.5ms pulse width is totally not matched for servo to turn to the neutral position.
When the servo turn to the right, if i pressed switch2 (which set for pulse width 1.5ms), the servo totally turned to the left but not the middle. As the same way when the servo turn to the left, if i pressed switch2, the servo turned to the right. My coding as follow by using CCS compiler, PIC18F4550, 20Mhz crystal.
- CODE: SELECT_ALL_CODE
#include <18F4550.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20M, crystal)
#define SERVO_CONTROL PIN_B3
#define SW1 PIN_B0
#define SW2 PIN_B1
#define SW3 PIN_B2
void main()
{ unsigned int a;
set_tris_b(0b00000111); //set port b of pin7-3 as output and input, pin2-0 as input
output_b(0);
while(true)
{
if(!input(SW1)==1)
{
for(a=0; a<100; a++)
{
output_high(SERVO_CONTROL); //high with pulse of +5V
delay_us(2000); //delay 2ms for move the servo to right
output_low(SERVO_CONTROL); //low pulse of 0V
delay_us(18000); //delay low pulse with 18ms, plus 2ms
// total 20ms for one pulse period
}
}
if(!input(SW2)==1)
{
for(a=0; a<10; a++)
{
output_high(SERVO_CONTROL); //high with pulse of +5V
delay_us(1500); //delay 1.5ms for move the servo to cetre
output_low(SERVO_CONTROL); //low pulse of 0V
delay_us(18500); //delay low pulse with 18.5ms, plus
// 1.5ms total 20ms for one pulse period
}
}
if(!input(SW3)==1)
{
for(a=0; a<100; a++)
{
output_high(SERVO_CONTROL); //high with pulse of +5V
delay_us(1000); //delay 1ms for move the servo to left
output_low(SERVO_CONTROL); //low pulse of 0V
delay_us(19000); //delay low pulse with 19ms, plus
// 1.0ms total 20ms for one pulse period
}
}
}
}
Hope can get kindly help soon. Thanks..^.^