![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #46 |
|
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);
}
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. | |
| |
| | #47 |
|
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! | |
| |
| | #48 |
|
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. | |
| |
| | #49 |
|
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... | |
| |
| | #50 | |
| Quote:
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. | ||
| |
| | #51 |
| 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);
}
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? | |
| |
| | #52 |
|
You code looks correct except that the mid position for the servo should be 1500 - 1.5mS. Mike. | |
| |
| | #53 |
|
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.. | |
| |
| | #54 |
|
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 | |
| |
| | #55 |
|
What PICDEMO board are you using
| |
| |
| | #56 |
|
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. | |
| |
| | #57 |
|
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. | |
| |
| | #58 |
|
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 | |
| |
| | #59 |
|
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.. | |
| |
| | #60 |
|
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 | |
| |
|
| 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 |