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.

Servo motor control...

Status
Not open for further replies.
For a 180 degree RC servo.. extreme 90 degrees is 1mS pulse width.. In the other direction -90 degrees is 2mS, wit 1.5mS in the center....

Oh!! yeah sorry..... If you make the delay routines yourself (As I have done ) you can pass a variable to the delay function..

If you create a delayUs() that excepts a long then you can add you ADC result to 1000.

So if your pot is tracked to one side the ADC will be zero (1000) .... If your pot is mid-way (512) the servo will go center ( 1512 ) and if the pot was at max you'll get (2024).

Just pass this

DelayUs(ADC_result + 1000);
 
Last edited:
For 20Mhz what should be here?
x/=7 //8mhz
Code:
void main()
    {
    TRISC = 0 ;
  unsigned int number ;    
    unsigned int i;

TRISA = 0xff ;
  ADCON1=0b10000000;
  ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)
  ADIF=0;
ADIE=1;
  PEIE=1;



while(1){


number = ReadADC(0);

RC2=1;

DelayUs(number+ 1000);
RC2=0;
__delay_ms(20);



}

    }
int ReadADC(void)
    {
    int ret = 0;
    __delay_ms(10);
    GO_DONE = 1;                    // start conversion
      while(GO_DONE);                    // wait for conversion
      ret =  (ADRESH & 0x3) << 8;        // get
      ret +=    ADRESL;                    // result
    return ret;
    }

void DelayUs(int x)    
  {
        
  while(x--);
    
  }
 
C:
while(1){
number = ReadADC(0);
RC2=1;
DelayUs(number+ 1000);
RC2=0;
__delay_ms(20);
}

Ahh!!! A pitfall.... There will be time to read your ADC here you have introduced a 10mS delay with ReadADC..

You'l need to factor that in... So

C:
while(1){ 
   number = ReadADC(0); 
   RC2=1; 
   DelayUs(number+ 1000); 
   RC2=0; 
   __delay_ms(10); 
   }

This is the DelayUs() to get NEARLY the correct values..

C:
void DelayUs(int x)    
    {
    x/=5;
    while(x--)
        {
        NOP();
        NOP();
        NOP();
        }    
    }
 
x/=5;
while(x--)
{
NOP();
NOP();
NOP();
}
}

how you calculated its 5?? for 8Mhz you were doing x/=7?

and you didn't answer how to calculate freq as we ere adding 20ms delay for 50hz but inst fetching will also take some time so, there is no need of calculating it?
 
Hi, now it is also not working...ok i have noticed that only roatating in few ~90* clock and anticlockwise for 180* or 360 what is to be done..?
Code:
#include <htc.h>

__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);

#define _XTAL_FREQ 20000000

void DelayUs(int x);

int ReadADC();

void main()

  {

  TRISC = 0 ;

  unsigned int number ;

  unsigned int i;

TRISA = 0xff ;

  ADCON1=0b10000000;

  ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)

  ADIF=0;

ADIE=1;

  PEIE=1;

while(1){

  number = ReadADC(0);

  RC2=1;

  DelayUs(number+ 1000);

  RC2=0;

  __delay_ms(10);

  }

  }

int ReadADC(void)

  {

  int ret = 0;

  __delay_ms(10);

  GO_DONE = 1;  // start conversion

  while(GO_DONE);  // wait for conversion

  ret =  (ADRESH & 0x3) << 8;  // get

  ret +=  ADRESL;  // result

  return ret;

  }

void DelayUs(int x)

  {

  x/=5;

  while(x--)

  {

  NOP();

  NOP();

  NOP();

  }

  }
 
Last edited:
you are one the people i learn from, would be a shame if you didnt post

Maybe if it would be possible to personally block all posts from India. about 35% of traffick to ETO comes from india these days. And I think well over half of new members are from India that post one question.. seeking for help for homework or their actual job. I can't tolerate that much longer. This used to be more professional forum.. now it is a helpdesk for Indian students. (Actually Ritesh claims to have a degree in engineerin. And he is trying to run his own business as far as I know.. and he comes here to ask us to do all the designing and coding.)
 
