PDA

View Full Version : Help with TIMER1 + SLEEP



Yodoad
- 22nd May 2009, 01:19
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:

'************************************************* ***************
'* 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!

Melanie
- 22nd May 2009, 09:00
Try adding a couple of Dummy statements AFTER the @ SLEEP in your PICBASIC example and see what happens...

@ SLEEP
@ NOP
@ NOP

Yodoad
- 22nd May 2009, 15:07
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?