Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

connecting a pwm servo(RKI 1210 aka HS311) to pic16f877a

Status
Not open for further replies.
sorry for coming off so redundant. what i meant was - am i supposed to get the PWM with 5v magnitude and varying duty cycles . just wanted to clarify if thats what i am expecting. nways will check and gt back to you tmrw.
 
What a pity ...

By the time you were talking, I could write a working program ( yess ... I'm a MkC user !!! ) and verify it with their debugger ...

for the correct pulse length ... I can't answer: two different servo models will need different pulses for the -90 and + 90 positions; only test with a calibrated pulse generator , reaching mechanical maximas can tell ...

i.e : Futaba will need 0.25 to 2.50 ms, Graupner will need 0.6 to 2.4 ms ... with awful non-linearities by the extrema ...

Alain
 
Last edited:
sorry for coming off so redundant. what i meant was - am i supposed to get the PWM with 5v magnitude and varying duty cycles . just wanted to clarify if thats what i am expecting. nways will check and gt back to you tmrw.

Yes, PIC's output logic levels, roughly 0V to roughly 5V and you're looking for positive going pulses (lengths as above from Acetronics) every 20mS or so (although the 20mS isn't at all critical).
 
The motor worked. it turns from 0 to 90 to 180 and any other angle in between. it has a precision of 1 degree. one and half months work .. phew! :)
the only problem now is that we got it working using the " delay_us" function in mikro C .. but the thing is our "time delay" value is a variable which will vary with respect to the angle required for the motor turn.
when we tried using "vdelay_ms()" and give a value such as "0.6" or 1.2 in the brackets the program doesnt work how it is expected to. can someone help me to write a program wher i can give the variable delay in us instead of ms. something like "vdelay_us" which doesnt exist in mikro C. Alain , you said your familiar with mKC rite? any help?

Nigel, the waves in the DSO where perfect. they had a magnitude of 4V across a 220 ohm resistor.
the mikro c prog i used was :


void main()
{
float x ; // used when the vdelay_ms function was used
int count,time;
PORTC=0 ;
TRISC=0 ;

count=0;

while (1)
{

while(count<50) // moves the motor to 90 degrees
{
PORTC.F3=1;
delay_us(1499);
PORTC.F3=0;
delay_us(20000-1499);

count++;
}

// WHEN 1499 us was given in delay_us function as in above it worked
//to move the motor to 90 degrees but when used in vdelay_ms function
// went to only 49.8 degrees. when x is changed there is no continous change
// in degrees but in irregular intervals

//while(count<50)
//{
//x=1.499;
//PORTC.F3=1;
//vdelay_ms(x);
//PORTC.F3=0;
//vdelay_ms(20-x);
//count++;
//}

count=0;

while(count<50)
{
PORTC.F3=1;
delay_us(2395); // moves the motor to 180 degrees
PORTC.F3=0;
delay_us(20000-2395);

count++;
}

count=0;
}

}
 
Hi,

This program is much, much better than previous ... and that was what I wrote yesterday for a " 3 fixed positions " requirement.

Code:
void main()
{
int x,y,a;
CMCON =7;
ADCON1 =7;
PORTC=0 ;
TRISC=0 ;

while(1)
 {
  x=0;
  while ( x<50)
  {
    PORTC.F3=1;
    delay_us(600);
    PORTC.F3=0;
    delay_us(19400);
    x++;
  }


//  delay_ms(1000);

  x=0;
  while ( x<50)
  {
    PORTC.F3=1;
    delay_us(1500);
    PORTC.F3=0;
    delay_us(18500);
    x++;
  }
//  delay_ms(1000);

//  y=2400 ;

  x=0;
  while ( x<50)
  {
  PORTC.F3=1;
  delay_us(2400);
  PORTC.F3=0;
  delay_us(17600);
  x++;
  }

//  delay_ms(1000);
 }
}


Now there's no miracle using the Delay functions of MikroC ... if values must vary, the general use is to use a timer that toggles a pin ...

A little "surfing" on MkE forums will show you Warren Schroeder, alias XOR has written and shared lots of servos dedicated programs ...

Personnally, I prefer to write my R/C dedicated programs with PicBasicPro ... which is, for these tasks, something really handy ... even much more Than MkBasic ... or generally speaking C compilers.

AND ... do not forget servos have a position error, proportionnal to their load, that easily can reach 2 to 3 degrees ... they also show a deadband of 2-5µs around the current position, and a no-load position error that hardly is < 1° ...

So, decently, no need to look for more than half a degree for positionning ... and I think to the best servos available on the market !!!

Alain
 
Last edited:
as already said ...

use a timer and an interrupt to toggle your pin !!!

Examples provided on MkC forum ...

Nigel also has pointed out the "20 ms" between pulses is not to be strictly observerved !!! 15 to 25 , eventually fluctuating a bit will fit .

Alain
 
Last edited:
i thought it would be simpler with a delay function. wont it get more complicated with a timer and interrupt hence making the chance of errors more? i though since we;re using mikro c it would become more simpler.
 
and i read the mikro c for forum and they've used asm coding for using timers. im new to mikro c so i figured since you were an experienced user u'd be able to suggest some simpler techniques. i started with mikro c programming only after i tried timers in asm coding using mplab interface.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top