Totally Baffled with Elapsed Timer


Closed Thread
Results 1 to 6 of 6
  1. #1

    Default Totally Baffled with Elapsed Timer

    Ok here's what I've come across and for the life of me I can't see what the problem is. I'm trying to build a timer for cycle testing things. What's happening is when I run the code with the red lines remarked out it works fine. If the lines are included the timer never seems to activate the relay. Well it does but so quick the relay can't even response fast enough. Any ideas?

    Code:
    '************************ 16F628A Configuration Fuses **************************
    
    @ __CONFIG _BOREN_ON & _CP_OFF & _DATA_CP_OFF & _PWRTE_ON & _WDT_ON & _LVP_OFF & _MCLRE_OFF & _INTRC_OSC_NOCLKOUT
    ' Brown-Out Detect - _BODEN_OFF, _BODEN_ON, _BOREN_OFF, _BOREN_ON
    ' Code Protection - _CP_OFF, CP_ON
    ' Data Code Protect - _DATA_CP_OFF, DATA_CP_ON
    ' Power-On Timer - _PWRTE_OFF, _PWRTE_ON
    ' Watchdog Timer - _WDT_OFF, _WDT_ON
    ' Low-Voltage Program - _LPV_OFF, _LVP_ON
    ' RA5/MCLR Function - _MCLRE_OFF, _MCLRE_ON
    ' Oscillator Selection - _RC_OSC_CLKOUT, _RC_OSC_NOCLKOUT, _ER_OSC_CLKOUT, 
    '                        _ER_OSC_NOCLKOUT, _INTOSC_OSC_CLKOUT, _INTOSC_OSC_NOCLKOUT, 
    '                        _INTRC_OSC_CLKOUT, _INTRC_OSC_NOCLKOUT, _EXTCLK_OSC, 
    '                        _HS_OSC, _XT_OSC, _LP_OSC
    '
    '*******************************************************************************
    
    '----------------------------  Port Assignments --------------------------------
    
    ' porta.0       Output 3 LED
    ' porta.1       Output 1 LED
    ' porta.2       Run LED
    ' porta.3       Output 2 LED
    ' porta.4       Unused
    ' porta.5       Unused
    ' porta.6       Unused
    ' porta.7       Unused
    ' portb.0       Input 3
    ' portb.1       Input 2
    ' portb.2       Input 1
    ' portb.3       HPWM Output
    ' portb.4       Up Button
    ' portb.5       Down Button
    ' portb.6       Program Button
    ' portb.7       Debug Output
    
    '----------------------------  EEPROM Locations --------------------------------
    
    ' 0     Output 1 Setting
    ' 1     Output 2 Setting
    ' 2     Output 3 Setting
    
    '----------------------------  Debug Settings ----------------------------------
    
        DEFINE  DEBUG_REG       PORTB   ' Debug port
        DEFINE  DEBUG_BIT       4       ' Debug port bit
        DEFINE  DEBUG_BAUD      9600    ' Debug baud rate
        DEFINE  DEBUG_MODE      1       ' Debug mode 1 = inverted
        DEFINE  DEBUG_PACING    300     ' Delay 'in micro sec' between characters sent
    
        i       con     254             ' Control byte
        clr     con     1               ' Clear the display
        line1   con     128             ' Move cursor to home position on line 1
        line2   con     192             ' Move cursor to home position on line 2
    
    '------------------------------- Variables -------------------------------------
    
        StillPressed        var     bit
        x                   var     byte
        Output1             var     word
        Output2             var     word
        Output3             var     word
        StartupDelay        var     byte
        TimeOut             var     byte
        
        Mode                var     byte        ' Timer mode -  0 = 1-Shot Off Start
                                                '               1 = 1-Shot On Start
                                                '               2 = Repeating Off Start
                                                '               3 = Repeating On Start
                                                
        OffTimeUnits        var     byte[4]
        OnTimeUnits         var     byte[4]
    
        OffTime             var     word        ' Off time dependant on units selected
        OnTime              var     word        ' On time dependant on units selected
        OffTimeUnitSelect   var     byte        ' Off time unit selector
        OnTimeUnitSelect    var     byte        ' On time unit selector
        SetOnTime           var     word        ' Programmed time to be on
        SetOffTime          var     word        ' Programmed time to be off
        RelayOutput         var     bit         ' Flag indicated relay status
            
    '------------------------ Miscellaneous Variables ------------------------------
    
    '                           porta.0
    '                           porta.1
    '                           porta.2
    '                           porta.3
    '                           porta.4
    '                           porta.5
    '                           portb.0
    '                           portb.1
        ProgramButton   var     portb.2
    '                           portb.3
    '                           portb.4
    '                           portb.5
    '                           portb.6
        Relay           var     portb.7
    
    '---------------------------  Port Initialization ------------------------------
    
        TRISA = %10110000
        TRISB = %11110111
        CMCON = %00000111           ' Disable comparator
        CCP1CON = %00000000         ' Enable PWM mode
        OPTION_REG.7 = 0            ' Enable portb pull-ups
        RCSTA.7 = 0                 ' Disable serial port
    
        DEFINE  OSC 4
    
    '----------------------------- Main Program Loop -------------------------------
    
        data 65,129,194
    
        include "Elapsed.pbp"
    
        gosub ResetTime                                                             ' Reset Time to  0d-00:00:00.00
        pause 250
        clear
        
        OffTimeUnitSelect = 0           ' 0=secs,1=mins,2=hrs,3=days,4=weeks
        OnTimeUnitSelect = 0            ' 0=secs,1=mins,2=hrs,3=days,4=weeks
    
        Mode = 2                        ' 0=0-Shot Off Start
                                        ' 1=1-Shot On Start
                                        ' 2=Repeating Off Start
                                        ' 3=Repeating On Start
    
        SetOffTime = 30                 ' 30 seconds off
        SetOnTime = 50                  ' 50 seconds on
    
        RelayOutput = 0                 ' Relay is off
    
    StartMenu:
       
    Start:
        gosub StartTimer                        ' Start the Elapsed Timer
        gosub InitialState
        if Mode = 1 then OneShotOnStart
        if (Mode = 2) or (Mode = 3) then Repeating
        
    OneShotOffStart:
        gosub OffUnits   
        gosub OnUnits
        if OnTime => SetOnTime then 
            low Relay
            RelayOutput = 1
            gosub StopTimer
            gosub ResetTime
            end
            goto StartMenu
        endif
            goto OneShotOffStart
    
    OneShotOnStart:
        gosub OffUnits   
        gosub OnUnits
        if OffTime => SetOffTime then 
            high Relay
            RelayOutput = 0
            gosub StopTimer
            gosub ResetTime
            end
            goto StartMenu
        endif
            goto OneShotOnStart
    
    Repeating:                                      ' Repeating mode
        gosub OffUnits   
        gosub OnUnits
        if (OffTime => SetOffTime) and (RelayOutput = 1) then 
            lcdout $fe,1,"TIME LEFT ON   "
            gosub DisplayData
            high Relay
            RelayOutput = 0
            gosub ResetTime
            goto Repeating
        endif
        if (OnTime => SetOnTime) and (RelayOutput = 0) then 
            lcdout $fe,1,"TIME LEFT OFF  "
            gosub DisplayData
            low Relay
            RelayOutput = 1
            gosub ResetTime
            goto Repeating
        endif
        gosub DisplayData
        goto repeating
    
    InitialState:
        if (Mode = 1) or (Mode = 3) then 
            RelayOutput = 1                 ' Turn relay on
            low Relay
            else
            RelayOutput = 0                 ' Turn relay off
            high Relay
        endif
        return    
    
    OffUnits:
        select case OffTimeUnitSelect
            case 0
            OffTime = AccuSeconds
            OffTimeUnits[0] = "S" : OffTimeUnits[1] = "E" : OffTimeUnits[2] = "C" : OffTimeUnits[3] = "S"
            case 1
            OffTime = AccuMinutes
            OffTimeUnits[0] = "M" : OffTimeUnits[1] = "I" : OffTimeUnits[2] = "N" : OffTimeUnits[3] = "S"
            case 2
            OffTime = AccuHours
            OffTimeUnits[0] = "H" : OffTimeUnits[1] = "R" : OffTimeUnits[2] = "S" : OffTimeUnits[3] = " "
            case 3
            OffTime = AccuDays
            OffTimeUnits[0] = "D" : OffTimeUnits[1] = "A" : OffTimeUnits[2] = "Y" : OffTimeUnits[3] = "S"
        end select
        return
    
    OnUnits:
        select case OnTimeUnitSelect
            case 0
            OnTime = AccuSeconds
            OnTimeUnits[0] = "S" : OnTimeUnits[1] = "E" : OnTimeUnits[2] = "C" : OnTimeUnits[3] = "S"
            case 1
            OnTime = AccuMinutes
            OnTimeUnits[0] = "M" : OnTimeUnits[1] = "I" : OnTimeUnits[2] = "N" : OnTimeUnits[3] = "S"
            case 2
            OnTime = AccuHours
            OnTimeUnits[0] = "H" : OnTimeUnits[1] = "R" : OnTimeUnits[2] = "S" : OnTimeUnits[3] = " "
            case 3
            OnTime = AccuDays
            OnTimeUnits[0] = "D" : OnTimeUnits[1] = "A" : OnTimeUnits[2] = "Y" : OnTimeUnits[3] = "S"
        end select
        return
    
    DisplayData:
        if RelayOutput = 1 then
            lcdout i,line2,dec (SetOnTime-OnTime)," ",OnTimeUnits[0],OnTimeUnits[1],OnTimeUnits[2],OnTimeUnits[3]
            else
            lcdout i,line2,dec (SetOffTime-OffTime)," ",OffTimeUnits[0],OffTimeUnits[1],OffTimeUnits[2],OffTimeUnits[3]
        endif
        return

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


    Did you find this post helpful? Yes | No

    Default

    It looks like you may have modified the Elapsed.bas file.
    And it's probably interfering with the system variables now.

    Can you show the changes?
    <br>
    DT

  3. #3


    Did you find this post helpful? Yes | No

    Default

    I think I have some other problems lurking in the woodwork. Debugging right now to narrow the search.

    Code:
    '****************************************************************
    '*  Name    : ELAPSED.PBP                                       *
    '*  Author  : Darrel Taylor                                     *
    '*  Notice  : Copyright (c) 2003                                *
    '*  Date    : 12/16/2003                                        *
    '*  Notes   :                                                   *
    '****************************************************************
    
    Define  INTHAND _ClockCount    ' Tell PBP Where the code starts on an interrupt
    Include "ASM_INTS.pbp"         ' ASM Interrupt Stubs
    
        Ticks           var     byte   ' 1/100th of a second
        Seconds         var     byte
        Minutes         var     byte
        Hours           var     byte
        Days            var     word
        AccuTicks       var     word
        AccuSeconds     var     word
        AccuMinutes     var     word
        AccuHours       var     word
        AccuDays        var     word
        R0save          var     word
        R1save          var     word
    
        SecondsChanged  var bit : SecondsChanged = 1
        MinutesChanged  var bit : MinutesChanged = 1
        HoursChanged    var bit
        DaysChanged     var bit
    
    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 = 03CB7h
      EndIF
      
    ; -----------------  ADD TimerConst to TMR1H:TMR1L
    ADD2_TIMER   macro
        CHK?RP  T1CON
        BCF     T1CON,TMR1ON           ; Turn off timer
        MOVLW   LOW(TimerConst)        ;  1
        ADDWF   TMR1L,F                ;  1    ; reload timer with correct value
        BTFSC   STATUS,C               ;  1/2
        INCF    TMR1H,F                ;  1
        MOVLW   HIGH(TimerConst)       ;  1
        ADDWF   TMR1H,F                ;  1
        endm
    
    ; -----------------  ADD TimerConst to TMR1H:TMR1L and restart TIMER1 
    RELOAD_TIMER  macro
        ADD2_TIMER
        BSF     T1CON,TMR1ON           ;  1    ; Turn TIMER1 back on
        CHK?RP  PIR1
        bcf     PIR1, TMR1IF           ; Clear Timer1 Interrupt Flag
        endm
    
    ; -----------------  Load TimerConst into TMR1H:TMR1L 
    LOAD_TIMER  macro
    EndAsm
        T1CON.0 = 0                    ; Turn OFF Timer1
        TMR1L = 0
        TMR1H = 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
        Ticks = Ticks + 1
        AccuTicks = AccuTicks + 1
        if Ticks = 100 then
           Ticks = Ticks-100
           Seconds = Seconds + 1
           AccuSeconds = AccuSeconds + 1
           SecondsChanged = 1
           if Seconds = 60 then
              Minutes = Minutes + 1
              AccuMinutes = AccuMinutes + 1
              MinutesChanged = 1
              Seconds = 0
           endif
           if Minutes = 60 then
              Hours = Hours + 1
              AccuHours = AccuHours + 1
              HoursChanged = 1
              Minutes = 0
           endif
           if Hours = 24 then
              Days = Days + 1
              AccuDays = AccuDays + 1
              DaysChanged = 1
              Hours = 0
           endif
        endif
      R1 = R1save                     ; Restore the PBP system vars
      R0 = R0save
    @ INT_RETURN                      ; Restore context and return from interrupt
    
    '-----====[ END OF TMR1 Interrupt Handler ]====---------------------------------
    
    StartTimer:
        T1CON.1 = 0                   ; (TMR1CS) Select FOSC/4 Clock Source
        T1CON.3 = 0                   ; (T1OSCEN) Disable External Oscillator
        PIR1.0  = 0                   ; (TMR1IF) Clear Timer1 Interrupt Flag
        PIE1.0  = 1                   ; (TMR1IE) Enable TMR1 overflow interrupt
        INTCON.6 = 1                  ; (PEIE) Enable peripheral interrupts
        INTCON.7 = 1                  ; (GIE) Enable global interrupts
        T1CON.0 = 1                   ; (TMR1ON) Start TIMER1
    return
    
    ; -----------------
    StopTimer:
        T1CON.0 = 0                   ; Turn OFF Timer1
    return
    
    ; -----------------
    ResetTime:
        R0save = T1CON.0              ; Save TMR1ON bit
        T1CON.0 = 0                   ; Turn OFF Timer1
        TMR1L = 0
        TMR1H = 0
    @   LOAD_TIMER                    ; Load TimerConst
        T1CON.0 = R0save              ; Restore TMR1ON bit
        Ticks = 0
        Seconds = 0
        Minutes = 0
        Hours = 0
        Days = 0
        AccuTicks = 0
        AccuSeconds = 0
        AccuMinutes = 0
        AccuHours = 0
        AccuDays = 0
        SecondsChanged = 1
    return
    
    OverElapsed:

  4. #4


    Did you find this post helpful? Yes | No

    Red face

    I figured out the problems, first had the on and off reversed and then somehow forgot the base resistor to the transistor firing the outlet relay, oops. lol

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


    Did you find this post helpful? Yes | No

    Default

    Great! Glad you found it.

    And just for more info, I compiled the modified elapsed file and everything looks good. It doesn't use any more system vars.

    Cheers,
    DT

  6. #6


    Did you find this post helpful? Yes | No

    Default

    The only modification I made to the ELASPED include file was the addition of the Accumulative seconds, minutes, hours and days variables. This allows me to enter 150 seconds instead of 2 minutes 30 seconds. I may get fancy and automatically scale the variables down as they decrease in value, just depends on what code space I will have left over.

Similar Threads

  1. Instant Interrupts - Revisited
    By Darrel Taylor in forum Code Examples
    Replies: 772
    Last Post: - 17th February 2016, 22:14
  2. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 17:39
  3. Get elapsed time while TIMER samples pulses
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th March 2009, 16:27
  4. Elapsed Timer Demo in a PIC12F675
    By Leonardo in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 21st November 2008, 00:01
  5. Darrel Taylor Elapsed Timer
    By rwskinner in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 13th March 2008, 01:22

Members who have read this thread : 1

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