Demon
- 5th October 2024, 03:37
16F18877
Program:
#CONFIG
__config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
__config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
__config _CONFIG5, _CP_OFF & _CPD_OFF
#ENDCONFIG
;--- Interrupts ----------------------------------------------------------------
;--- Defines -------------------------------------------------------------------
DEFINE OSC 32
RA4PPS = 0 ' Disable CCP5
RB0PPS = 0 ' Disable CCP4
RB5PPS = 0 ' Disable CCP3
RC1PPS = 0 ' Disable CCP2
RC2PPS = 0 ' Disable CCP1
ANSELA = %00000000
ANSELB = %00000000
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000000
;--- Setup Port directions -----------------------------------------------------
TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %00001000
INCLUDE "Elapsed_DN_32MHz.bas" ; Elapsed Timer Routines
Days = 0 ' set initial time
Hours = 0
Minutes = 0
Seconds = 1
LatB.5 = 0
GOSUB StartTimer
LatB.5 = 1
Main:
IF Ticks > 100 THEN
LatB.5 = 0
ENDIF
GOTO Main
end
Elapsed DN, with 32MHz mod:
'************************************************* ***************
'* Name : ELAPSED.PBP *
'* Author : Darrel Taylor *
'* Notice : Copyright (c) 2003 *
'* Date : 12/16/2003 *
'* Notes : *
'************************************************* ***************
Define INTHAND _ClockCount ' Tell PBP Where the code starts on an interrupt
Include "ASM_INTS.bas" ' ASM Interrupt Stubs
Ticks VAR BYTE ' 1/100th of a second
Seconds VAR BYTE
Minutes VAR BYTE
Hours VAR BYTE
Days VAR WORD
R0save VAR WORD
R1save VAR WORD
ZERO VAR BIT
CountDown VAR BIT
SecondsChanged VAR BIT
MinutesChanged VAR BIT
HoursChanged VAR BIT
DaysChanged VAR BIT
CountDown = 0
SecondsChanged = 1
MinutesChanged = 1
Goto OverElapsed
' ------------------------------------------------------------------------------
Asm
IF OSC == 4 ; Constants for 100hz interrupt from Timer1
TimerConst = 0D8F7h ; Executed at compile time only
EndIF
If OSC == 8
TimerConst = 0B1E7h
EndIF
If OSC == 10
TimerConst = 09E5Fh
EndIF
If OSC == 20
TimerConst = 03CB7h
EndIF
If OSC == 32 ; T1CON
TimerConst = 063C7h ; TMR1CS bits 7-6, 00 = FOSC/4
EndIF ; T1CKPS bits 5-4, 01 = 1:2 Prescale value
; ----------------- ADD TimerConst to TMR1H:TMR1L
ADD2_TIMER macro
CHK?RP T1CON
BCF T1CON,TMR1ON ; Turn off timer
MOVLW LOW(TimerConst) ; 1
ADDWF TMR1L,F ; 1 ; reload timer with correct value
BTFSC STATUS,C ; 1/2
INCF TMR1H,F ; 1
MOVLW HIGH(TimerConst) ; 1
ADDWF TMR1H,F ; 1
endm
; ----------------- ADD TimerConst to TMR1H:TMR1L and restart TIMER1
RELOAD_TIMER macro
ADD2_TIMER
BSF T1CON,TMR1ON ; 1 ; Turn TIMER1 back on
CHK?RP PIR1
bcf PIR1, TMR1IF ; Clear Timer1 Interrupt Flag
endm
; ----------------- Load TimerConst into TMR1H:TMR1L
LOAD_TIMER macro
EndAsm
T1CON.0 = 0 ; Turn OFF Timer1
TMR1L = 0
TMR1H = 0
Asm
ADD2_TIMER
endm
EndAsm
' ------[ This is the Interrupt Handler ]---------------------------------------
ClockCount: ' Note: this is being handled as an ASM interrupt
@ INT_START
@ RELOAD_TIMER ; Reload TIMER1
R0save = R0 ; Save 2 PBP system vars that are used during
R1save = R1 ; the interrupt
Ticks = Ticks + 1
if Ticks = 100 then
Ticks = 0
IF CountDown THEN
IF Seconds > 0 THEN
Seconds = Seconds - 1
SecondsChanged = 1
IF Seconds = 0 THEN
IF Days = 0 THEN
IF Hours = 0 THEN
IF Minutes = 0 THEN
GOSUB StopTimer ; Zero reached
ZERO = 1
ENDIF
ENDIF
ENDIF
ENDIF
ELSE
IF Minutes > 0 THEN
Minutes = Minutes - 1
Seconds = 59
SecondsChanged = 1
MinutesChanged = 1
ELSE
IF Hours > 0 THEN
Hours = Hours - 1
Minutes = 59
Seconds = 59
SecondsChanged = 1
MinutesChanged = 1
HoursChanged = 1
ELSE
IF Days > 0 THEN
Days = Days - 1
Hours = 23
Minutes = 59
Seconds = 59
SecondsChanged = 1
MinutesChanged = 1
HoursChanged = 1
DaysChanged = 1
ELSE ; Zero already reached, shouldn't get here
GOSUB StopTimer
ZERO = 1
ENDIF
ENDIF
ENDIF
ENDIF
ELSE ; Counting Up
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
endif
ENDIF
R1 = R1save ; Restore the PBP system vars
R0 = R0save
@ INT_RETURN ; Restore context and return from interrupt
'-----====[ END OF TMR1 Interrupt Handler ]====---------------------------------
StartTimer:
IF NOT ZERO THEN
T1CON.1 = 0 ; (TMR1CS) Select FOSC/4 Clock Source
T1CON.3 = 0 ; (T1OSCEN) Disable External Oscillator
PIR1.0 = 0 ; (TMR1IF) Clear Timer1 Interrupt Flag
PIE1.0 = 1 ; (TMR1IE) Enable TMR1 overflow interrupt
INTCON.6 = 1 ; (PEIE) Enable peripheral interrupts
INTCON.7 = 1 ; (GIE) Enable global interrupts
T1CON.0 = 1 ; (TMR1ON) Start TIMER1
ENDIF
return
; -----------------
StopTimer:
T1CON.0 = 0 ; Turn OFF Timer1
return
; -----------------
ResetTime:
ZERO = 0
R0save = T1CON.0 ; Save TMR1ON bit
T1CON.0 = 0 ; Turn OFF Timer1
TMR1L = 0
TMR1H = 0
@ LOAD_TIMER ; Load TimerConst
T1CON.0 = R0save ; Restore TMR1ON bit
Ticks = 0
Seconds = 0
Minutes = 0
Hours = 0
Days = 0
SecondsChanged = 1
return
OverElapsed:
ASM_INTS.bas:
'************************************************* ***************
'* Name : ASM_INTS.PBP *
'* Author : Darrel Taylor *
'* Notice : Copyright (c) 2003 *
'* Date : JAN 4, 2003 *
'************************************************* ***************
wsave var byte $20 SYSTEM ' location for W if in bank0
' --- IF any of these three lines cause an error ?? Simply Comment them out to fix the problem ----
wsave1 var byte $A0 SYSTEM ' location for W if in bank1
wsave2 var byte $120 SYSTEM ' location for W if in bank2
wsave3 var byte $1A0 SYSTEM ' location for W if in bank3
' ------------------------------------------------------------------------------
ssave var byte BANK0 SYSTEM ' location for STATUS register
psave var byte BANK0 SYSTEM ' location for PCLATH register
fsave var byte BANK0 SYSTEM ' location for FSR register
Asm
INT_START macro
IF (CODE_SIZE <= 2)
movwf wsave ; copy W to wsave register
swapf STATUS,W ; swap status reg to be saved into W
clrf STATUS ; change to bank 0 regardless of current bank
movwf ssave ; save status reg to a bank 0 register
movf PCLATH,w ; move PCLATH reg to be saved into W reg
movwf psave ; save PCLATH reg to a bank 0 register
EndIF
movf FSR,W ; move FSR reg to be saved into W reg
movwf fsave ; save FSR reg to a bank 0 register
endm
EndAsm
Asm
INT_RETURN macro
MOVF fsave,W ; Restore the FSR reg
MOVWF FSR
Movf psave,w ; Restore the PCLATH reg
Movwf PCLATH
swapf ssave,w ; Restore the STATUS reg
movwf STATUS
swapf wsave,f
swapf wsave,w ; Restore W reg
Retfie ; Exit the interrupt routine
endm
EndAsm
For starters, I'm just trying to get a 100tick interval on the Logic Probe with LatB.5.
Program:
#CONFIG
__config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
__config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
__config _CONFIG5, _CP_OFF & _CPD_OFF
#ENDCONFIG
;--- Interrupts ----------------------------------------------------------------
;--- Defines -------------------------------------------------------------------
DEFINE OSC 32
RA4PPS = 0 ' Disable CCP5
RB0PPS = 0 ' Disable CCP4
RB5PPS = 0 ' Disable CCP3
RC1PPS = 0 ' Disable CCP2
RC2PPS = 0 ' Disable CCP1
ANSELA = %00000000
ANSELB = %00000000
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000000
;--- Setup Port directions -----------------------------------------------------
TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %00001000
INCLUDE "Elapsed_DN_32MHz.bas" ; Elapsed Timer Routines
Days = 0 ' set initial time
Hours = 0
Minutes = 0
Seconds = 1
LatB.5 = 0
GOSUB StartTimer
LatB.5 = 1
Main:
IF Ticks > 100 THEN
LatB.5 = 0
ENDIF
GOTO Main
end
Elapsed DN, with 32MHz mod:
'************************************************* ***************
'* Name : ELAPSED.PBP *
'* Author : Darrel Taylor *
'* Notice : Copyright (c) 2003 *
'* Date : 12/16/2003 *
'* Notes : *
'************************************************* ***************
Define INTHAND _ClockCount ' Tell PBP Where the code starts on an interrupt
Include "ASM_INTS.bas" ' ASM Interrupt Stubs
Ticks VAR BYTE ' 1/100th of a second
Seconds VAR BYTE
Minutes VAR BYTE
Hours VAR BYTE
Days VAR WORD
R0save VAR WORD
R1save VAR WORD
ZERO VAR BIT
CountDown VAR BIT
SecondsChanged VAR BIT
MinutesChanged VAR BIT
HoursChanged VAR BIT
DaysChanged VAR BIT
CountDown = 0
SecondsChanged = 1
MinutesChanged = 1
Goto OverElapsed
' ------------------------------------------------------------------------------
Asm
IF OSC == 4 ; Constants for 100hz interrupt from Timer1
TimerConst = 0D8F7h ; Executed at compile time only
EndIF
If OSC == 8
TimerConst = 0B1E7h
EndIF
If OSC == 10
TimerConst = 09E5Fh
EndIF
If OSC == 20
TimerConst = 03CB7h
EndIF
If OSC == 32 ; T1CON
TimerConst = 063C7h ; TMR1CS bits 7-6, 00 = FOSC/4
EndIF ; T1CKPS bits 5-4, 01 = 1:2 Prescale value
; ----------------- ADD TimerConst to TMR1H:TMR1L
ADD2_TIMER macro
CHK?RP T1CON
BCF T1CON,TMR1ON ; Turn off timer
MOVLW LOW(TimerConst) ; 1
ADDWF TMR1L,F ; 1 ; reload timer with correct value
BTFSC STATUS,C ; 1/2
INCF TMR1H,F ; 1
MOVLW HIGH(TimerConst) ; 1
ADDWF TMR1H,F ; 1
endm
; ----------------- ADD TimerConst to TMR1H:TMR1L and restart TIMER1
RELOAD_TIMER macro
ADD2_TIMER
BSF T1CON,TMR1ON ; 1 ; Turn TIMER1 back on
CHK?RP PIR1
bcf PIR1, TMR1IF ; Clear Timer1 Interrupt Flag
endm
; ----------------- Load TimerConst into TMR1H:TMR1L
LOAD_TIMER macro
EndAsm
T1CON.0 = 0 ; Turn OFF Timer1
TMR1L = 0
TMR1H = 0
Asm
ADD2_TIMER
endm
EndAsm
' ------[ This is the Interrupt Handler ]---------------------------------------
ClockCount: ' Note: this is being handled as an ASM interrupt
@ INT_START
@ RELOAD_TIMER ; Reload TIMER1
R0save = R0 ; Save 2 PBP system vars that are used during
R1save = R1 ; the interrupt
Ticks = Ticks + 1
if Ticks = 100 then
Ticks = 0
IF CountDown THEN
IF Seconds > 0 THEN
Seconds = Seconds - 1
SecondsChanged = 1
IF Seconds = 0 THEN
IF Days = 0 THEN
IF Hours = 0 THEN
IF Minutes = 0 THEN
GOSUB StopTimer ; Zero reached
ZERO = 1
ENDIF
ENDIF
ENDIF
ENDIF
ELSE
IF Minutes > 0 THEN
Minutes = Minutes - 1
Seconds = 59
SecondsChanged = 1
MinutesChanged = 1
ELSE
IF Hours > 0 THEN
Hours = Hours - 1
Minutes = 59
Seconds = 59
SecondsChanged = 1
MinutesChanged = 1
HoursChanged = 1
ELSE
IF Days > 0 THEN
Days = Days - 1
Hours = 23
Minutes = 59
Seconds = 59
SecondsChanged = 1
MinutesChanged = 1
HoursChanged = 1
DaysChanged = 1
ELSE ; Zero already reached, shouldn't get here
GOSUB StopTimer
ZERO = 1
ENDIF
ENDIF
ENDIF
ENDIF
ELSE ; Counting Up
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
endif
ENDIF
R1 = R1save ; Restore the PBP system vars
R0 = R0save
@ INT_RETURN ; Restore context and return from interrupt
'-----====[ END OF TMR1 Interrupt Handler ]====---------------------------------
StartTimer:
IF NOT ZERO THEN
T1CON.1 = 0 ; (TMR1CS) Select FOSC/4 Clock Source
T1CON.3 = 0 ; (T1OSCEN) Disable External Oscillator
PIR1.0 = 0 ; (TMR1IF) Clear Timer1 Interrupt Flag
PIE1.0 = 1 ; (TMR1IE) Enable TMR1 overflow interrupt
INTCON.6 = 1 ; (PEIE) Enable peripheral interrupts
INTCON.7 = 1 ; (GIE) Enable global interrupts
T1CON.0 = 1 ; (TMR1ON) Start TIMER1
ENDIF
return
; -----------------
StopTimer:
T1CON.0 = 0 ; Turn OFF Timer1
return
; -----------------
ResetTime:
ZERO = 0
R0save = T1CON.0 ; Save TMR1ON bit
T1CON.0 = 0 ; Turn OFF Timer1
TMR1L = 0
TMR1H = 0
@ LOAD_TIMER ; Load TimerConst
T1CON.0 = R0save ; Restore TMR1ON bit
Ticks = 0
Seconds = 0
Minutes = 0
Hours = 0
Days = 0
SecondsChanged = 1
return
OverElapsed:
ASM_INTS.bas:
'************************************************* ***************
'* Name : ASM_INTS.PBP *
'* Author : Darrel Taylor *
'* Notice : Copyright (c) 2003 *
'* Date : JAN 4, 2003 *
'************************************************* ***************
wsave var byte $20 SYSTEM ' location for W if in bank0
' --- IF any of these three lines cause an error ?? Simply Comment them out to fix the problem ----
wsave1 var byte $A0 SYSTEM ' location for W if in bank1
wsave2 var byte $120 SYSTEM ' location for W if in bank2
wsave3 var byte $1A0 SYSTEM ' location for W if in bank3
' ------------------------------------------------------------------------------
ssave var byte BANK0 SYSTEM ' location for STATUS register
psave var byte BANK0 SYSTEM ' location for PCLATH register
fsave var byte BANK0 SYSTEM ' location for FSR register
Asm
INT_START macro
IF (CODE_SIZE <= 2)
movwf wsave ; copy W to wsave register
swapf STATUS,W ; swap status reg to be saved into W
clrf STATUS ; change to bank 0 regardless of current bank
movwf ssave ; save status reg to a bank 0 register
movf PCLATH,w ; move PCLATH reg to be saved into W reg
movwf psave ; save PCLATH reg to a bank 0 register
EndIF
movf FSR,W ; move FSR reg to be saved into W reg
movwf fsave ; save FSR reg to a bank 0 register
endm
EndAsm
Asm
INT_RETURN macro
MOVF fsave,W ; Restore the FSR reg
MOVWF FSR
Movf psave,w ; Restore the PCLATH reg
Movwf PCLATH
swapf ssave,w ; Restore the STATUS reg
movwf STATUS
swapf wsave,f
swapf wsave,w ; Restore W reg
Retfie ; Exit the interrupt routine
endm
EndAsm
For starters, I'm just trying to get a 100tick interval on the Logic Probe with LatB.5.