# Servo motor control...

Status
Not open for further replies.

#### koolguy

##### Active Member
Last edited by a moderator:

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 ;
//  ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)
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
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:

#### koolguy

##### Active Member
Right now i don't have PICkit3 so, can't go without it for this project.(very costly)

#### misterT

##### Well-Known 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
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:

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.

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
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
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
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
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:

#### koolguy

##### Active Member
These servo's are soooo easy to control.... Set up a 1.9khz pwm...
But i have listen they work on 50Hz

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
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
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
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...
• 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

#### koolguy

##### Active Member
Here is the brand...
Exact replacement for Futaba S3003 and Hitec HS-311.

#### misterT

##### Well-Known 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.

Replies
0
Views
2K
Replies
9
Views
3K
Replies
10
Views
3K
Replies
6
Views
738
Replies
4
Views
3K