Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 25th October 2009, 11:27 AM   #46
Default

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.
Pommie is online now  
Old 25th October 2009, 12:23 PM   #47
Default

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!
yohanevindra is offline  
Old 25th October 2009, 12:29 PM   #48
Default

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.
Pommie is online now  
Old 25th October 2009, 12:47 PM   #49
Default

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...
yohanevindra is offline  
Old 25th October 2009, 04:36 PM   #50
Default

Quote:
Originally Posted by Pommie View Post
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 by be80be; 26th October 2009 at 02:33 AM.
be80be is offline  
Old 26th October 2009, 06:04 AM   #51
Default

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?
yohanevindra is offline  
Old 26th October 2009, 06:57 AM   #52
Default

You code looks correct except that the mid position for the servo should be 1500 - 1.5mS.

Mike.
Pommie is online now  
Old 26th October 2009, 09:52 AM   #53
Default

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..
yohanevindra is offline  
Old 26th October 2009, 10:18 AM   #54
Default

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.
Pommie is online now  
Old 26th October 2009, 03:16 PM   #55
Default

What PICDEMO board are you using
be80be is offline  
Old 26th October 2009, 04:03 PM   #56
Default

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 by yohanevindra; 26th October 2009 at 04:06 PM.
yohanevindra is offline  
Old 26th October 2009, 04:07 PM   #57
Default

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.
Pommie is online now  
Old 26th October 2009, 04:14 PM   #58
Default

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
be80be is offline  
Old 26th October 2009, 04:22 PM   #59
Default

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..
yohanevindra is offline  
Old 26th October 2009, 04:32 PM   #60
Default

i started a new thread here fr my problems, since it didnt seem relevant to the topic of this thread. sorry guys!

servo control problems
yohanevindra is offline  
Reply

Tags
c18, code, junebug, servo

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
MP Lab Program Help bamafan54 Micro Controllers 5 7th January 2009 03:16 PM
servo motor program code basf_12 Electronic Projects Design/Ideas/Reviews 8 31st October 2006 03:57 AM
Tough assembly program for the PIC16F84 asmpic Micro Controllers 34 3rd December 2004 07:50 PM
how to understand this code!!! indie Electronic Projects Design/Ideas/Reviews 3 11th September 2004 08:39 PM
An error in pic16f84a, why? Zener_Diode Micro Controllers 6 11th April 2004 03:55 AM



All times are GMT. The time now is 06:35 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker