Code:
@ DEVICE LVP_OFF,HS_OSC
DEFINE OSC 20
DEFINE INTHAND TMRint
'Variables for saving state in interrupt handler
wsave VAR BYTE $70 system ' Saves W
ssave VAR BYTE bank0 system ' Saves STATUS
psave VAR BYTE bank0 system ' Saves PCLATH
fsave VAR BYTE bank0 system ' Saves FSR
TMR0_Ticks VAR BYTE system
TMR0 = 96
GoTo Init
Asm
TMRint
; Uncomment the following if the device has less than 2k of code space
; movwf wsave ; Save W
; swapf STATUS, W ; Swap STATUS to W (swap avoids changing STATUS)
; clrf STATUS ; Clear STATUS
; movwf ssave ; Save swapped STATUS
; movf PCLATH, W ; Move PCLATH to W
; movwf psave ; Save PCLATH
; Increment ticks, reload, clr int flags
;------------------------------------------------------------------------------
incf TMR0_Ticks,f ; increment overflow ticks
movlw 96 ; 256-96=160. 160x200nSx256=8.192mS. 8.192mSx122=0.999mS= ~1S
movwf TMR0 ; reload
bcf INTCON,2 ; clear TMR0 int flag
;----------------------------------------------------------------------------
movf fsave, W ; retrieve FSR value
movwf FSR ; Restore FSR
movf psave, W ; Retrieve PCLATH value
movwf PCLATH ; Restore PCLATH
swapf ssave, W ; (swap to avoid changing STATUS)
movwf STATUS ; Restore STATUS
swapf wsave, F ; Swap the stored W value
swapf wsave, W ; Restore it to W (swap to avoid changing STATUS)
retfie ; Return from the interrupt
EndAsm
Init:
PORTD = 0
TRISD = 0
OPTION_REG = %00000111 ' 1:256 prescaler to TMR0
INTCON = %11100000 ' Enable TIMR0 interrupts
Main:
IF TMR0_Ticks >= 122 THEN
TMR0_Ticks = 0
PORTD.0 = PORTD.0 ^ 1 ' ~1 second pulse on RD0
ENDIF
GOTO Main
End
Timer1 would be better, but this should get you started.
Bookmarks