;*******************
Interrupt:
call Clock_step
bcf INTCON, T0IF ;Clear interrupt flag to be able to return
retfie
The above code is called on each timed interrupt - every 8.192ms. Now you need to make something useful of that. The following is only a 122-interrupt second, just to show context.
;*******************
Clock_step:
incf tick
movfw tick
sublw D'122' ;Count 122 cycles, add 1 second
btfss STATUS,Z
goto Clock_end
clrf tick ;I have 1 second, reset int counter.
call Seconds_inc ;Increment seconds counter
movfw seconds
btfss STATUS,Z ;On seconds=0, increment minutes
goto Clock_end
call Minutes_inc ;Increment minutes counter
movfw minutes
btfss STATUS,Z ;On minutes=0, increment hours
goto Clock_end
call Hours_inc ;Increment hours counter
;--snip-- you get the idea.
Clock_end:
return
;*******************
Seconds_inc:
incf seconds
movfw seconds
sublw D'60'
btfsc STATUS,Z
clrf seconds ;Reset on 60 seconds
return
Minutes_inc:
;--snip-- again, you get the idea.
Remember, this is only the imprecise clock, only to show the concept. Now you'll need to adjust for 122 and 123 cycles and all that shizzle I mentioned in the other journal.
;*******************
Clock_step:
incf tick
movfw tick
[b]subwf tick_wait,w[/b] ;Instead of 122, check against tick_wait
btfss STATUS,Z
goto Clock_end
clrf tick ;I have 1 second, reset int counter.
[b]call Tick_adj[/b] ;Check if next cycle counts 122 o 123
call Seconds_inc ;Increment seconds counter
;--snip-- same as previous sample.
Clock_end:
return
;*******************
Seconds_inc:
;--snip-- same as previous sample.
;*******************
; NEW CODE BELOW
;*******************
Tick_adj:
incf adj_s
movfw adj_s
sublw D'14'
btfss STATUS,Z ;If 14, I'm done with 14s cycle
goto tick_adj2 ;Otherwise, check if it's 13
clrf adj_s ;reset 14s cycle
movlw D'122'
movwf tick_wait
goto tick_adj_end
tick_adj2
movfw adj_s
sublw D'13'
btfss STATUS,Z ;If 13, it's adjust time
goto tick_adj_end ;if not 13 either, nothing to do
incf adj_c
movfw adj_c
sublw D'64'
btfsc STATUS,Z
goto tick_adj_end ;if this is the 64th adj cycle, ignore it
incf tick_wait ;14th second is next, lasts 123
movfw adj_c
sublw D'65' ;Check if 65
btfsc STATUS,Z
clrf adj_c ;If 65, reset count
tick_adj_end:
return
As you can see, the code has grown quite a bit. But here we have our (theoretically) perfect clock.You must log in to post a comment. You can login or register a new account.
Code looks nice and simple too always a plus.
)