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.

Junebug Servo C18 code.

Status
Not open for further replies.
same problem...for some reason the timer is not counting into the high byte, so hence its maximum value is only 256.

im looking at the timer through the watch window, and it doesnt seem to go upto the value specified in the CCPR register, but then when i looked at the simulator logic analyser, and measured the pulses, it seems like its working. to measure the frequency of pulses in the logic analyser, do i just need to line the cursor with the respective point and then multiply the no of cycles by (Fosc/4)?
 
Last edited:
I just tried your code in MPSIM and it appears to work fine.

The code I tried is,
Code:
#include <p18f452.h>

#define ServoPin PORTAbits.RA2

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(ServoPin==1)
    {
        ServoPin = 0;
        CCPR2 = 25000 - ServoPosition;
    }
    else
    {
        ServoPin = 1;
        CCPR2 = ServoPosition;
    }
    PIR2bits.CCP2IF = 0;
    
}

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

I can see what you are saying. In the sim the high byte is not incrementing but it still seems to work.

Unfortunately, I dont have a 452 and so can't test it in the actual hardware but I can confirm that it does work in the 1320.

Have you tried switching to CCP1 and Timer1?

Mike.
 
i'll be able to test it tomorrow using the hardware and i will get back to you.

this is part of a major project which involves ultrasound sensors, IR sensors, keypad, LCD and servos.the guy working on the ultrasound is using CCP1 to pulse the US module, so i cant use that..

i'll check it using the minimal-PIC18 board tht we're using since it has the 10MHz clock and get back to you regarding whether it worked or not. thanks for your help!

or if i have time, i might even re-do the calculations for my PICDEM board and do it at home!
 
If you switch to the internal oscillator (OSCCON=0x72) then you should be able to use any hardware.

Just out of curiosity, why are you using such an old chip?

Mike.
 
internal oscillator?but then i dont need to use any extra hardware right??jus configure it and then change the calculations accordingly?

well..tht's the requirement of our course..our whole course is centred around the PIC18f452..i dont know why they still use it if its old...
 
If you switch to the internal oscillator (OSCCON=0x72) then you should be able to use any hardware.

Just out of curiosity, why are you using such an old chip?

Mike.

The PIC18f452 doesn't have a internal oscillator Most all compilers use 18f542 in there samples It's a fussy Chip.
I never had any problems using code from the 18f1320 after you set the OSC right you should be good to go
 
Last edited:
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 = 20000 - ServoPosition;
	}
	else
	{
		AzimuthServoPin = 1;
		CCPR2 = ServoPosition;
	}
	PIR2bits.CCP2IF = 0;
	
}

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

That's the code I'm running now. The program seems to work on the simulator. I earlier wanted to test it on the minimal-PIC18 board that we're using, but there was some difficulty debugging it.

the PICDEM board has a 4MHz crystal, and using no prescaler the timer will increment every 1microsecond right? so based on that the settings for CCPR2 were 20000 for the "OFF" time and between 1000-2000 for the control pulse...are these right?

however, intially when pluggin the servo in, i think we mayb hav inadvertantly plugged in -5V to the control signal pin, and then -5V to the ground pin..then we corrected everything and plugged in the ground pin to the groun of the PICDEM board and the control signal to the AN2 port..but now when i run the code i jus hear the servo buzzing..has it blown?do we need a new servo?or is there something with the code/calculations/board?
 
You code looks correct except that the mid position for the servo should be 1500 - 1.5mS.

Mike.
 
it just doesnt seem to be working :( all that we get is a buzzing sound from the servo..

MPLAB SIM indicates that the pulses on the output pin changes when i change the servoposition variable..

when i ran it in proteus, for whtever value of servoposition i entered the servo would always go to 90 degrees..
 
Do you have access to another servo or a scope? Can you flash an LED to confirm it's actually running code?

I can't see how it would make a difference unless the pin is heavily loaded but,
Code:
#define AzimuthServoPin PORTAbits.RA2
//should be
#define AzimuthServoPin LATAbits.LATA2

Mike.
 
I'm using the PICDEM2+ demo board.

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.

it doesnt work in proteus either... here's the code again.. for 10Mhz

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()
{	
	ADCON1 = 0x07;
	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);
}
 
Last edited:
If the scope shows correct pulses then it has to be the servo or the power supply. Do you see the correct pulses with the servo connected?

Mike.
 
Have you set which on of theses right
RC J7 installed, Y2 empty, Y1 empty

Crystal J7 removed, Y2 empty, crystal in Y1, caps in C4 and C5

Canned Oscillator J7 removed, oscillator in Y2 (Y1, C4, C5 empty)

Resonator – no internal caps J7 removed, Y2 empty, resonator in Y1, caps in C4 and C5

Resonator – with internal caps J7 removed, Y2 empty, resonator in Y1, C4 and C5 empty
 
should i start my own thread?since this is not relevant to the topic?

well, my PICDEM board doesnt hav Y1, C4, C5, J7 is removed and Y2 is the 4MHz oscillator..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top