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.

misterT

Well-Known Member
Most Helpful Member
Last edited:

koolguy

Active Member
For servo i have modified this code for getting 50Hz PWM.
without ISR it was 305HZ at 1:16 and PR at 255.
so using ISR I am calculating it...

Code:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
void main()
  {
  //TRISA = 0xff ;
  //ADCON1=0b00000000;
//  ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)
//  ADIF=0;
  TRISC = 0 ;
  PR2 = 255 ;
  T2CON = 0b00000100 ; 
  CCP1CON = 0b00111100; 
  PORTC = 0 ; 
  while(1)
    {
  CCPR1L = 0 ;
    }
  }
void interrupt ISR()    // This just swaps the buffer to the display
  {
  if(TMR2IF)        // make sure its the timer interrupt.
    {
    CCPR1L = 0 ;//??
    }
  TMR2IF = 0;
  }
 

misterT

Well-Known Member
Most Helpful Member
Why don't you buy the PicKit3 that has a 3 channel logic analyzer? It would make your work so much easier. How are you going to verify that the signal is what it is supposed to be? And more importantly; when the signal is wrong, you will need to know how wrong it is. Is it too fast, too slow, or no signal at all etc.
 
Last edited:

misterT

Well-Known Member
Most Helpful Member
Right now i don't have PICkit3 so, can't go without it for this project.(very costly)

Ok.. I understand. If I remember correctly, you use PIC16F887 for all your projects.. and you have done PWM before, so you must be familiar with it. What is your question or problem?
 

koolguy

Active Member
Ok.. I understand. If I remember correctly, you use PIC16F887 for all your projects.. and you have done PWM before, so you must be familiar with it. What is your question or problem?
I want to create 50Hz and data sheet says..
Detailed Specifications
Control System: +Pulse Width Control 1500usec Neutral
Required Pulse: 3-5 Volt Peak to Peak Square Wave
Operating Voltage: 4.8-6.0 Volts
Operating Temperature Range: -20 to +60 Degree C
Operating Speed (4.8V): 0.19sec/60° at no load
Operating Speed (6.0V): 0.15sec/60° at no load
 

misterT

Well-Known Member
Most Helpful Member
I'm sorry I can't help you with complete settings (because I don't work with PICs that much), but you should be reading the PIC datasheet:
https://ww1.microchip.com/downloads/en/DeviceDoc/41291D.pdf

Starting from page 128 are the PWM mode instructions.

The datasheet also gives a nice protocol to setup PWM operation:

11.5.7 SETUP FOR PWM OPERATION
The following steps should be taken when configuring the CCP module for PWM operation:

1. Disable the PWM pin (CCPx) output drivers as an input by setting the associated TRIS bit.

2. Set the PWM period by loading the PR2 register.

3. Configure the CCP module for the PWM mode by loading the CCPxCON register with the appropriate values.

4. Set the PWM duty cycle by loading the CCPRxL register and DCxB<1:0> bits of the CCPxCON register.

5. Configure and start Timer2:
• Clear the TMR2IF interrupt flag bit of the PIR1 register.
• Set the Timer2 prescale value by loading the T2CKPS bits of the T2CON register.
• Enable Timer2 by setting the TMR2ON bit of the T2CON register.

6. Enable PWM output after a new PWM cycle has started:
• Wait until Timer2 overflows (TMR2IF bit of the PIR1 register is set).
• Enable the CCPx pin output driver by clearing the associated TRIS bit.
 

koolguy

Active Member
I have created PWM but its min frq with 20Mhz and 1:16 pre scaller is
without ISR it was 305HZ at 1:16 and PR at 255.
so using ISR I am calculating it...
 

misterT

Well-Known Member
Most Helpful Member
I have created PWM but its min frq with 20Mhz and 1:16 pre scaller is
without ISR it was 305HZ at 1:16 and PR at 255.
so using ISR I am calculating it...

Well, that sucks.. change to AVR, or slower clock, or do the servo signal with software. Is 16 really the highest prescaler you can get? Lame.

Here is one very simple software version: https://vshamu.wordpress.com/tag/servo-motor-interfacing-with-pic16f887/

Or.. was your question about how to do the servo signal using timer interrupts?
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
These servo's are soooo easy to control.... Set up a 1.9khz pwm... set the pwm to 50:50 and the servo sits at mid position.... set the pwm to 99:1 and the servo shoots to 90 degrees swap it round to 1:99 and the servo shots to -90 degrees...
 

large_ghostman

Well-Known Member
Most Helpful Member
do a forum search, pommie did a really good code for servo's using a junebug, but the code could easily be modded for your chip. i cant remember the post but search for servo code and it should show up. servo pulses are best done with the timer rather than PWM part of chip IIRC.
 

koolguy

Active Member
for 1.9khz PR2 39 and 1:16 pre scaller @20Mhz 1.9khz and 512uSec. and CCP1L=127 FOR mid , 1 for right and 254 for left right??
 

misterT

Well-Known Member
Most Helpful Member
for 1.9khz PR2 39 and 1:16 pre scaller @20Mhz 1.9khz and 512uSec. and CCP1L=127 FOR mid , 1 for right and 254 for left right??

Try it.

If you feed the servo too short pulse, or a too long pulse, it will make an ugly sound, but it will not break it. Just don't stress it for too long. You can find the limits of the servo by experimenting.
 
Last edited:

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Not as efficiently though.... If your PWM is 20khz the motion is smooth and clean.... At 50hz the motor will appear sluggish.. I found that the control is different when using low frequency.... I have a BIG motor that I control with PWM @ 1.9khz The response is really good ( There is a hum, but as it's on a crane I don't give a monkey's) but the control is excellent.... If I use 250hz it isn't as controllable...
 

misterT

Well-Known Member
Most Helpful Member
But i have listen they work on 50Hz

The frequency is not important. The pulse width is important. The servo takes the information from the pulse width (it calculates how long the incoming pulse is). 50 Hz is the "old" standard. But you can send the pulses at higher rate to modern servos.

Why don't you just try it.. the servo is not going to break.
 

jpanhalt

Well-Known Member
Most Helpful Member
These servo's are soooo easy to control.... Set up a 1.9khz pwm... set the pwm to 50:50 and the servo sits at mid position.... set the pwm to 99:1 and the servo shoots to 90 degrees swap it round to 1:99 and the servo shots to -90 degrees...
In reading your and misterT's comments (#17), I have learned something worth trying this Winter.
  • What brand and model servo are you using?
  • Are the "modern" servos digital or analog? If digital, what is the deadband spec in uS? (If you just give the brand and model, I can look up the latter info.)
John
 

misterT

Well-Known Member
Most Helpful Member
Here is the brand...
Exact replacement for Futaba S3003 and Hitec HS-311.

Are you just going to sit and wait for code? All this help and you do nothing.

Try it! Send it pulses between 1..2 milliseconds and see if it turns! Post your results.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top