N00b head-scratcher -> Servo pulses

Hi,

I’m a very happy owner of a BH3-R starting a new learning course.
Today I started to program my micro controller, figgering out that servo-pulse thing. Aldue I’m doing quite well, I’m kinda stuck.
I buyed the default HItec HS-475HB servo’s but I can’t get them to rotate 180 degrees.

Some details,
I’m a experienced programmer willing to learn (better) C/C++ and ASM.
I have never before programmed a micro controller so I toulght let’ s start easy with a atmel AVR Mega128 ( :laughing: ).
I wrote some simple test loop in c:

#define ServoPort_A PORTA
#define ServoPort_B PORTB
#define ServoPort_C PORTC
#define DdrPreset 0xFF

#include <avr/io.h>
// I have a 16MHz XTAL on my dev board.
// So lets make sure F_CPU is set before including delay.h
#ifdef F_CPU
#undef F_CPU
#endif
#define F_CPU 16000000UL  // 16 MHz
#include <util/delay.h>

int main()
{
	// Set all used DDR bits
	DDRA=DdrPreset;
	DDRB=DdrPreset;
	DDRC=DdrPreset;
        int16_t Delay;
        Delay=0;
        while(1){
		    ServoPort_A=0xFF;
		     ServoPort_B=0xFF;
		     ServoPort_C=0xFF;		
		     _delay_ms(1.5);
		     ServoPort_A=0x00;
		    ServoPort_B=0x00;
		    ServoPort_C=0x00;
		    _delay_ms(15);
	      while(Delay<200){
                    Delay++;
		     ServoPort_A=0xFF;
		     ServoPort_B=0xFF;
		     ServoPort_C=0xFF;		
		     _delay_ms(2);
		     ServoPort_A=0x00;
		    ServoPort_B=0x00;
		    ServoPort_C=0x00;
		    _delay_ms(15);
              }
              while(Delay>1){
              Delay--;
		     ServoPort_A=0xFF;
		     ServoPort_B=0xFF;
		     ServoPort_C=0xFF;		
		     _delay_ms(1);
		     ServoPort_A=0x00;
		    ServoPort_B=0x00;
		    ServoPort_C=0x00;
		    _delay_ms(15);
              }
        }
  return 0;
}

Fuses set:

  OCDEN 
  JTAGEN
  CKOPT
  SUT0

In my rather n00bish opinion I would say this should do the trick,…
The code should move the servo from one side to another,… and it does.
The problem is that this should be 180 degree rotating servo’s, but it is only rotating 90 degrees.

What am I doing wrong and what should I do to get this thing rotating 180 degrees?

Anny help is appreciated.

Sorry, I’m not a C programmer, so can you tell me the range of pulses that are being sent. You need a range of about 500us to 2500uS to be able to get 180° from the servos. :wink:

Jim he is running from 1 to 2 ms pulse width, the frame is fixed and a little short at 15ms+delay but probably close enough for a test. He is using analog servos though so he probably isn’t operating mid range, i.e. he’s getting 90 degrees but not +/- 45 degrees relative to center.

Robopunked, as Jim said you need 0.5ms to 2.5mS to get the entire 180 degrees. If you don’t have your frame time at 20mS though you “might” bump into an endstop on one end or the other. Change your _delay_ms(f) statements to these pairs:

_delay_ms(0.5) and _delay_ms(19.5)
and
_delay_ms(2.5) and _delay_ms(17.5)

which will give the minimum 0.5ms pulse and maximum 2.5ms pulse both with a 20ms frame time.

I’m sending pulses of 1 to 2 milliseconds with an interval of 15 milliseconds.
So that would be the problem then.

I did just tried the proposed pulse-length, and yes that much better.
Tnx for your help, that solved my (first) problem. 8)

[edited]
Lol,… fast help here,… how nice.
I will change the interval to 20ms,… as I noticed the bump you where talking about.

Tnx again :wink:
[/edited]