Modded this code for program:
https://dt.picbasic.co.uk/CODEX/ETimer
Elapsed_DN.bas came from here:
https://www.picbasic.co.uk/forum/con...sed-Timer-Demo
ASM_INTS.bas came from here:
https://dt.picbasic.co.uk/CODEX/ETimerExamples
Modded this code for program:
https://dt.picbasic.co.uk/CODEX/ETimer
Elapsed_DN.bas came from here:
https://www.picbasic.co.uk/forum/con...sed-Timer-Demo
ASM_INTS.bas came from here:
https://dt.picbasic.co.uk/CODEX/ETimerExamples
Last edited by Demon; - 5th October 2024 at 05:07.
My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.
Not as dumb as yesterday, but stupider than tomorrow!
There is no FSR in the 16F18877, you have FSR0 and FSR1.
You need to look at the TMR1 setup... you need to set different registers and bits (ie T1CON and T1CLK).
My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.
Not as dumb as yesterday, but stupider than tomorrow!
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.
Last edited by Demon; - 5th October 2024 at 23:33.
My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.
Not as dumb as yesterday, but stupider than tomorrow!
I would start off with interrupt and in routine just add to a counter then in Basic do the math to get seconds, minutes etc. Otherwise that is a lot of code to fumble through.![]()
there are more problems here than i can count
1, timer3 has no clock source
2, how can ticks ever get beyond 100
whenCode: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
3, a 16f18877 like all enhanced core pic16's has Auto context saveCode: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
the entire int_start / int_return macros are inappropriate
4, you need to rethink that, that is for timer1 not 3
Code: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
Warning I'm not a teacher
I'm already using Timer1, hence why I hoped to use Timer3.
I'll swap what I'm doing, so I can use DT's routine without changing Timer1.
(At the restaurant, so can't post elaborate reply)
My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.
Not as dumb as yesterday, but stupider than tomorrow!
My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.
Not as dumb as yesterday, but stupider than tomorrow!
how big would Ticks have to be to make this true ?
IF Ticks > 100 THEN
Warning I'm not a teacher
I have no clue why the elapsed timer worked once before I went to bed. I get up today, and I can't get it to repeat the same results.
I didn't comprehend the depth of this statement, until now. I thought I was just forced to accept using Timer1 in the routines, and adapt my main code to use other timers.
As they say, this is the way. I already have Timer1 running in 50ms intervals, I can easily repeat the same process with Timer3 and maintain a counter (I'm only after 100-200ms intervals anyways, this is just to avoid checking ADC every MainLoop).
I really thought Elapsed Timer could work with the 16F18877. I didn't understand that the assembler code just doesn't work with the newer PICs.
(Hopefully I haven't misunderstood anything this time)
My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.
Not as dumb as yesterday, but stupider than tomorrow!
PIC 16F18877
Timer1 at 50ms intervals
Timer3 at 150ms intervals
Code:
Code:'*************************************************************************************************** ' * ' Generate trace on Interrupt on Logic Probe * ' * ' PIC 16F18877 * ' * '*************************************************************************************************** #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 ---------------------------------------------------------------- include "I:\Project_v2\PBP\PBP_Includes\DT_INTS-14_16F18877.bas" include "I:\Project_v2\PBP\PBP_Includes\ReEnterPBP.bas" ASM INT_LIST macro ; IntSource, Label, Type, ResetFlag? INT_Handler TMR1_INT, _Timer1Interrupt, PBP, yes INT_Handler TMR3_INT, _Timer3Interrupt, PBP, yes endm INT_CREATE ; Creates the interrupt processor INT_ENABLE TMR1_INT ;enables TMR1 interrupts INT_ENABLE TMR3_INT ;enables TMR3 interrupts ENDASM DEFINE OSC 32 define CCP1_REG 0 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 T1CON = %00110001 ' 1:8 Prescale, Enables Timer1 T3CON = %00110001 ' 1:8 Prescale, Enables Timer3 ' bit 7-6 Unimplemented: Read as ‘0’ ' bit 5-4 CKPS<1:0>: Timer1 Input Clock Prescale Select bits ' ----> 11 = 1:8 Prescale value ' 10 = 1:4 Prescale value ' 01 = 1:2 Prescale value ' 00 = 1:1 Prescale value ' bit 3 Unimplemented: Read as ‘0’ ' bit 2 SYNC: Timer1 Synchronization Control bit ' When TMR1CLK = FOSC or FOSC/4 ' This bit is ignored. The timer uses the internal clock and no ' additional synchronization is performed. ' When TMR1CS<1:0> = (any setting other than FOSC or FOSC/4) ' 1 = Do not synchronize external clock input ' 0 = Synchronized external clock input with system clock ' bit 1 RD16: Timer1 On bit ' 1 = All 16 bits of Timer1 can be read simultaneously (TMR1H is buffered) ' 0 = 16-bit reads of Timer1 are disabled (TMR1H is not buffered) ' bit 0 ON: Timer1 On bit ' ----> 1 = Enables Timer1 ' 0 = Stops Timer1 and clears Timer1 gate flip-flop T1CLK = %00000001 ' FOSC/4 Timer1 Clock T3CLK = %00000001 ' FOSC/4 Timer1 Clock ' bit 7-4 Unimplemented: Read as ‘0’ ' bit 3-0 TxCS<3:0>: Timer1/3/5 Clock Select bits ' 1111 = LC4_out ' 1110 = LC3_out ' 1101 = LC2_out ' 1100 = LC1_out ' 1011 = TMR5 overflow output ' 1010 = TMR3 overflow output ' 1001 = TMR1 overflow output ' 1000 = TMR0 overflow output ' 0111 = CLKR output clock ' 0110 = SOSC ' 0101 = MFINTOSC ' 1.05msec @ 32MHz with no preload ' 0100 = LFINTOSC ' n/a ' 0011 = HFINTOSC ' 16.56msec @ 32MHz with no preload ' 0010 = FOSC ' 16.54msec @ 32MHz with no preload ' ----> 0001 = FOSC/4 ' 66.18msec @ 32MHz with no preload ' 0000 = TxCKIPPS ANSELA = %00000000 ANSELB = %00000000 ANSELC = %00000000 ANSELD = %00000000 ANSELE = %00000000 TRISA = %00000000 TRISB = %00000000 TRISC = %00000000 TRISD = %00000000 TRISE = %00001000 Timer1Ended var byte Timer3Ended var byte Timer3Counter var byte LatB.2 = 0 ' Set Timer1 trace low LatB.0 = 0 ' Set Timer3 trace low LatB.1 = 0 ' Set heartbeat trace low pause 1 goto Start ;--- Interrupts ---------------------------------------------------------------- Timer1Interrupt: T1CON.0 = 0 ' Stops Timer1 Timer1Ended = 1 ' Set flag @ INT_RETURN Timer3Interrupt: T3CON.0 = 0 ' Stops Timer3 Timer3Ended = 1 ' Set flag @ INT_RETURN ;--- Subroutines --------------------------------------------------------------- StartTimer1: @ INT_CLEAR TMR1_INT -- clear flags TMR1H = 62 : TMR1L = 95 ' 50msec interval (32MHz, 16bit, 1:8) T1CON.0 = 1 ' Starts Timer1 Timer1Ended = 0 ' Set flag return StartTimer3: @ INT_CLEAR TMR1_INT -- clear flags TMR3H = 62 : TMR3L = 95 ' 50msec interval (32MHz, 16bit, 1:8) T3CON.0 = 1 ' Starts Timer3 Timer3Ended = 0 ' Set flag return Start: GOSUB StartTimer1 LatB.2 = 1 ' Start Timer1 trace Timer3Counter = 0 GOSUB StartTimer3 LatB.0 = 1 ' Start Timer3 trace Main: LatB.1 = 1 ' Pulse heartbeat IF Timer1Ended = 1 THEN LatB.2 = 0 ' Stop Timer1 trace GOSUB StartTimer1 LatB.2 = 1 ' Start Timer1 trace ENDIF IF Timer3Ended = 1 THEN Timer3Counter = Timer3Counter + 1 if Timer3Counter = 3 then Timer3Counter = 0 LatB.0 = 0 ' Stop Timer3 trace endif GOSUB StartTimer3 LatB.0 = 1 ' Start Timer3 trace ENDIF LatB.1 = 0 ' Reset heartbeat trace GOTO Main end![]()
My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.
Not as dumb as yesterday, but stupider than tomorrow!
Bookmarks