16F18877
I start the timer, but I don't get a pulse on Logic Probe after 100 Ticks.
Code:
Code:
#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
define CCP1_REG 0 ' Must clear unused CCP pins or else unpredictable results
DEFINE CCP1_BIT 0
define CCP2_REG 0
DEFINE CCP2_BIT 0
DEFINE CCP3_REG 0
DEFINE CCP3_BIT 0
define CCP4_REG 0
DEFINE CCP4_BIT 0
define CCP5_REG 0 ' Must clear unused CCP pins or else unpredictable results
DEFINE CCP5_BIT 0
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_Timer3_32MHz.bas" ' Elapsed Timer Routines
'Include "ASM_INTS_FSR0.bas" ' Required in folder to compile
Days = 0 ' set initial time
Hours = 0
Minutes = 1
Seconds = 0
LatB.0 = 0 ' Set interrupt trace low
LatB.1 = 0 ' Set heartbeat trace low
GOSUB StartTimer
LatB.0 = 1 ' Beginning of interrupt
Main:
LatB.1 = 1 ' Pulse heartbeat
IF Ticks > 100 THEN
LatB.0 = 0 ' End of interrupt
ENDIF
LatB.1 = 0 ' Reset heartbeat trace
GOTO Main
end
Elapsed_DN_Timer3_32MHz.bas, modified for Timer3 and 32MHz:
Code:
'****************************************************************
'* Name : ELAPSED.PBP *
'* Author : Darrel Taylor *
'* Notice : Copyright (c) 2003 *
'* Date : 12/16/2003 *
'* Notes : *
'****************************************************************
'* Name : Elapsed_DN_Timer3_32MHz.bas *
'* Author : Robert H *
'* Date : 2024-10-05 *
'* Notes : Added support for 16F18877: *
' a) Added 32MHz *
' b) Used Timer3 instead of Timer1: *
' - changed Timer1 to Timer3 *
' - changed T1 to T3 *
' - changed TMR1 to TMR3 *
' - changed PIR1 to PIR4 *
' - changed PIE1 to PIE4 *
' c) Changed T3CON,TMR3ON to T3CON,ON *
' d) Used FSR0 in ASM_INTS_FSR0.bas *
'****************************************************************
Define INTHAND _ClockCount ' Tell PBP Where the code starts on an interrupt
Include "ASM_INTS_FSR0.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 Timer3
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 ; T3CON
TimerConst = 063C7h ; TMR3CS bit 1, 0 = FOSC/4
EndIF ; T3CKPS bit 5-4, 01 = 1:2 Prescale value
; ----------------- ADD TimerConst to TMR3H:TMR3L
ADD2_TIMER macro
CHK?RP T3CON
BCF T3CON,TMR3ON ; Turn off timer
MOVLW LOW(TimerConst) ; 1
ADDWF TMR3L,F ; 1 ; reload timer with correct value
BTFSC STATUS,C ; 1/2
INCF TMR3H,F ; 1
MOVLW HIGH(TimerConst) ; 1
ADDWF TMR3H,F ; 1
endm
; ----------------- ADD TimerConst to TMR3H:TMR3L and restart Timer3
RELOAD_TIMER macro
ADD2_TIMER
BSF T3CON,TMR3ON ; 1 ; Turn Timer3 back on
CHK?RP PIR4
bcf PIR4, TMR3IF ; Clear Timer3 Interrupt Flag
endm
; ----------------- Load TimerConst into TMR3H:TMR3L
LOAD_TIMER macro
EndAsm
T3CON.0 = 0 ; Turn OFF Timer3
TMR3L = 0
TMR3H = 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 Timer3
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 TMR3 Interrupt Handler ]====---------------------------------
StartTimer:
IF NOT ZERO THEN
T3CON.1 = 0 ; (TMR3CS) Select FOSC/4 Clock Source
T3CON.3 = 0 ; (T3OSCEN) Disable External Oscillator
PIR4.0 = 0 ; (TMR3IF) Clear Timer3 Interrupt Flag
PIE4.0 = 1 ; (TMR3IE) Enable TMR3 overflow interrupt
INTCON.6 = 1 ; (PEIE) Enable peripheral interrupts
INTCON.7 = 1 ; (GIE) Enable global interrupts
T3CON.0 = 1 ; (TMR3ON) Start Timer3
ENDIF
return
; -----------------
StopTimer:
T3CON.0 = 0 ; Turn OFF Timer3
return
; -----------------
ResetTime:
ZERO = 0
R0save = T3CON.0 ; Save TMR3ON bit
T3CON.0 = 0 ; Turn OFF Timer3
TMR3L = 0
TMR3H = 0
@ LOAD_TIMER ; Load TimerConst
T3CON.0 = R0save ; Restore TMR3ON bit
Ticks = 0
Seconds = 0
Minutes = 0
Hours = 0
Days = 0
SecondsChanged = 1
return
OverElapsed:
ASM_INTS_FSR0.bas, modified for FSR0:
Code:
'****************************************************************
'* Name : ASM_INTS.PBP *
'* Author : Darrel Taylor *
'* Notice : Copyright (c) 2003 *
'* Date : JAN 4, 2003 *
'* Notes : *
'****************************************************************
'* Name : ASM_INTS_FSR0.bas *
'* Author : Robert H *
'* Date : 2024-10-05 *
'* Notes : Added support for 16F18877: *
' a) Changed FSR to FSR0 *
'****************************************************************
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 FSR0 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 FSR0,W ; move FSR0 reg to be saved into W reg
movwf fsave ; save FSR0 reg to a bank 0 register
endm
EndAsm
Asm
INT_RETURN macro
MOVF fsave,W ; Restore the FSR0 reg
MOVWF FSR0
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
I used 18F4450 as an example of "default" settings.
18F4450, bit 0 is called TMR1ON in the datasheet.
16F18877, bit 0 is called ON.
If I rename the bit to ON, I get Symbol not previously defined (ON).
I must have missed something.
Bookmarks