Using CCP1 and CCP2 to measure instant fuel consumption


Closed Thread
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Sao Paulo - Brazil
    Posts
    92

    Default Using CCP1 and CCP2 to measure instant fuel consumption

    Hello friends,

    some time ago, I did a digital speedometer for my car using the following code to count pulses (written by Tim_B for Proton Basic)

    ==============================================

    Define LCD_DREG PORTA
    Define LCD_DBIT 0
    Define LCD_RSREG PORTB
    Define LCD_RSBIT 4
    Define LCD_EREG PORTB
    Define LCD_EBIT 5

    REM Variables
    TMR0_POSTCOUNT1 var WORD ' POSTSCALER COUNTERS
    COUNT_RESULT var WORD ' THE RESULT OF THE COUNT
    REFRESHED var BIT ' FLAG TO SAY COUNT REFRESHED

    '-------------------------'
    ON INTERRUPT GOTO PULSE_COUNT ' DEFINE WHERE TO JUMP TO ON AN INTERRUPT
    '-------------------------'

    GOTO INITIALIZE ' JUMP ALL INT CODE

    '----- INTERRUPT CODE -------------------
    Disable
    PULSE_COUNT:
    TMR0 = TMR0 + 7 ' RELOAD THE TIMER
    TMR0_POSTCOUNT1 = TMR0_POSTCOUNT1 - 1
    IF TMR0_POSTCOUNT1 = 0 THEN
    TMR0_POSTCOUNT1 = 500 ' 1/4 sec (may need some adjust)
    T1CON.0 = 0 ' TURN OFF THE TMR1
    COUNT_RESULT.lowbyte = TMR1L ' GET THE COUNT
    COUNT_RESULT.Highbyte = TMR1H ' GET THE COUNT
    TMR1L = 0
    TMR1H = 0
    T1CON.0 = 1
    REFRESHED = 1 ' SET FLAG TO SAY COUNT READ
    ENDIF
    INTCON.2 = 0 ' CLEAR INTTERUPT FLAG
    Resume
    enable ' RELOAD THE SYSTEM VARS
    '------ END OF INTERRUPTS CODE ----------

    '------ INTIALIzE ALL REGS --------------
    INITIALIZE:

    TMR0_POSTCOUNT1 = 500 ' INITIALZE THE POST SCALERS
    OPTION_REG.0 = 0 ' SET THE PRESCALER TO 1:1
    OPTION_REG.1 = 0
    OPTION_REG.2 = 0
    OPTION_REG.5 = 0 ' TMR0 CLOCK FROM MAIN OSC
    OPTION_REG.3 = 0 ' IN THIS INSTANCE PRESCALER IS ASSINED TO TMR0
    INTCON.2 = 0 ' CLEAR INTERRUPT FLAG
    T1CON = %00000110
    T1CON.0 = 1 ' TURN TIMER 1 ON
    TMR0 = 7 ' LOAD THE TIMER
    INTCON.5 = 1 ' ENABLE TMR0 INTERRUPTS
    REFRESHED = 0 ' DONT MAKE FIRST READ UNTIL MAIN INT HAS OCCURED
    INTCON.7 = 1 ' START THE WHOLE THING RUNNING
    COUNT_RESULT = 0
    pause 500
    lcdout $FE,1



    '------ MAIN CODE BELOW HERE -------------

    LOOP:
    IF REFRESHED = 1 THEN
    lcdout $fe,1,$fe,2, "Freq = ", dec (COUNT_RESULT * 4) ' Show results in pulses / Sec
    REFRESHED = 0 ' PREVENT A RE-READ UNTIL NEXT MAIN INTERRUPT
    ENDIF
    GOTO LOOP

    END

    ===========================================

    Well, I bought a new car (used, but newer than the other one). I had do modify a lot my circuit to make it working due to the Car´s Hall sensor strange bahavior. Now, it´s fixed.

    But now, I would like to measure my instat fuel consumption, reading the Injector´s "on" time and the engine´s RPM (please, see http://www.eelkevisser.nl/fuel.htm )

    I was reading the 16F877A datasheet and I noticed that CCP1 and CCP2 uses Timer1.

    Is it possible to measure those two other parameters (engine´s RPM and Injector "on"-time) using the CCP1 and CCP2 without messing my interrupts ?

    Sorry for my bad english !

    Regards.

    Sérgio

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Capture is not reliable with Timer1 in asynchronous counter mode. You're also
    stopping & clearing Timer1 in your interrupt handler which would cause
    problems with capture.

    I would change to the 18F4431. This one has a Motion Feedback Module with
    capture that uses Timer5, measures pulse widths or frequency, and it's really
    easy to use.

    The 18F4431 has everything you need.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Jun 2007
    Posts
    56


    Did you find this post helpful? Yes | No

    Default

    Hi Bruce,

    I am interested to use motion feedback module on 18F4431, could you please post sample code how to read the number of rotation ?

    Thanks,

    Johan

  4. #4
    Join Date
    Nov 2003
    Location
    Sao Paulo - Brazil
    Posts
    92


    Did you find this post helpful? Yes | No

    Default

    Thanks Bruce !

    I will check here were I live, if I can buy this device.

    But, thank you for your advice !

    Do you have tips on how to use the CCP4 and/or CCP5 on this device ?

    Regards .

    Sérgio

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Here's an example of measuring a 32kHz signal on RA2 with the Motion Feedback Capture. It's pretty simple to use.
    Code:
    DEFINE OSC 4
    DEFINE DEBUG_REG PORTC
    DEFINE DEBUG_BIT 6 
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_MODE 0 ' 1 = inverted, 0 = true
    
    T1 VAR WORD
    T2 VAR WORD
    T3 VAR WORD
    
    Capture VAR PIR3.1
    False CON 0
    True  CON 1
    
    ' auto time base reset, capture on rising to falling edge
        RiseToFall CON %01000111
    ' auto time base reset, capture on falling to rising edge
        FallToRise CON %01000110
    ' auto time base reset, frequency mode, capture on every rising edge
        Frequency  CON %01000101
      
        ANSEL0 = 0           ' All digital
        TRISA.2 = 1          ' Cap1 input pin (Capture input)
        INTCON = 0           ' Interrupts off
        TMR5H = 0            ' Clear high byte of TMR5 counter
        TMR5L = 0            ' Clear low byte
        T5CON = %00000001    ' prescale=1:1, int clock, TMR5=on
    
        GOTO Main
        
    Trash: ' Trash 1st capture after capture mode changes
        Capture = False       ' Reset capture flag
        WHILE Capture = False ' Wait for capture
        WEND                  ' 
        Capture = False       ' Reset capture flag
        RETURN                ' Return 
    
    Main:
        GOSUB LtoH
        GOSUB HtoL 
        GOSUB Freq
        PAUSE 500
        GOTO Main
    
    LtoH:
        ' Capture on falling to rising edge
        CAP1CON = FallToRise 
        GOSUB Trash
        WHILE Capture = False 
        WEND
        T1.HighByte = CAP1BUFH ' high byte
        T1.LowByte = CAP1BUFL  ' low byte
    
        DEBUG "Low PW = ",DEC T1,"uS",13,10
        RETURN
        
    HtoL:
        ' Capture on rising to falling edge
        CAP1CON = RiseToFall
        GOSUB Trash
        WHILE Capture = False
        WEND
        T2.HighByte = CAP1BUFH
        T2.LowByte = CAP1BUFL
    
        DEBUG "High PW = ",DEC T2,"uS",13,10
        RETURN
        
    Freq:    
        ' Frequency measurement mode, capture every rising edge
        ' to measure period
        CAP1CON = Frequency
        GOSUB Trash
        WHILE Capture = False
        WEND
        T3.HighByte = CAP1BUFH
        T3.LowByte = CAP1BUFL
    
        DEBUG "Freq = 1/",DEC T3,"uS",13,10
        RETURN
    
        END
    Returns;
    High PW = 17uS
    Low PW = 14uS
    Freq = 1/31uS

    This 18F series has a boat-load of nifty features. Check out how fast the A/D
    is. Should be a killer PIC for robotics applications.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Nov 2003
    Location
    Sao Paulo - Brazil
    Posts
    92


    Did you find this post helpful? Yes | No

    Default

    Thank you very Much Bruce !

    regards .

    Sérgio

Similar Threads

  1. CCP1 and CCP2 to measure 2 frequencies together
    By bobonapoletano in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 28th December 2005, 07:34

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts