1. 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.
    Dismiss Notice

Junebug Servo C18 code.

Discussion in 'Microcontrollers' started by Pommie, Feb 6, 2008.

  1. yohanevindra

    yohanevindra New Member

    Joined:
    Nov 26, 2008
    Messages:
    143
    Likes:
    0
    Location:
    Sydney, Australia & Colombo, Sri Lanka
    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: Oct 25, 2009
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,008
    Likes:
    316
    Location:
    Brisbane Australia
    I just tried your code in MPSIM and it appears to work fine.

    The code I tried is,
    Code (text):

    #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.
     
  3. yohanevindra

    yohanevindra New Member

    Joined:
    Nov 26, 2008
    Messages:
    143
    Likes:
    0
    Location:
    Sydney, Australia & Colombo, Sri Lanka
    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!
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,008
    Likes:
    316
    Location:
    Brisbane Australia

    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.
     
  6. yohanevindra

    yohanevindra New Member

    Joined:
    Nov 26, 2008
    Messages:
    143
    Likes:
    0
    Location:
    Sydney, Australia & Colombo, Sri Lanka
    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...
     
  7. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,793
    Likes:
    134
    Location:
    morristown,tn
    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: Oct 25, 2009
  8. yohanevindra

    yohanevindra New Member

    Joined:
    Nov 26, 2008
    Messages:
    143
    Likes:
    0
    Location:
    Sydney, Australia & Colombo, Sri Lanka
    Code (text):

    #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?
     
  9. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,008
    Likes:
    316
    Location:
    Brisbane Australia
    You code looks correct except that the mid position for the servo should be 1500 - 1.5mS.

    Mike.
     
  10. yohanevindra

    yohanevindra New Member

    Joined:
    Nov 26, 2008
    Messages:
    143
    Likes:
    0
    Location:
    Sydney, Australia & Colombo, Sri Lanka
    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..
     
  11. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,008
    Likes:
    316
    Location:
    Brisbane Australia
    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 (text):

    #define AzimuthServoPin PORTAbits.RA2
    //should be
    #define AzimuthServoPin LATAbits.LATA2
     
    Mike.
     
  12. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,793
    Likes:
    134
    Location:
    morristown,tn
    What PICDEMO board are you using
     
  13. yohanevindra

    yohanevindra New Member

    Joined:
    Nov 26, 2008
    Messages:
    143
    Likes:
    0
    Location:
    Sydney, Australia & Colombo, Sri Lanka
    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 (text):

    #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: Oct 26, 2009
  14. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,008
    Likes:
    316
    Location:
    Brisbane Australia
    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.
     
  15. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,793
    Likes:
    134
    Location:
    morristown,tn
    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
     
  16. yohanevindra

    yohanevindra New Member

    Joined:
    Nov 26, 2008
    Messages:
    143
    Likes:
    0
    Location:
    Sydney, Australia & Colombo, Sri Lanka
    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..
     
  17. yohanevindra

    yohanevindra New Member

    Joined:
    Nov 26, 2008
    Messages:
    143
    Likes:
    0
    Location:
    Sydney, Australia & Colombo, Sri Lanka

Share This Page