Interrupt RPM and Taylors Elapsed time on 18F4620


Closed Thread
Results 1 to 40 of 71

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default

    Here is the elasped timer code I modified to use T3 and go to the thousandths. Also I have the interrupt set up as INTLHAND

    Code:
    '****************************************************************
    '*  Name    : ELAPSED.PBP                                       *
    '*  Author  : Darrel Taylor                                     *
    '*  Notice  : Copyright (c) 2003                                *
    '*  Date    : 12/16/2003                                        *
    '*  Notes   :                                                   *
    '****************************************************************
    disable debug
    
    Define  INTLHAND _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.1  = 0                   ; (TMR1IF) Clear Timer1 Interrupt Flag
        PIE2.1  = 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


    Did you find this post helpful? Yes | No

    Default

    This appears to be working correctly. Are there any problems going this route?
    Of course this isn't all the code, just the part that is relevant.
    Code:
    DEFINE INTHAND Direction 
    
    ASM
    Direction
    ENDASM
      If PIR1.2 = 1 then
        goto RPMASM
      endif
      
      If PIR2.1 = 1 then
        goto ClockCount
      endif
    return  
    
    RPMASM: 
    ASM
    CCP_INT
      BTFSS CCP1CON,0      ; capture from rising edge?
      BRA Fall             ; no .. goto falling edge
      MOVFF CCPR1L, TS    ; get low capture byte into T1
      MOVFF CCPR1H, TS+1   ; get high capture byte into T1
      BRA IntExit          ; outta here
    Fall
      MOVFF CCPR1L, PW     ; get low capture byte into PW
      MOVFF CCPR1H, PW+1   ; get high capture byte into PW
      BSF CF,0             ; indicate last capture
    IntExit
      BTG CCP1CON,0        ; toggle between rising/falling edge captures
      BCF PIR1,2           ; clear capture interrupt flag bit
      RETFIE FAST          ; return/restore W, STATUS and BSR
    ENDASM
    
    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

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


    Did you find this post helpful? Yes | No

    Default

    You don't need the bit tests in Direction since you have CCP_INT (or Direction) set as a high-priority interrupt, and you have ClockCount set as a low-priority interrupt.

    Direction will be jumped to automatically when the CCP interrupt fires. ClockCount will be jumped to automatically when the TMR3 interrupt fires. Note that your low-priority interrupt can be interrupted by the high-priority one.
    Regards,

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

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