Help with TIMER1 + SLEEP


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2009
    Posts
    4

    Default Help with TIMER1 + SLEEP

    TIMER1 should be possible to still counting when the PIC is in SLEEP mode, but i dont get it to work.
    I have modded DT Elapsed_INT18.bas to work with a second 32.768kHz crystal on T1OSO and T1OSI:
    Code:
    '****************************************************************
    '*  Name    : Elapsed_INT-18.bas                                *
    '*  Author  : Darrel Taylor                                     *
    '*  Date    : JUL 11, 2006                                      *
    '*  Version : 1.0                                               *
    '*  Notes   : Must have DT_INTS-18.bas loaded first             *
    '****************************************************************
    DISABLE DEBUG
    
    ; syntax =     Handler  IntSource,        Label, Type, ResetFlag?
    DEFINE  Elapsed_Handler  TMR1_INT,  _ClockCount,  PBP,  yes
    ; the above define can be used in the INT_LIST macro, if desired (optional)
    
    Seconds  var byte
    Minutes  var byte
    Hours    var byte
    Days     var word
    
    SecondsChanged   var bit
    MinutesChanged   var bit
    HoursChanged     var bit
    DaysChanged      var bit
    SecondsChanged = 1
    MinutesChanged = 1
    
    Goto OverElapsed
    
    ' ------------------------------------------------------------------------------
    ' To calculate a constant for a different crystal frequency - see this web page
    ' http://www.picbasic.co.uk/forum/showthread.php?t=2031
    ' ------------------------------------------------------------------------------
    Asm
    
    TimerConst = 08000h  ; For 32,768 kHz
      
    ; -----------------  ADD TimerConst to TMR1H:TMR1L
    ADD2_TIMER   macro
    ;    CHK?RP  T1CON
        BCF     T1CON,TMR1ON, 0        ; Turn off timer
        MOVLW   LOW(TimerConst)        ;  1
        ADDWF   TMR1L,F, 0             ;  1    ; reload timer with correct value
        BTFSC   STATUS,C               ;  1/2
        INCF    TMR1H,F, 0             ;  1
        MOVLW   HIGH(TimerConst)       ;  1
        ADDWF   TMR1H,F, 0             ;  1
        endm
    
    ; -----------------  ADD TimerConst to TMR1H:TMR1L and restart TIMER1 ----------
    RELOAD_TIMER  macro
        ADD2_TIMER
        BSF     T1CON,TMR1ON, 0           ;  1    ; Turn TIMER1 back on
        endm
    
    ; -----------------  Load TimerConst into TMR1H:TMR1L --------------------------
    LOAD_TIMER  macro
        MOVE?CT  0, T1CON,TMR1ON
        MOVE?CB  0, TMR1L
        MOVE?CB  0, TMR1H
        ADD2_TIMER
        endm
    EndAsm
    
    ' ------[ This is the Interrupt Handler ]---------------------------------------
    ClockCount:
    @ RELOAD_TIMER                    ; Reload TIMER1
    
           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
    
    @ INT_RETURN                      ; Restore context and return from interrupt
    
    '-----====[ END OF TMR1 Interrupt Handler ]====---------------------------------
    
    StartTimer:
        T1CON.0 = 1                   ; (TMR1ON) Start TIMER1
        T1CON.1 = 1                   ; (TMR1CS) Use External clock from RC0/T1OSO/T13CKI pin
        T1CON.2 = 1                   ; (T1SYNC) Do not synchronize external clock input
        T1CON.3 = 1                   ; (T1OSCEN) Disable External Oscillator
        T1CON.6 = 0                   ; (T1RUN) Device clock is derived from another source
    
    return
    
    ; -----------------
    StopTimer:
        T1CON.0 = 0                   ; Turn OFF Timer1
    return
    
    BitSave  VAR  BIT
    ; -----------------
    ResetTime:
        BitSave = T1CON.0             ; Save TMR1ON bit
    @   LOAD_TIMER                    ; Load TimerConst
        T1CON.0 = BitSave             ; Restore TMR1ON bit
        Seconds = 0
        Minutes = 0
        Hours = 0
        Days = 0
        SecondsChanged = 1
        MinutesChanged = 1
        HoursChanged = 1
        DaysChanged = 1
    return
    
    OverElapsed:
    ENABLE DEBUG
    I am not 100% sure of the T1CON settings (red in the code)
    If i just send the data out without SLEEP via USART it works like a charm! But if i add:
    @ SLEEP
    HSEROUT [DEC2 Hours,DEC2 Minutes,DEC2 Seconds]
    (WDT on, WDTPS = 128 in settings)

    It don't work, i just get strange results like "Ò‚‚‚‚Ò‚‚‚‚Ò‚‚Ò‚S‚Ò‚‚Ò" in hypertrm

    Any ideas?

    It is PIC18F4550 and yes i have looked in the manual!
    Last edited by Yodoad; - 22nd May 2009 at 01:23.

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


    Did you find this post helpful? Yes | No

    Default

    Try adding a couple of Dummy statements AFTER the @ SLEEP in your PICBASIC example and see what happens...

    @ SLEEP
    @ NOP
    @ NOP

  3. #3
    Join Date
    May 2009
    Posts
    4


    Did you find this post helpful? Yes | No

    Default

    I did switch over from primary external clock to internal clock and it works better now!
    But if the PIC sleeps more then 1 second i get into trouble.
    So guess the PIC cannot sleep more then 1 second to get proper result?
    Because the interrupt routine is build to do a interrupt every second. And an TMR1 interrupt would not execute if the PIC is in sleep mode, right? Even if i have secondary clock source for TIMER1?

Similar Threads

  1. Won't go back to SLEEP after 1st Interrupt
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 29th June 2009, 09:00
  2. 16F628A current high during sleep
    By Rubicon in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 9th October 2006, 10:21
  3. Wierd sleep issue
    By orca in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 14th March 2006, 22:06
  4. Timer1 and Interupt wierdness
    By mind in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 23rd August 2005, 01:24
  5. timer1 sleep
    By yettie in forum General
    Replies: 2
    Last Post: - 5th August 2005, 01:01

Members who have read this thread : 1

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

Tags for this Thread

Posting Permissions

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