#chip 18f25k22,32
#config osc = int
dir portb.7 out
' TMR0_Initialize() '
InitTimer0(Osc, (TMR0_FOSC4 + PRE0_32) , POST0_1)
SetTimer(0, 0xF900) ' Preload Count for 1ms '
StartTimer 0
' Add a handler for the interrupt '
On Interrupt Timer0Overflow Call TMR0_ISR
do
loop
Sub TMR0_ISR()
portb.7 = not portb.7
;
SetTimer(0, 0xFA00) ' reset count for 1ms adj for isr time'
End Sub
End
Now here's the basic and the assembler equivalent
LIST p=18F25K22, r=DEC
#include <P18F25K22.inc>
CONFIG LVP = OFF, MCLRE = INTMCLR, WDTEN = OFF, FOSC = INTIO67
;********************************************************************************
;Set aside memory locations for variables
SAVESYSTEMP1 EQU 1
SYSBSR EQU 2
SYSSTATUS EQU 15
SYSTEMP1 EQU 3
SYSW EQU 14
TMRNUMBER EQU 4
TMRPOST EQU 5
TMRPRES EQU 6
TMRSOURCE EQU 7
TMRVALUE EQU 8
TMRVALUE_H EQU 9
;********************************************************************************
;Vectors
ORG 0
goto BASPROGRAMSTART
ORG 8
bra INTERRUPT
;********************************************************************************
;Start of program memory page 0
ORG 12
BASPROGRAMSTART
;Call initialisation routines
rcall INITSYS
;Enable interrupts
bsf INTCON,GIE,ACCESS
bsf INTCON,PEIE,ACCESS
;Start of the main program
;dir portb.7 out
bcf TRISB,7,ACCESS
;InitTimer0(Osc, (TMR0_FOSC4 + PRE0_32) , POST0_1)
movlw 1
movwf TMRSOURCE,BANKED
movlw 69
movwf TMRPRES,BANKED
clrf TMRPOST,BANKED
rcall INITTIMER0146
;SetTimer(0, 0xF900) ' Preload Count for 1ms '
clrf TMRNUMBER,BANKED
clrf TMRVALUE,BANKED
movlw 249
movwf TMRVALUE_H,BANKED
rcall SETTIMER
;StartTimer 0
clrf TMRNUMBER,BANKED
rcall STARTTIMER
;On Interrupt Timer0Overflow Call TMR0_ISR
bsf INTCON,TMR0IE,ACCESS
;do
SysDoLoop_S1
;loop
bra SysDoLoop_S1
SysDoLoop_E1
;End
bra BASPROGRAMEND
BASPROGRAMEND
sleep
bra BASPROGRAMEND
;********************************************************************************
INITSYS
;[canskip] SPLLEN, IRCF2, IRCF1, IRCF0 = b'1110'
bsf OSCTUNE,PLLEN,ACCESS
bsf OSCCON,IRCF2,ACCESS
bsf OSCCON,IRCF1,ACCESS
bcf OSCCON,IRCF0,ACCESS
;BSR = 0
clrf BSR,ACCESS
;TBLPTRU = 0
clrf TBLPTRU,ACCESS
;SET ADFM OFF
bcf ADCON2,ADFM,ACCESS
;SET ADCON0.ADON OFF
bcf ADCON0,ADON,ACCESS
;ANSELA = 0
banksel ANSELA
clrf ANSELA,BANKED
;ANSELB = 0
clrf ANSELB,BANKED
;ANSELC = 0
clrf ANSELC,BANKED
;#IFDEF bit(C2ON): C2ON = 0: #ENDIF
bcf CM2CON,C2ON,ACCESS
;#IFDEF bit(CM2CON0_EN): CM2CON0_EN = 0: #ENDIF
;#IFDEF bit(C1ON): C1ON = 0: #ENDIF
bcf CM1CON,C1ON,ACCESS
;#IFDEF bit(CM1CON0_EN): CM1CON0_EN = 0: #ENDIF
;PORTA = 0
clrf PORTA,ACCESS
;PORTB = 0
clrf PORTB,ACCESS
;PORTC = 0
clrf PORTC,ACCESS
;PORTE = 0
clrf PORTE,ACCESS
banksel 0
return
;********************************************************************************
;Overloaded signature: BYTE:BYTE:BYTE:
INITTIMER0146
return
;********************************************************************************
Interrupt
;Save Context
movff WREG,SysW
movff STATUS,SysSTATUS
movff BSR,SysBSR
;Store system variables
movff SysTemp1,SaveSysTemp1
;On Interrupt handlers
btfss INTCON,TMR0IE,ACCESS
bra NotTMR0IF
btfss INTCON,TMR0IF,ACCESS
bra NotTMR0IF
rcall TMR0_ISR
bcf INTCON,TMR0IF,ACCESS
bra INTERRUPTDONE
NotTMR0IF
;User Interrupt routine
INTERRUPTDONE
;Restore Context
;Restore system variables
movff SaveSysTemp1,SysTemp1
movff SysW,WREG
movff SysSTATUS,STATUS
movff SysBSR,BSR
retfie 0
;********************************************************************************
SETTIMER
;If TMRNumber = 0 Then
movf TMRNUMBER,F,BANKED
btfsc STATUS, Z,ACCESS
;TMR0L = TMRValue
movff TMRVALUE,TMR0L
;End If
;If TMRNumber = 1 then
decf TMRNUMBER,W,BANKED
btfss STATUS, Z,ACCESS
bra ENDIF11
;TMR1H = TMRValue_H
movff TMRVALUE_H,TMR1H
;TMR1L = TMRValue
movff TMRVALUE,TMR1L
;End If
ENDIF11
;If TMRNumber = 2 Then
movlw 2
subwf TMRNUMBER,W,BANKED
btfsc STATUS, Z,ACCESS
;TMR2 = TMRValue
movff TMRVALUE,TMR2
;End If
;If TMRNumber = 3 then
movlw 3
subwf TMRNUMBER,W,BANKED
btfss STATUS, Z,ACCESS
bra ENDIF13
;TMR3H = TMRValue_H
movff TMRVALUE_H,TMR3H
;TMR3L = TMRValue
movff TMRVALUE,TMR3L
;End If
ENDIF13
;If TMRNumber = 4 Then
movlw 4
subwf TMRNUMBER,W,BANKED
btfsc STATUS, Z,ACCESS
;TMR4 = TMRValue
movff TMRVALUE,TMR4
;End If
;If TMRNumber = 5 then
movlw 5
subwf TMRNUMBER,W,BANKED
btfss STATUS, Z,ACCESS
bra ENDIF15
;TMR5H = TMRValue_H
movff TMRVALUE_H,TMR5H
;TMR5L = TMRValue
movff TMRVALUE,TMR5L
;End If
ENDIF15
;If TMRNumber = 6 Then
movlw 6
subwf TMRNUMBER,W,BANKED
btfsc STATUS, Z,ACCESS
;TMR6 = TMRValue
movff TMRVALUE,TMR6
;End If
return
;********************************************************************************
STARTTIMER
;IF TMRNumber = 0 then Set TMR0ON on
movf TMRNUMBER,F,BANKED
btfsc STATUS, Z,ACCESS
bsf T0CON,TMR0ON,ACCESS
;IF TMRNumber = 1 then Set TMR1ON on
decf TMRNUMBER,W,BANKED
btfsc STATUS, Z,ACCESS
bsf T1CON,TMR1ON,ACCESS
;IF TMRNumber = 2 then Set TMR2ON on
movlw 2
subwf TMRNUMBER,W,BANKED
btfsc STATUS, Z,ACCESS
bsf T2CON,TMR2ON,ACCESS
;IF TMRNumber = 3 then Set TMR3ON on
movlw 3
subwf TMRNUMBER,W,BANKED
btfsc STATUS, Z,ACCESS
bsf T3CON,TMR3ON,ACCESS
;IF TMRNumber = 4 then Set TMR4ON on
movlw 4
subwf TMRNUMBER,W,BANKED
btfss STATUS, Z,ACCESS
bra ENDIF7
banksel T4CON
bsf T4CON,TMR4ON,BANKED
ENDIF7
;IF TMRNumber = 5 then Set TMR5ON on
movlw 5
banksel TMRNUMBER
subwf TMRNUMBER,W,BANKED
btfss STATUS, Z,ACCESS
bra ENDIF8
banksel T5CON
bsf T5CON,TMR5ON,BANKED
ENDIF8
;IF TMRNumber = 6 then Set TMR6ON on
movlw 6
banksel TMRNUMBER
subwf TMRNUMBER,W,BANKED
btfss STATUS, Z,ACCESS
bra ENDIF9
banksel T6CON
bsf T6CON,TMR6ON,BANKED
ENDIF9
banksel 0
return
;********************************************************************************
TMR0_ISR
;portb.7 = not portb.7
clrf SysTemp1,BANKED
btfsc PORTB,7,ACCESS
incf SysTemp1,F,BANKED
comf SysTemp1,F,BANKED
bcf LATB,7,ACCESS
btfsc SysTemp1,0,BANKED
bsf LATB,7,ACCESS
;
;SetTimer(0, 0xFA00) ' reset count for 1ms adj for isr time'
clrf TMRNUMBER,BANKED
clrf TMRVALUE,BANKED
movlw 250
movwf TMRVALUE_H,BANKED
rcall SETTIMER
return
;********************************************************************************
END