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

C18 Questions

Discussion in 'Microcontrollers' started by 3v0, Jun 25, 2009.

  1. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    Try,
    perc=(diff*100/ceilingrd);

    Mike.
     
  2. Wond3rboy

    Wond3rboy Member

    Joined:
    Jun 25, 2008
    Messages:
    762
    Likes:
    7
    Location:
    Sindh,Pakistan
    Thanks Mike it works.
     
  3. Triode

    Triode Member

    Joined:
    Feb 11, 2009
    Messages:
    936
    Likes:
    22
    Location:
    Bay Area
    I'm trying to implement a timer interrupt in the PWM capture code above, its purpose will be to generate a timed PWM signal to control a motor based on the captured servo signal.

    Am I correct in thinking that any low interupt will call that ISR function? It seems like it would, is an if statement like the one I used there:
    "if(PIR1bits.CCP1IF == 1)" which reads the interrupt flag the normal way to handle this?

    Also, I don't have specific questions about this because I haven't really gotten into it yet, but if anyone can give me hints, or show me how to set up a timer interrupt in my PWM capture code it would be helpfull. I'm going to start trying to figure it out when I get home and I'll post my code so far.
     
    Last edited: Feb 16, 2010
  4. dave

    Dave New Member

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


     
  5. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA

    A percentage is the (amount)/(max possible). Execpt for the case where they are equal it will always be less then 1 which is zero in integer math.
    To overcome this we scale the number to get a percentage number instead of a fraction. We use (amount*100)/(possible). It works well.

    The problem you are seeing is that you amount is 0x18 and the possible is 0x1018. This works out to about 0.0058. If we use our percent scaling we will still get a zero!

    In short the math is correct. Zero is the right answer. You could choose to do rounding and then you would get 1%.

    Please PM me. Sorry about the slow reply
     
  6. Triode

    Triode Member

    Joined:
    Feb 11, 2009
    Messages:
    936
    Likes:
    22
    Location:
    Bay Area
    Heres my servo receiver code if anyone is interested, I changed it just a bit. Now if used on the junebug it will change between the leds above button 1 if you go forward and reverse. You just need to connect the reciever pins that would go to a servo to the 3 pin port that's on the opposite side of the junebug from the USB port (Red to U5V, Black to GND and White to RB3)

    Code (text):
    //Basic configureation of chip periferials
     #pragma config OSC = INTIO2, WDT = OFF, LVP = OFF
     #include <p18f1320.h>

    //setup pins for PWM input
    #define ReceiverPin PORTBbits.RB3
    #define ReceiverTris TRISBbits.TRISB3


    //PWM capture variables
    unsigned int PWM1RiseTime = 0; //timer value at rising edge capture
    unsigned int PWM1Width = 0; //calculated width
    char PWM1Edge = 1; //edge currently being monitored 1 = rising, 0 = falling

    //set up interrupt
    void low_ISR(void);//prototype
    #pragma code low_vector = 0x08
    void low_interrupt (void){
    _asm goto low_ISR _endasm
    }
    #pragma code
    #pragma interrupt low_ISR

    //debug stuff
    char test1 = 0;
    char test2 = 0;

    void main(void)
    {  
        unsigned int MotorCount = 1;
        unsigned char MotorOn; 

        OSCCON = 0x72; //8MHz clock
        while(!OSCCONbits.IOFS); //Wait for OSC to become stable

        //configure timer1
        OpenTimer1(0b10010101);
        PIE1bits.TMR1IE = 1; //periferial interupt register, 1 = timer 1 interupt enabled
        PIR1bits.TMR1IF = 0; //clears the timer 1 interupt flag

        //configure CCP1
        CCP1CON = 0b0000101; //configure CCP1 for capture, rising edge
        INTCONbits.PEIE=1;      //enable peripheral interrupts
        PIE1bits.CCP1IE=1;      //enabled CCP1 interrupt
        INTCONbits.GIE=1;       //enable branching to interrupt
       
        ReceiverTris = 1; //set RB3 for input so the capture can work.
       
        //configure ports
        ADCON1 = 0xff; //all digital
        INTCON2bits.RBPU = 0; //port b weak pullups on
       
        //configure control bits for output
        TRISAbits.TRISA0 = 0;
        TRISAbits.TRISA6 = 0;
        TRISAbits.TRISA4 = 0;
        //enable bit
        LATAbits.LATA4 = 1; //Enable bit
       

        while(1)
        {
           

        }
    }


    void low_ISR(void)
    {      

        test1 = 1;
        //ccp interrupt
        if(PIR1bits.CCP1IF == 1)
        {  
            PIR1bits.CCP1IF = 0; //clear the flag
            if(PWM1Edge == 1)//if detecting rising
            {  
               
                PWM1RiseTime = CCPR1;//save the low timer value for the rise time
                CCP1CON = 0b0000100;//switch to detect falling edge
                PWM1Edge = 0;//switch to indicate falling edge is next
            }
            else //detecting falling
            {

                PWM1Width = CCPR1 - PWM1RiseTime;
                CCP1CON = 0b0000101;//switch to detect rising edge
                PWM1Edge = 1;//switch to indicate rising edge is next
            }

            //This stuff is just for testing
            if(PWM1Width > 1800)
            {
                LATAbits.LATA6 = 0;
                LATAbits.LATA0 = 1;
            }
           
            if(PWM1Width < 1400)
            {
                LATAbits.LATA6 = 1;
                LATAbits.LATA0 = 0;
            }

            if(PWM1Width > 1400 && PWM1Width < 1800)
            {
                LATAbits.LATA6 = 0;
                LATAbits.LATA0 = 0;
            }
           
        }
    }
     
    now all i need is to put in a pwm output that changes with that number and reverses in the middle and im good to go.
     
  7. Wond3rboy

    Wond3rboy Member

    Joined:
    Jun 25, 2008
    Messages:
    762
    Likes:
    7
    Location:
    Sindh,Pakistan

    Hi 3V0 Thanks for your reply. I already showed you the board in chat. It was my PICDEMZ clone. :)
     

Share This Page