Last edited:
Maybe if it would be possible to personally block all posts from India. about 35% of traffick to ETO comes from india these days. And I think well over half of new members are from India that post one question.. seeking for help for homework or their actual job. I can't tolerate that much longer. This used to be more professional forum.. now it is a helpdesk for Indian students. (Actually Ritesh claims to have a degree in engineerin.)

I gave up a long time ago. Shame really.

Mike.
 
Maybe if it would be possible to personally block all posts from India. about 35% of traffick to ETO comes from india these days. And I think well over half of new members are from India that post one question.. seeking for help for homework or their actual job. I can't tolerate that much longer. This used to be more professional forum.. now it is a helpdesk for Indian students. (Actually Ritesh claims to have a degree in engineerin. And he is trying to run his own business as far as I know.. and he comes here to ask us to do all the designing and coding.)

This problem is not inherent in India. Many of the nations in Asia have the same problem. We are using a lot of the classic old "British System" education and still adhering to that.
 
Maybe if it would be possible to personally block all posts from India. about 35% of traffick to ETO comes from india these days. And I think well over half of new members are from India that post one question.. seeking for help for homework or their actual job. I can't tolerate that much longer. This used to be more professional forum.. now it is a helpdesk for Indian students. (Actually Ritesh claims to have a degree in engineerin. And he is trying to run his own business as far as I know.. and he comes here to ask us to do all the designing and coding.)
So, what is to be done?
when people ask from me i also help them as you.
 
So, what is to be done?

Learn. Stop being lazy. You come here asking same questions again and again. You don't bother to explain the project and the problem properly. You do not draw any schematics, you copy-paste most of your code. It is so frustrating to try to help you when you do not put any real effort to your projects.
 
Hi, now it is also not working...ok i have noticed that only roatating in few ~90* clock and anticlockwise for 180* or 360 what is to be done..?


I really don't understand this comment at all...... That code was for 90 degrees.. I know it worked because I tested it before I sent it to you...

Calculation..... A while statement consumes 7 clock cycles... 3 x NOP() = 3, Approximately 10 cycles ..

A 20Mhz clock gives a .2nS clock cycle
10 * 2nS = 5uS
1500 / 5 = 300 ... dividing by 5 gives 300 iterations.

300 * 5uS = 1.5mS.... (There are other little hindrances.. ie call times so it need fetling )
 
Please help to clarify that. With respect to using 1.9 KHz were Ian Rogers and misterT referring to "modern" industrial servos, "modern" model servos that work at that frequency (if so, please give a brand and model number), or something else?

Well, "industrial" is a bit overstatement. But, a modern servo is a digital servo. What Ritesh has is a "traditional" analog servo. Digital servos are more accurate and faster. I would stick to the standard 50 Hz control signal with analog servos. I don't know the "speed limits" of either servo. Looks like 300 Hz is standard for digital servos.
 
1500... 1.5mS delay

Code:
;RCservo.c: 6: while(x--)
 
   movlw   -1
   addwf   DelayUs@x,f
   skipc
   decf   DelayUs@x+1,f
   incf   DelayUs@x,w
   skipnz
   incf   DelayUs@x+1,w
   btfsc   3,2
   return
     
   ;RCservo.c: 7: _nop();
   nop
   goto   l554
__end_of_DelayUs:

Here's the while statement.....
 
**broken link removed**
 
Post #61....

If you create a delayUs() that excepts a long then you can add you ADC result to 1000.

So if your pot is tracked to one side the ADC will be zero (1000) .... If your pot is mid-way (512) the servo will go center ( 1512 ) and if the pot was at max you'll get (2024).

Just pass this

DelayUs(ADC_result + 1000);

Just goes to prove you don't read what we post to help you
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top