Interrupt RPM and Taylors Elapsed time on 18F4620


Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 40 of 71
  1. #1

    Default Interrupt RPM and Taylors Elapsed time on 18F4620

    I see the 18F4620 has a couple 16 bit timers. I have RPM going into C2/CCP1. So if I have the code set up right I should be getting RPM. I am not though. I did have this code working just fine on a 16F877A.

    I needed to modify Taylors ET code to use Timer3. This code works just fine using Timer1 but then when I change the code around for Timer3 its not working.
    Any ideas? I am hoping I just have a couple lines of code setting up flags incorrectly and I will be able to use the ET and RPM at the same time.

    Code:
    RPM Code
    
    DEFINE OSC 20
    Prefix          con      $FE             ' needed before each command
    LcdCls          CON      $51             ' clear LCD (use PAUSE 5 after)
    CursorPS        con      $45             'Cursor Position
    Capture         VAR     PIR2.0		      ' CCP1 capture flag
    Overflow        VAR     PIR2.1	    	' Timer1 overflow flag
    RPM             var     word
    period          var     Word
    TotalTime       var     word      'Holds seconds and tics 
    
    LCD             VAR     PortC.6    'LCD output
    Gate1           Var     PortC.1
    Gate2           var     PortD.1
    Gate3           var     PortD.2
    Gate4           var     PortD.3
    Gate5           var     PortD.6
    Gate6           var     PortD.7
    Gate7           var     PortD.5
    Gate8           var     PortD.4
    WOT             var     PortB.0
     
    ADCON0 =0
    ADCON1.3=1
    ADCON1.2=1
    ADCON1.1=1
    ADCON1.0=1
    TRISE.0=1
    TRISE.1=1
    
    T1CON.7=1     'enable timer  16 bit `   
    T1CON.6=1     'Timer1 OSC
    T1CON.5=1     '1:4 prescaler
    T1CON.4=0     '1:4 prescaler
    T1CON.3=1     'Timer1 OSC enabled
    T1CON.2=0     'sychro clock
    T1CON.1=1     'external clock
    T1CON.0=0     'stop timer
    
    pause 100
    SEROUT2 LCD,84, [Prefix, LcdCls]
    
    Include "modedefs.bas"	' Mode definitions for Serout
    
    Loop:
    TMR1H = 0                              
    TMR1L = 0
    capture = 0
    
    Start:
          If Capture = 0 then
             Goto Start
          endif
          'code doesn't make it this far..assuming waiting for Cpature =1
          T1CON.0=1
          Capture = 0 
          
    CaptureLoop:
      If Capture = 0 then
        Goto CaptureLoop
      endif
      
    period.LowByte=CCPR1L
    period.HighByte=CCPR1H
    
    RPM = 10000
    RPM = RPM * RPM ' 100,000,000
    RPM = DIV32 period ' 100,000,000 / RevCount
    RPM = RPM * 60 ' Per minute
    RPM = DIV32 398 '400
    RPM = (RPM*5)
    pause 10
     SEROUT2 LCD,84, [Prefix,CursorPS,0,"RPM ",dec5 RPM, " ", #Period]
     T1Con.0=0
     
     Gosub ClearTimer1
     
     Goto Loop
     
     ClearTimer1:
     If Capture = 0 then
        goto ClearTimer1
     endif
     
     TMR1H = 00
     TMR1L = 0
     Capture = 0
     Overflow = 0 
     return
    Code:
    Taylors ET Code
    '****************************************************************
    '*  Name    : ELAPSED.PBP                                       *
    '*  Author  : Darrel Taylor                                     *
    '*  Notice  : Copyright (c) 2003                                *
    '*  Date    : 12/16/2003                                        *
    '*  Notes   :                                                   *
    '****************************************************************
    disable debug
    
    Define  INTHAND _ClockCount    ' Tell PBP Where the code starts on an interrupt
    Include "ASM_INTS-18.bas"         ' ASM Interrupt Stubs
    
    Ticks    var Word   ' 1/1000th of a second
    Seconds  var byte
    Minutes  var byte
    Hours    var byte
    Days     var word
    R0save   var word
    R1save   var word
    R4save   var word
    
    SecondsChanged   var bit
    MinutesChanged   var bit
    HoursChanged     var bit
    DaysChanged      var bit
    SecondsChanged = 1
    MinutesChanged = 1
    
    Goto OverElapsed
    
    ' ------------------------------------------------------------------------------
    Asm
      IF OSC == 4                       ; Constants for 100hz interrupt from Timer1
    TimerConst = 0D8F7h                 ; Executed at compile time only
      EndIF
      If OSC == 8
    TimerConst = 0B1E7h
      EndIF
      If OSC == 10
    TimerConst = 09E5Fh
      EndIF
      If OSC == 20
    TimerConst = 0EC80h
      EndIF
      If OSC == 40
    TimerConst = 03CB7h
      EndIF
    
    ; -----------------  ADD TimerConst to TMR1H:TMR1L
    ADD2_TIMER   macro
        CHK?RP  T3CON
        BCF     T3CON,TMR3ON           ; Turn off timer
        MOVLW   LOW(TimerConst)        ;  1
        ADDWF   TMR3L,F                ;  1    ; reload timer with correct value
        BTFSC   STATUS,C               ;  1/2
        INCF    TMR3H,F                ;  1
        MOVLW   HIGH(TimerConst)       ;  1
        ADDWF   TMR3H,F                ;  1
        endm
    
    ; -----------------  ADD TimerConst to TMR1H:TMR1L and restart TIMER1
    RELOAD_TIMER  macro
        ADD2_TIMER
        BSF     T3CON,TMR3ON           ;  1    ; Turn TIMER1 back on
        CHK?RP  PIR2
        bcf     PIR2, TMR3IF           ; Clear Timer1 Interrupt Flag
        endm
    
    ; -----------------  Load TimerConst into TMR1H:TMR1L
    LOAD_TIMER  macro
    EndAsm
        T3CON.0 = 0                    ; Turn OFF Timer1
        TMR3L = 0
        TMR3H = 0
    Asm
        ADD2_TIMER
        endm
    EndAsm
    
    ' ------[ This is the Interrupt Handler ]---------------------------------------
    ClockCount:   ' Note: this is being handled as an ASM interrupt
    @ INT_START
    @ RELOAD_TIMER                    ; Reload TIMER1
      R0save = R0                     ; Save 2 PBP system vars that are used during
      R1save = R1                     ; the interrupt
      R4save = R4
        Ticks = Ticks + 1
        if Ticks = 1000 then
           Ticks = Ticks-1000
           Seconds = Seconds + 1
           SecondsChanged = 1
           if Seconds = 60 then
              Minutes = Minutes + 1
              MinutesChanged = 1
              Seconds = 0
           endif
           if Minutes = 60 then
              Hours = Hours + 1
              HoursChanged = 1
              Minutes = 0
           endif
           if Hours = 24 then
              Days = Days + 1
              DaysChanged = 1
              Hours = 0
           endif
        endif
      R4 = R4save                     ; Restore the PBP system vars
      R1 = R1save
      R0 = R0save
    @ INT_RETURN                      ; Restore context and return from interrupt
    
    '-----====[ END OF TMR1 Interrupt Handler ]====---------------------------------
    
    StartTimer:
        T3CON.1 = 0                   ; (TMR1CS) Select FOSC/4 Clock Source
        T3CON.3 = 0                   ; (T1OSCEN) Disable External Oscillator
        PIR2.0  = 0                   ; (TMR1IF) Clear Timer1 Interrupt Flag
        PIE2.0  = 1                   ; (TMR1IE) Enable TMR1 overflow interrupt
        INTCON.6 = 1                  ; (PEIE) Enable peripheral interrupts
        INTCON.7 = 1                  ; (GIE) Enable global interrupts
        T3CON.0 = 1                   ; (TMR1ON) Start TIMER1
    @ if OSC == 40
        T3CON.5 = 0                   ; Prescaler = 1:2
        T3CON.4 = 1
    @ endif
    return
    
    ; -----------------
    StopTimer:
        T3CON.0 = 0                   ; Turn OFF Timer1
    return
    
    ; -----------------
    ResetTime:
        R0save = T3CON.0              ; Save TMR1ON bit
        T3CON.0 = 0                   ; Turn OFF Timer1
        TMR3L = 0
        TMR3H = 0
    @   LOAD_TIMER                    ; Load TimerConst
        T3CON.0 = R0save              ; Restore TMR1ON bit
        Ticks = 0
        Seconds = 0
        Minutes = 0
        Hours = 0
        Days = 0
        SecondsChanged = 1
    return
    
    OverElapsed:
    enable debug

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


    Did you find this post helpful? Yes | No

    Default

    Looks like you forgot to write to CCP1CON to turn on capture.
    Regards,

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

  3. #3


    Did you find this post helpful? Yes | No

    Default rpm is getting really close

    Thanks for your help and also Taylors email. I inserted CCP1CON flags and also had a couple others wrong. I am learning alot about how to read the datasheets changing chips.

    I am using an external 20 MHZ OSC

    The code below works except it isn't as close as when I was using the 877A chip. I have an rpm frequency generator with a digital readout that I am using to check the rpm with the 4620 chip. Following is data

    RPM with generator/RPM with 4620
    4000/3975
    5000/4965
    6000/5960
    7000/6955
    8000/7950
    9000/8945
    10000/9935

    All chip numbers are 99.35% appr from RPM generator. When I used the 877A all numbers matched up dead on.

    Any ideas?

    Code:
    DEFINE OSC 20
    Prefix          con     $FE           ' needed before each command
    LcdCls          CON     $51           ' clear LCD (use PAUSE 5 after)
    CursorPS        con     $45           'Cursor Position
    Capture         VAR     PIR1.2		    ' CCP1 capture flag CCP1IF
    Overflow        VAR     PIR1.0	    	' Timer1 overflow flag TMR1IF:
    RPM             var     word
    period          var     Word
    TotalTime       var     word      'Holds seconds and tics 
    
    LCD             VAR     PortC.6    'LCD output
    Gate1           Var     PortC.1
    Gate2           var     PortD.1
    Gate3           var     PortD.2
    Gate4           var     PortD.3
    Gate5           var     PortD.6
    Gate6           var     PortD.7
    Gate7           var     PortD.5
    Gate8           var     PortD.4
    WOT             var     PortB.0
     
    ADCON0 =0       'A/D Off
    ADCON1.3=1      'All AN channels digital
    ADCON1.2=1      '''
    ADCON1.1=1      '''
    ADCON1.0=1      '''
    TRISE.0=1       'EO input
    TRISE.1=1       'E1 input
    CCP1CON = %00000110  ; Capture mode, every 4th rising edge 0110 
    T1CON.7=1     'enable timer  16 bit `   
    T1CON.6=1     'Timer1 OSC
    T1CON.5=1     '1:4 prescaler
    T1CON.4=0     '1:4 prescaler
    T1CON.3=0     'Timer1 OSC off
    T1CON.2=0     'sychro clock
    T1CON.1=0     'internal clock
    T1CON.0=0     'stop timer
    
    pause 10
    SEROUT2 LCD,84, [Prefix, LcdCls]
    
    Include "modedefs.bas"	' Mode definitions for Serout
    SEROUT2 LCD,84, [Prefix,CursorPS,0,"RPM test"]
    pause 500
    
    Loop:
    TMR1H = 0    'Clear 8-bit register                              
    TMR1L = 0    'Clear 8-bit register
    capture = 0  'Clear Timer
    'SEROUT2 LCD,84, [Prefix,CursorPS,0,"Capture ", dec Capture," ",DEC Period]
    Start:
          If Capture = 0 then
             Goto Start
          endif
    
          T1CON.0=1
          Capture = 0 
          
    CaptureLoop:
      If Capture = 0 then 
        'SEROUT2 LCD,84, [Prefix,CursorPS,0,"Dont Capture ", dec Capture," ",DEC Period]
        Goto CaptureLoop
      endif
    T1Con.0=0  
    period.LowByte=CCPR1L
    period.HighByte=CCPR1H
    
    RPM = 10000
    RPM = RPM * RPM ' 100,000,000
    RPM = DIV32 period ' 100,000,000 / RevCount
    RPM = RPM * 60 ' Per minute
    RPM = DIV32 400
    RPM = (RPM*5)
    
    SEROUT2 LCD,84, [Prefix,CursorPS,20,"RPM ",dec5 RPM, " ", DEC Period]
      
    Gosub ClearTimer1
     
    Goto Loop
     
    ClearTimer1:
    If Capture = 0 then
      'SEROUT2 LCD,84, [Prefix,CursorPS,40,"Don't Clear Timer ", DEC Period]
      goto ClearTimer1
    endif
    ''SEROUT2 LCD,84, [Prefix,CursorPS,40,"Clear Timer ", DEC Period]
    TMR1H = 00
    TMR1L = 0
    Capture = 0
    Overflow = 0 
    return

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


    Did you find this post helpful? Yes | No

    Default

    If you're using exactly the same setting for capture, timer1, etc, and it works 100%
    on the 877A, but not on the 4620, then I would suspect a bug in the 4620, or something
    has been changed somewhere between the 2 versions.

    What values do you get if sending the raw value from period to your display immediately
    after this?

    period.LowByte=CCPR1L
    period.HighByte=CCPR1H
    Last edited by Bruce; - 28th January 2010 at 01:04.
    Regards,

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

  5. #5


    Did you find this post helpful? Yes | No

    Default 877A to 4620 data

    Here are the numbers from an 877A and 4620 both configured to use HS 20 MHZ.

    I will say I am not too sure with the T1Con flags if its set exactly as the 877A

    Code:
    RPM			877A	4620
    5000	Low Byte	202	243
            High Byte	175	58
    6000	Low Byte	125	31
            High Byte	146	497
    7000	Low Byte	141	26
            High Byte	167	421
    8000	Low Byte	125	215
            High Byte	146	36
    Code:
    '877A Code
    DEFINE OSC 20
    Prefix          con      $FE             ' needed before each command
    LcdCls          CON      $51             ' clear LCD (use PAUSE 5 after)
    CursorPS        con      $45             'Cursor Position
    Capture         VAR     PIR1.2		      ' CCP1 capture flag
    Overflow        VAR     PIR1.0	    	' Timer1 overflow flag
    RPM             var     word
    period          var     Word
    TotalTime       var     word      'Holds seconds and tics 
    
    LCD             VAR     PortC.6    'LCD output
    Gate1           Var     PortC.1
    Gate2           var     PortD.1
    Gate3           var     PortD.2
    Gate4           var     PortD.3
    Gate5           var     PortD.6
    Gate6           var     PortD.7
    Gate7           var     PortD.5
    Gate8           var     PortD.4
    WOT             var     PortB.0
     
    ADCON0 =0
    ADCON1.3=1
    ADCON1.2=1
    ADCON1.1=1
    ADCON1.0=1
    TRISE.0=1
    TRISE.1=1
    CCP1CON = %00000110   			' Enable the CCP1 capture, every 4th rising edge
    
    pause 100
    SEROUT2 LCD,84, [Prefix, LcdCls]
    low Gate1
    Low Gate2
    Low Gate3  
    Low Gate4 
    Low Gate5 
    Low Gate6
    Low Gate7  
    Low Gate8 
    
    Include "modedefs.bas"	' Mode definitions for Serout
    T1CON=%00100000
    
    Loop:
    TMR1H  = 0                              
    TMR1L  = 0
    capture = 0
    
    Start:
    	IF capture = 0 Then  
        goto Start	' Wait here for the first capture
      endif
    
     T1CON.0 = 1             ' Start the Timer
     capture = 0             ' Reset  the capture flag
     
    CaptureLoop:
    IF capture = 0 Then 
      goto CaptureLoop	' Wait here until captured
    endif
      
    period.lowbyte = CCPR1L		' Store the captured value in
    period.highbyte = CCPR1H	' period variable
    
    RPM = 10000
    RPM = RPM * RPM ' 100,000,000
    RPM = DIV32 period ' 100,000,000 / RevCount
    RPM = RPM * 60 ' Per minute
    RPM = DIV32 400 '400
    RPM = (RPM*5)
    pause 10
    SEROUT2 LCD,84, [Prefix,CursorPS,20,"RPM ",dec5 RPM, " ", DEC CCPR1L, " ", dec CCPR1H]
    T1CON.0=0
    
    gosub cleartimer1    
    
    GoTo loop					' Do it forever
        	
    ClearTimer1:
    IF (capture = 0) Then 
      goto cleartimer1	' Wait for beginning of next period
    endif
    
    TMR1L = 0					    ' Clear Timer1 low register
    TMR1H = 0					    ' Clear Timer1 high register
    capture = 0					    ' Clear capture flag
    overflow = 0				    ' Clear overflow flagReturn
    return
    Code:
    '4620
    DEFINE OSC 20
    Prefix          con     $FE           ' needed before each command
    LcdCls          CON     $51           ' clear LCD (use PAUSE 5 after)
    CursorPS        con     $45           'Cursor Position
    Capture         VAR     PIR1.2		    ' CCP1 capture flag CCP1IF
    Overflow        VAR     PIR1.0	    	' Timer1 overflow flag TMR1IF:
    RPM             var     word
    period          var     Word
    TotalTime       var     word      'Holds seconds and tics 
    
    LCD             VAR     PortC.6    'LCD output
    Gate1           Var     PortC.1
    Gate2           var     PortD.1
    Gate3           var     PortD.2
    Gate4           var     PortD.3
    Gate5           var     PortD.6
    Gate6           var     PortD.7
    Gate7           var     PortD.5
    Gate8           var     PortD.4
    WOT             var     PortB.0
     
    ADCON0 =0       'A/D Off
    ADCON1.3=1      'All AN channels digital
    ADCON1.2=1      '''
    ADCON1.1=1      '''
    ADCON1.0=1      '''
    TRISE.0=1       'EO input
    TRISE.1=1       'E1 input
    CCP1CON = %00000110  ; Capture mode, every 4th rising edge 0110 
    T1CON.7=1     'enable timer  16 bit `   
    T1CON.6=1     'Timer1 OSC
    T1CON.5=1     '1:4 prescaler
    T1CON.4=0     '1:4 prescaler
    T1CON.3=0     'Timer1 OSC off
    T1CON.2=0     'sychro clock
    T1CON.1=0     'internal clock
    T1CON.0=0     'stop timer
    
    pause 10
    SEROUT2 LCD,84, [Prefix, LcdCls]
    
    Include "modedefs.bas"	' Mode definitions for Serout
    SEROUT2 LCD,84, [Prefix,CursorPS,0,"RPM test"]
    pause 500
    
    Loop:
    TMR1H = 0    'Clear 8-bit register                              
    TMR1L = 0    'Clear 8-bit register
    capture = 0  'Clear Timer
    Start:
          If Capture = 0 then
             Goto Start
          endif
    
          T1CON.0=1
          Capture = 0 
          
    CaptureLoop:
    If Capture = 0 then 
      Goto CaptureLoop
    endif
    T1Con.0=0  
    period.LowByte=CCPR1L
    period.HighByte=CCPR1H
    
    RPM = 10000
    RPM = RPM * RPM ' 100,000,000
    RPM = DIV32 period ' 100,000,000 / RevCount
    RPM = RPM * 60 ' Per minute
    RPM = DIV32 400
    RPM = (RPM*5)
    
    SEROUT2 LCD,84, [Prefix,CursorPS,20,"RPM ",dec5 RPM, " ", DEC CCPR1L, " ", dec CCPR1H]
      
    Gosub ClearTimer1
     
    Goto Loop
     
    ClearTimer1:
    If Capture = 0 then
      goto ClearTimer1
    endif
    
    TMR1H = 00
    TMR1L = 0
    Capture = 0
    Overflow = 0 
    return

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


    Did you find this post helpful? Yes | No

    Default

    Are your result figures correct? It shows the exact same result on the 877A for 6000
    and 8000 RPM!

    What do you get with something like this on both versions?
    Code:
    period.lowbyte = CCPR1L		' Store the captured value in
    period.highbyte = CCPR1H	' period variable
    
    SEROUT2 LCD,84, [Prefix,CursorPS,20,dec5 period]
    Regards,

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

  7. #7


    Did you find this post helpful? Yes | No

    Default Period info

    RPM and period numbers per chip
    Rpm 4620 877a
    5000 15090 14999
    6000 12575 12499
    7000 10777 10712
    8000 9430 9373
    9000 8383 8331
    10000 7545 7499

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


    Did you find this post helpful? Yes | No

    Default

    Do you have another 18F type besides a 4620 you can test this on?

    That would tell you if it's a hardware bug on the 4620. This part has
    a massive list of errata & odd problems with ECCP, Timer1, Timer3,
    and more.
    Regards,

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

  9. #9


    Did you find this post helpful? Yes | No

    Default

    I might have a 4520 floating around. Can you suggest a part number that will work with the boards I already have made? I will order it today if you have a suggestion.

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


    Did you find this post helpful? Yes | No

    Default

    A 4520 should drop right in, but this one also has a boat-load of errata for CCP, timer, and
    a bunch more. I would test it if you already have one before buying another PIC.

    The 18F4431 is my own personal favorite, and I also like the older 18F452. The drop-in
    replacement (your 4520) for the 452 has way too many bugs.
    Regards,

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

  11. #11


    Did you find this post helpful? Yes | No

    Default

    Thanks it looks like a couple different config flags need to be changed to go with the 4431. The pinout appears to be the same.?.

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


    Did you find this post helpful? Yes | No

    Default

    It's the same pinout. It also has a lot of extra features.

    But - I would try that 4520 if you can find it.

    Where are you getting your PICs from? I have samples, and would be
    more than willing to shoot you an 18F4431 if you cover postage.
    Last edited by Bruce; - 28th January 2010 at 22:08.
    Regards,

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

  13. #13


    Did you find this post helpful? Yes | No

    Default

    I usually go through Mouser but I would rather spend money with you. Do you want to Paypal the freight? My paypal acct is [email protected].

    I need a 44 pin TQFP

    Shipping is
    xBase
    506 Park St
    Sterling CO 80751

    Hell with your 719 area code you can't be far anyway.
    Thanks
    Toby

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


    Did you find this post helpful? Yes | No

    Default

    I need a 44 pin TQFP
    Yikes .. I only have DIP package types. Never get TQFP samples.

    But - I am in the process as we speak of putting in a big order for various PIC types, so
    I can grab one for you on this order, and get it out to you tomorrow. All at our exact cost
    on shipping & part.

    PIC18F4431-I/PT $6.60
    Priority Mail $4.95
    Regards,

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

  15. #15
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Tobias View Post
    RPM and period numbers per chip
    Rpm 4620 877a
    5000 15090 14999
    6000 12575 12499
    7000 10777 10712
    8000 9430 9373
    9000 8383 8331
    10000 7545 7499
    I think you need to clear the timer sooner. (Immediately after capture).

    The program goes through the whole conversion to RPM with multiplications and DIV32's before clearing the timer.

    The 18F's multiply faster because they have a hardware multiplier.
    So it clears the timer quicker and ends up with bigger numbers.

    The 4620 numbers are closer to reality, but it's still wrong.

    Clear the timer immediately after a capture and both chips should read the same numbers (or really close).

    hth,
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel,

    Code:
    Start:
          If Capture = 0 then ' ignores 1st capture
             Goto Start
          endif
    
          T1CON.0=1
          Capture = 0 
          
    CaptureLoop:
    If Capture = 0 then 
      Goto CaptureLoop
    endif
    T1Con.0=0  
    period.LowByte=CCPR1L
    period.HighByte=CCPR1H
    With both PICs running at 20MHz, CCPR1L & CCPR1H should have the same values - since
    it's all done in hardware with the capture module capturing the value of timer1 after the
    4 rising edges.

    And the value being captured is before the multiply.
    Regards,

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

  17. #17


    Did you find this post helpful? Yes | No

    Default

    Ship three please. How do you want me to pay?

    Quote Originally Posted by Bruce View Post
    Yikes .. I only have DIP package types. Never get TQFP samples.

    But - I am in the process as we speak of putting in a big order for various PIC types, so
    I can grab one for you on this order, and get it out to you tomorrow. All at our exact cost
    on shipping & part.

    PIC18F4431-I/PT $6.60
    Priority Mail $4.95

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


    Did you find this post helpful? Yes | No

    Default

    I've already ordered one, but let's hold-off on me shipping one until we hear back from
    Darrel. He might have noticed something I'm just not picking up on...;o)

    I haven't compiled both versions and looked at the timing difference from the instance
    you enable TMR1. That may make a difference.
    Last edited by Bruce; - 28th January 2010 at 22:56.
    Regards,

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

  19. #19
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    With both PICs running at 20MHz, CCPR1L & CCPR1H should have the same values - since it's all done in hardware with the capture module capturing the value of timer1 after the
    4 rising edges.
    Yes the captured value is stored in CCPR1L:H, but the CCP module does not clear Timer1. You have to do that. And it's not being cleared until after the math.

    Then it goes back up to Loop: and clears it again, without any reference to the incoming signal. (actually, it is referenced to the last capture plus math time).

    If you only clear it once, immediately after the capture, then it will be ready for the next capture.

    The first capture is always wrong, and must be discarded.
    Code:
    Loop:
    TMR1H = 0    'Clear 8-bit register                              
    TMR1L = 0    'Clear 8-bit register
    capture = 0  'Clear Timer
    Start:
          If Capture = 0 then
             Goto Start
          endif
    
          T1CON.0=1
          Capture = 0 
          
    CaptureLoop:
    If Capture = 0 then 
      Goto CaptureLoop
    endif
    T1Con.0=0  
    period.LowByte=CCPR1L
    period.HighByte=CCPR1H
    
    RPM = 10000
    RPM = RPM * RPM ' 100,000,000
    RPM = DIV32 period ' 100,000,000 / RevCount
    RPM = RPM * 60 ' Per minute
    RPM = DIV32 400
    RPM = (RPM*5)
    
    SEROUT2 LCD,84, [Prefix,CursorPS,20,"RPM ",dec5 RPM, " ", DEC CCPR1L, " ", dec CCPR1H]
      
    Gosub ClearTimer1
     
    Goto Loop
    Added: I guess if Timer1 was Stopped in the ClearTimer1 routine (or at Loop: ), what you have would work better, but it will only capture every other 4-pulses. (8-pulses total)
    Last edited by Darrel Taylor; - 28th January 2010 at 23:08. Reason: Added
    DT

  20. #20
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Oi vey,
    I just saw ...

    T1Con.0=0

    Ignore what I just said, and I'm off to proteus to make sure before I open my mouth again.

    DT

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


    Did you find this post helpful? Yes | No

    Default

    Cool. If I'm wrong, he gets a free PIC & you get the eBeer....

    Like I said before, there could indeed be a timing difference between these points;
    Code:
          T1CON.0=1
          Capture = 0
    That would for sure be the cause.
    Last edited by Bruce; - 29th January 2010 at 00:15.
    Regards,

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

  22. #22


    Did you find this post helpful? Yes | No

    Default Free beer appreciated

    Here is what I have now. I have set it up for every rising pulse and changed the math because when I incorporate this into my other code, waiting for four pulses delays alot of events.

    Its still off with the output RPM numbers by the same percentage.
    Code:
    DEFINE OSC 20
    Prefix          con     $FE           ' needed before each command
    LcdCls          CON     $51           ' clear LCD (use PAUSE 5 after)
    CursorPS        con     $45           'Cursor Position
    Capture         VAR     PIR1.2		    ' CCP1 capture flag CCP1IF
    Overflow        VAR     PIR1.0	    	' Timer1 overflow flag TMR1IF:
    RPM             var     word
    period          var     Word
    TotalTime       var     word      'Holds seconds and tics 
    
    LCD             VAR     PortC.6    'LCD output
    Gate1           Var     PortC.1
    Gate2           var     PortD.1
    Gate3           var     PortD.2
    Gate4           var     PortD.3
    Gate5           var     PortD.6
    Gate6           var     PortD.7
    Gate7           var     PortD.5
    Gate8           var     PortD.4
    WOT             var     PortB.0
     
    ADCON0 =0       'A/D Off
    ADCON1.3=1      'All AN channels digital
    ADCON1.2=1      '''
    ADCON1.1=1      '''
    ADCON1.0=1      '''
    TRISE.0=1       'EO input
    TRISE.1=1       'E1 input
    CCP1CON = %00000101  ; Capture mode, every rising edge 0101 
    T1CON.7=1     'enable timer  16 bit `   
    T1CON.6=1     'Timer1 OSC
    T1CON.5=1     '1:4 prescaler
    T1CON.4=0     '1:4 prescaler
    T1CON.3=0     'Timer1 OSC off
    T1CON.2=0     'sychro clock
    T1CON.1=0     'internal clock
    T1CON.0=0     'stop timer
    
    pause 10
    SEROUT2 LCD,84, [Prefix, LcdCls]
    
    Include "modedefs.bas"	' Mode definitions for Serout
    SEROUT2 LCD,84, [Prefix,CursorPS,0,"RPM test"]
    pause 500
    
    TMR1H = 0    'Clear 8-bit register                              
    TMR1L = 0    'Clear 8-bit register
    capture = 0  'Clear Timer
    
    Calc_RPM_Timer:
    TMR1H = 0    'Clear 8-bit register                              
    TMR1L = 0    'Clear 8-bit register
    capture = 0  'Clear Timer
    
    StartRPM:
      If Capture = 0 then
         Goto StartRPM
      endif
    
      T1CON.0=1
      Capture = 0 
          
    CaptureLoop:
      If Capture = 0 then 
        Goto CaptureLoop
      endif
    T1Con.0=0  
    period.LowByte=CCPR1L
    period.HighByte=CCPR1H
    
    TMR1H = 0
    TMR1L = 0
    Capture = 0
    Overflow = 0 
    
    RPM = 10000
    RPM = RPM * RPM ' 100,000,000
    RPM = DIV32 period ' 100,000,000 / RevCount
    RPM = RPM * 60 ' Per minute
    RPM = DIV32 1600
    RPM = (RPM*5)
    
    SEROUT2 LCD,84, [Prefix,CursorPS,20,"RPM ",dec5 RPM]
    goto Calc_RPM_Timer 
    return

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


    Did you find this post helpful? Yes | No

    Default

    I've already ordered you the 4431. Sorry I placed the order before I knew you needed
    or wanted 3. But I'm still holding the at cost offer open if you want this one even
    if/when Darrel proves me wrong - which he does way too often...;o)

    These really are some cool PICs'.

    If you don't mind my asking - what boards are you using? Surely you're not de-soldering
    and replacing TQFP package types!
    Regards,

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

  24. #24


    Did you find this post helpful? Yes | No

    Default

    Yea I will take a couple of the PICS no matter the outcome here. One way or another I want to get it working right. I need to use DT's elapsed time file and also interrupt rpm at the same time. So I can't go back to the 877A unless there is another approach.

    Desoldering the TQFP's is pretty easy. Not something I want to do often but when needed I have the technique down.

    Thanks to both of you on helping me get this sorted out too.

  25. #25
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Proteus Rules.

    Here's what I found.
    After the capture was made, and the math completed, the result is displayed with a SEROUT2 to the LCD.

    But at 9600 baud, by the time it gets around to displaying CCPR1L and CCPR1H, multiple captures have taken place without Timer1 being cleared.

    If you use this line instead, you get the same numbers on both chips and they are the right ones.

    Code:
    SEROUT2 LCD,84, [Prefix,CursorPS,20,"RPM ",dec5 RPM, " ", DEC5 period]
    <object id='stUkhdQ01IR19bR15YXVpZVFJV' width='425' height='344' type='application/x-shockwave-flash' data='http://www.screentoaster.com/swf/STPlayer.swf' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0'><param name='movie' value='http://www.screentoaster.com/swf/STPlayer.swf'/><param name='allowFullScreen' value='true'/><param name='allowScriptAccess' value='always'/><param name='flashvars' value='video=stUkhdQ01IR19bR15YXVpZVFJV'/></object><div style='width: 425px; text-align: right;'><a href='http://www.screentoaster.com/'>Capture your screen in seconds</a></div>

    My SEROUT2 was a little different ...
    SEROUT2 LCD,84, ["RPM ",dec5 RPM, " ", DEC CCPR1H<<8+CCPR1L," ",DEC period," Ovr:",BIN1 Overflow,13,10]

    The small pulse I measured was the time to do the math.
    The big pulse was the time for SEROUT2.
    The original SEROUT2 was shorter, and around 20mS.
    <br>
    Last edited by Darrel Taylor; - 29th January 2010 at 03:32. Reason: more more more
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Nope...

    Lets say we had ten thousand captures after this;
    Code:
    Loop:
    TMR1H = 0    'Clear 8-bit register                              
    TMR1L = 0    'Clear 8-bit register
    capture = 0  'Clear Timer
    'SEROUT2 LCD,84, [Prefix,CursorPS,0,"Capture ", dec Capture," ",DEC Period]
    Start:
          If Capture = 0 then
             Goto Start
          endif
    
          T1CON.0=1
          Capture = 0 
          
    CaptureLoop:
      If Capture = 0 then 
        'SEROUT2 LCD,84, [Prefix,CursorPS,0,"Dont Capture ", dec Capture," ",DEC Period]
        Goto CaptureLoop
      endif
    T1Con.0=0  
    period.LowByte=CCPR1L
    period.HighByte=CCPR1H
    They would all be zero since TMR1 was disabled after the T1CON.0=0.
    Once period = CCPR1L & CPR1H it's a done deal until the next capture,
    and Timer1 is re-enabled.

    Capture works wether or not Timer1 is enabled. It just returns 0 if it's off.

    Yet - I may be missing something...
    Last edited by Bruce; - 29th January 2010 at 04:20.
    Regards,

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

  27. #27
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Yup...

    But that's a very interesting point.

    In the 4620 program it does turn off the timer at that point.
    But the 877A program does not.

    And you can even see the difference in the simulation.
    The 4620 always has the same values for CCPR1L:H and period (+1?), but the 877A doesn't.

    4620
    Code:
          T1CON.0=1
          Capture = 0 
          
    CaptureLoop:
    If Capture = 0 then 
      Goto CaptureLoop
    endif
    T1Con.0=0  
    period.LowByte=CCPR1L
    period.HighByte=CCPR1H
    
    RPM = 10000
    877A
    Code:
     T1CON.0 = 1             ' Start the Timer
     capture = 0             ' Reset  the capture flag
     
    CaptureLoop:
    IF capture = 0 Then 
      goto CaptureLoop	' Wait here until captured
    endif
    ; nada  
    period.lowbyte = CCPR1L		' Store the captured value in
    period.highbyte = CCPR1H	' period variable
    
    RPM = 10000
    And when a Timer is turned off, it retains the last value in the TMR?L:H registers.
    <br>
    DT

  28. #28


    Did you find this post helpful? Yes | No

    Default

    With all that in mind, what do I need to change from the last code I posted to get RPM correct?

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


    Did you find this post helpful? Yes | No

    Default

    Hey Darrel,

    Disabling TMR1 after the event has zero affect on the value that CCPR1l & CCPR1H record at the time of the event. The value in Timer1 is captured immediatley on event.

    Can you recommend something for the OP that wiil fix it?

    If you can -- he gets the free PIC, and I'll send you a six-pack of your favorite BEER...

    FYI: Disabling the timer after the capture event has zero effect on the value captured by the hardware catpure module.

    Remember ... It's done in hardware
    Last edited by Bruce; - 29th January 2010 at 05:06. Reason: FYI
    Regards,

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

  30. #30


    Did you find this post helpful? Yes | No

    Default

    Bruce
    Ship a couple of the chips tomorrow. I will pay for them. I need to get this calculating correctly soon. If you mail tomorrow I bet I have it saturday.

  31. #31
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    Disabling TMR1 after the event has zero affect on the value that CCPR1l & CCPR1H record at the time of the event. The value in Timer1 is captured immediatley on event.
    Absolutely true!
    But the numbers given originally were from multiple captures without the timer being turned off. So they were irrelevant and confusing.

    Can you recommend something for the OP that will fix it?

    If you can -- he gets the free PIC, and I'll send you a six-pack of your favorite BEER...
    I can ... soon. Just don't want to have to say Oi Vey again.

    Hell, ship him the chips, and I'll pay for them if I fail.
    But long before he gets them, the problem will be solved, and I will drink your beer.
    <br>
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Hey Toby,

    I'm real sorry I didn't order three of these things for you, but if you refer back to post
    #23 -- you'll see what happened.
    Regards,

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

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


    Did you find this post helpful? Yes | No

    Default

    But long before he gets them, the problem will be solved, and I will drink your beer.
    Cool, but you have to prove me wrong to get that BEER...

    What's your favorite suds?

    Just want to be prepared for when you DO....
    Regards,

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

  34. #34


    Did you find this post helpful? Yes | No

    Default

    One will be fine too, will it ship tomorrow? Not that I don't have faith in DT

  35. #35
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    Tobias,

    Not that I think there is anything wrong with the way you are doing things, but - I earlier posted a routine that can read virtually any number of tachometers to good accuracy, and to RPMs far past 15000 (if your tach output is the standard 2 pulse/rev) type. It averages over a period, so jitter
    isn't a problem.

    It is interrupt-driven, and runs totally in the background.
    Charles Linquist

  36. #36


    Did you find this post helpful? Yes | No

    Default

    Did you ever test it on an 18F4620? I found it. I am sure I will have a few questions in the morning.
    Last edited by Tobias; - 29th January 2010 at 05:54.

  37. #37
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Charles Linquis View Post
    I earlier posted a routine that can read virtually any number of tachometers to good accuracy, and to RPMs far past 15000 (if your tach output is the standard 2 pulse/rev) type.
    BEER thief
    <br>
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Yep. My order was shipped UPS RED, and should be here tomorrow, so I'll be shooting
    your 18F4431 TQFP out by standard Priority Mail. You should be receiving it pretty fast
    with you being just down-the-road.

    Not that I don't have faith in DT
    You should .. he's a real genius at this stuff...

    BTW: Your total for the 18F4431 TQFP + shipping is $11.55, which is exactly our cost for
    the part + shipping. Just make a Paypal payment in this amount to [email protected]

    And, if you need any help using this thing - just ask. I've done a boat-load of commercial
    apps with these.
    Regards,

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

  39. #39


    Did you find this post helpful? Yes | No

    Default

    Thanks check your PayPal acct

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


    Did you find this post helpful? Yes | No

    Default

    BEER thief
    That is just so WRONG ... hit squad has been mobilized....
    Regards,

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

Members who have read this thread : 2

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