PIC 18F4620 RPM and Elapsed Time interrupts

Discussion in 'Embedded Systems and Microcontrollers' started by Tobias, Jan 27, 2010.

  1. Tobias

    Thread Starter Active Member

    May 19, 2008
    158
    0
    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 found some code using Timer1 to do elapesed time. I needed to modify the 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 ( (Unknown Language)):
    1.  
    2. RPM Code
    3.  
    4. DEFINE OSC 20
    5. Prefix          con      $FE             ' needed before each command
    6. LcdCls          CON      $51             ' clear LCD (use PAUSE 5 after)
    7. CursorPS        con      $45             'Cursor Position
    8. Capture         VAR     PIR2.0            ' CCP1 capture flag
    9. Overflow        VAR     PIR2.1          ' Timer1 overflow flag
    10. RPM             var     word
    11. period          var     Word
    12. TotalTime       var     word      'Holds seconds and tics
    13.  
    14. LCD             VAR     PortC.6    'LCD output
    15. Gate1           Var     PortC.1
    16. Gate2           var     PortD.1
    17. Gate3           var     PortD.2
    18. Gate4           var     PortD.3
    19. Gate5           var     PortD.6
    20. Gate6           var     PortD.7
    21. Gate7           var     PortD.5
    22. Gate8           var     PortD.4
    23. WOT             var     PortB.0
    24.  
    25. ADCON0 =0
    26. ADCON1.3=1
    27. ADCON1.2=1
    28. ADCON1.1=1
    29. ADCON1.0=1
    30. TRISE.0=1
    31. TRISE.1=1
    32.  
    33. T1CON.7=1     'enable timer  16 bit `  
    34. T1CON.6=1     'Timer1 OSC
    35. T1CON.5=1     '1:4 prescaler
    36. T1CON.4=0     '1:4 prescaler
    37. T1CON.3=1     'Timer1 OSC enabled
    38. T1CON.2=0     'sychro clock
    39. T1CON.1=1     'external clock
    40. T1CON.0=0     'stop timer
    41.  
    42. pause 100
    43. SEROUT2 LCD,84, [Prefix, LcdCls]
    44.  
    45. Include "modedefs.bas"  ' Mode definitions for Serout
    46.  
    47. Loop:
    48. TMR1H = 0                              
    49. TMR1L = 0
    50. capture = 0
    51.  
    52. Start:
    53.       If Capture = 0 then
    54.          Goto Start
    55.       endif
    56.       'code doesn't make it this far..assuming waiting for Cpature =1
    57.       T1CON.0=1
    58.       Capture = 0
    59.      
    60. CaptureLoop:
    61.   If Capture = 0 then
    62.     Goto CaptureLoop
    63.   endif
    64.  
    65. period.LowByte=CCPR1L
    66. period.HighByte=CCPR1H
    67.  
    68. RPM = 10000
    69. RPM = RPM * RPM ' 100,000,000
    70. RPM = DIV32 period ' 100,000,000 / RevCount
    71. RPM = RPM * 60 ' Per minute
    72. RPM = DIV32 398 '400
    73. RPM = (RPM*5)
    74. pause 10
    75.  SEROUT2 LCD,84, [Prefix,CursorPS,0,"RPM ",dec5 RPM, " ", #Period]
    76.  T1Con.0=0
    77.  
    78.  Gosub ClearTimer1
    79.  
    80.  Goto Loop
    81.  
    82.  ClearTimer1:
    83.  If Capture = 0 then
    84.     goto ClearTimer1
    85.  endif
    86.  
    87.  TMR1H = 00
    88.  TMR1L = 0
    89.  Capture = 0
    90.  Overflow = 0
    91.  return
    Code ( (Unknown Language)):
    1. ET Code
    2. disable debug
    3.  
    4. Define  INTHAND _ClockCount    ' Tell PBP Where the code starts on an interrupt
    5. Include "ASM_INTS-18.bas"         ' ASM Interrupt Stubs
    6.  
    7. Ticks    var Word   ' 1/1000th of a second
    8. Seconds  var byte
    9. Minutes  var byte
    10. Hours    var byte
    11. Days     var word
    12. R0save   var word
    13. R1save   var word
    14. R4save   var word
    15.  
    16. SecondsChanged   var bit
    17. MinutesChanged   var bit
    18. HoursChanged     var bit
    19. DaysChanged      var bit
    20. SecondsChanged = 1
    21. MinutesChanged = 1
    22.  
    23. Goto OverElapsed
    24.  
    25. ' ------------------------------------------------------------------------------
    26. Asm
    27.   IF OSC == 4                       ; Constants for 100hz interrupt from Timer1
    28. TimerConst = 0D8F7h                 ; Executed at compile time only
    29.   EndIF
    30.   If OSC == 8
    31. TimerConst = 0B1E7h
    32.   EndIF
    33.   If OSC == 10
    34. TimerConst = 09E5Fh
    35.   EndIF
    36.   If OSC == 20
    37. TimerConst = 0EC80h
    38.   EndIF
    39.   If OSC == 40
    40. TimerConst = 03CB7h
    41.   EndIF
    42.  
    43. ; -----------------  ADD TimerConst to TMR1H:TMR1L
    44. ADD2_TIMER   macro
    45.     CHK?RP  T3CON
    46.     BCF     T3CON,TMR3ON           ; Turn off timer
    47.     MOVLW   LOW(TimerConst)        ;  1
    48.     ADDWF   TMR3L,F                ;  1    ; reload timer with correct value
    49.     BTFSC   STATUS,C               ;  1/2
    50.     INCF    TMR3H,F                ;  1
    51.     MOVLW   HIGH(TimerConst)       ;  1
    52.     ADDWF   TMR3H,F                ;  1
    53.     endm
    54.  
    55. ; -----------------  ADD TimerConst to TMR1H:TMR1L and restart TIMER1
    56. RELOAD_TIMER  macro
    57.     ADD2_TIMER
    58.     BSF     T3CON,TMR3ON           ;  1    ; Turn TIMER1 back on
    59.     CHK?RP  PIR2
    60.     bcf     PIR2, TMR3IF           ; Clear Timer1 Interrupt Flag
    61.     endm
    62.  
    63. ; -----------------  Load TimerConst into TMR1H:TMR1L
    64. LOAD_TIMER  macro
    65. EndAsm
    66.     T3CON.0 = 0                    ; Turn OFF Timer1
    67.     TMR3L = 0
    68.     TMR3H = 0
    69. Asm
    70.     ADD2_TIMER
    71.     endm
    72. EndAsm
    73.  
    74. ' ------[ This is the Interrupt Handler ]---------------------------------------
    75. ClockCount:   ' Note: this is being handled as an ASM interrupt
    76. @ INT_START
    77. @ RELOAD_TIMER                    ; Reload TIMER1
    78.   R0save = R0                     ; Save 2 PBP system vars that are used during
    79.   R1save = R1                     ; the interrupt
    80.   R4save = R4
    81.     Ticks = Ticks + 1
    82.     if Ticks = 1000 then
    83.        Ticks = Ticks-1000
    84.        Seconds = Seconds + 1
    85.        SecondsChanged = 1
    86.        if Seconds = 60 then
    87.           Minutes = Minutes + 1
    88.           MinutesChanged = 1
    89.           Seconds = 0
    90.        endif
    91.        if Minutes = 60 then
    92.           Hours = Hours + 1
    93.           HoursChanged = 1
    94.           Minutes = 0
    95.        endif
    96.        if Hours = 24 then
    97.           Days = Days + 1
    98.           DaysChanged = 1
    99.           Hours = 0
    100.        endif
    101.     endif
    102.   R4 = R4save                     ; Restore the PBP system vars
    103.   R1 = R1save
    104.   R0 = R0save
    105. @ INT_RETURN                      ; Restore context and return from interrupt
    106.  
    107. '-----====[ END OF TMR1 Interrupt Handler ]====---------------------------------
    108.  
    109. StartTimer:
    110.     T3CON.1 = 0                   ; (TMR1CS) Select FOSC/4 Clock Source
    111.     T3CON.3 = 0                   ; (T1OSCEN) Disable External Oscillator
    112.     PIR2.0  = 0                   ; (TMR1IF) Clear Timer1 Interrupt Flag
    113.     PIE2.0  = 1                   ; (TMR1IE) Enable TMR1 overflow interrupt
    114.     INTCON.6 = 1                  ; (PEIE) Enable peripheral interrupts
    115.     INTCON.7 = 1                  ; (GIE) Enable global interrupts
    116.     T3CON.0 = 1                   ; (TMR1ON) Start TIMER1
    117. @ if OSC == 40
    118.     T3CON.5 = 0                   ; Prescaler = 1:2
    119.     T3CON.4 = 1
    120. @ endif
    121. return
    122.  
    123. ; -----------------
    124. StopTimer:
    125.     T3CON.0 = 0                   ; Turn OFF Timer1
    126. return
    127.  
    128. ; -----------------
    129. ResetTime:
    130.     R0save = T3CON.0              ; Save TMR1ON bit
    131.     T3CON.0 = 0                   ; Turn OFF Timer1
    132.     TMR3L = 0
    133.     TMR3H = 0
    134. @   LOAD_TIMER                    ; Load TimerConst
    135.     T3CON.0 = R0save              ; Restore TMR1ON bit
    136.     Ticks = 0
    137.     Seconds = 0
    138.     Minutes = 0
    139.     Hours = 0
    140.     Days = 0
    141.     SecondsChanged = 1
    142. return
    143.  
    144. OverElapsed:
    145. enable debug
     
Loading...