# servo control problems

Status
Not open for further replies.

#### yohanevindra

##### New Member
so im using the PICDEM2+ board and the minimal PIC18 board, and the servos just wont respond to the control signals

the servos keep twitching and dont respond to the control signals from the PIC..sometimes the twitching is more controlled with a control signal, & sometimes it isnt.they twitch from the time i plug in the power..

the oscillocope shows perfect pulses of min 1ms and max 2ms. what am i doing wrong/not doing?

the power supply for it is 5V regulated 3A.

we've tried about 3 different servos, same story..

it doesnt work in proteus either... here's the code again.. for 10Mhz..this code has been modified using the code given in the "junebug servo code" thread by pommie...

as per be80be reply, my board does not have the Y1 oscialltor and C4 and C5 capacitors. Y2 is the 4MHz osciallator.

Code:
#include <p18f452.h>

#define AzimuthServoPin PORTAbits.RA2
//#define ElevationServoPin PORTAbits.RA5

int ServoPosition;

void CCP2_ISR();

#pragma code high_pri_vec = 0x0008
void high_pri_vec()
{
_asm
GOTO CCP2_ISR
_endasm
}

#pragma interrupt CCP2_ISR
void CCP2_ISR()
{
if(AzimuthServoPin==1)
{
AzimuthServoPin = 0;
CCPR2 = 25000 - ServoPosition;
}
else
{
AzimuthServoPin = 1;
CCPR2 = ServoPosition;
}
PIR2bits.CCP2IF = 0;

}

#pragma code
void main()
{
TRISAbits.TRISA2 = 0; //Set Pin A2 as an output for the Servo
AzimuthServoPin = 0;
CCP2CON = 0b00001011; //Setup CCP2 to generate special event
ServoPosition = 1875; //Mid point position for Servo
CCPR2 = ServoPosition;
T1CONbits.RD16 = 1;
T1CONbits.T1OSCEN = 1;
T3CON = 0b11001001; //Setup timer3, prescaler=2
//TMR3H = 0;
//TMR3L = 0;
PIE2bits.CCP2IE = 1; //Enable CCP2 interrupt
INTCONbits.PEIE = 1; //Enable peripheral interrupts
INTCONbits.GIE = 1; //Enable all interrupts
while(1);
}

#### Mr RB

##### Well-Known Member
What's the low period between high pulses? They will twitch like that if the low period is too long. Check on your CRO and make it 1.5mS HIGH period and 20mS LOW period. That should centre the servo with no twitching.

#### Pommie

##### Well-Known Member
Just noticed that you haven't got a config line. Are you setting your config manually?

Maybe you should add a config line,
Code:
#include <p18f452.h>

[COLOR="Red"]#pragma config WDT=OFF,LVP=OFF,OSC=XT,OSCS=OFF,DEBUG=ON[/COLOR]

#define AzimuthServoPin LATAbits.LATA2

Without a config it will default to RC oscillator and that would cause problems. It also explains why it works in the simulator.

If your not using debug then set it to OFF.

Mike.

Last edited:

#### yohanevindra

##### New Member
so that configuration line is to configure
watch dog timer - off
low voltage programming - off
OSC -?
OSCS - ?
DEBUG - on?

im using the PICDEM2+ with a 4MHz crystal and then later on on a 10MHz crystal on a minimal PIC18 board...
will i hav to change the config stuff once i program it on the final PIC?

#### Pommie

##### Well-Known Member
With the 4Meg crystal you should use OSC=XT and for the 10MHz use OSC=HS. Actually, both should work fine as HS. See table 2.2 in the data sheet.

The OSCS setting stands for Oscillator Switch which allows the system to switch to the timer1 oscillator so this is best left off.

Mike.

#### yohanevindra

##### New Member
we ran the servos on a test circuit we found on the internet, and it seemed to work ok. but when we run it on the code it behaves really weird... did the onfig bit change, and still the same...

#### Slorn

##### New Member
Hi

Here's a great tutorial that uses a servo controlled by a PIC18:

It shows how to build a very simple light tracking robot with only a servo and two light sensors.

Slorn

Status
Not open for further replies.

Replies
0
Views
2K
Replies
6
Views
2K
Replies
122
Views
9K
Replies
24
Views
8K
Replies
3
Views
989