Then since 2019 I have used this module "SingleTimer.bas"
which allowed me to select the timer_* to use for compatibility with other modules
using the option :
#OPTION SINGLETIMER_TIMER = USE_TIMER_1 ( or USE_TIMER_2, USE_TIMER_3, .. USE_TIMER_5 )
do not ask me what were the problems that led me to this solution because I do not remember
- Code: Select all
Module SingleTimer
imports Isr ' interrupt macros and constants
private const _fosc as single = _clock * 1000000 ' instructions per second
#if _clock = 40
private const _reloadTimerValue as single = _fosc / 1000 ' instructions every 1 ms (no prescaler)
private const _prescalerValue = 0
#elseif _clock = 80
private const _reloadTimerValue as single = _fosc / 1000 / 8 ' instructions every 1 ms (1:8 prescaler)
private const _prescalerValue = 16
#else
#warning "_prescalerValue not set for this speed in SingleTimer.bas"
#endif
private Const _allSystemVars As Byte = 0
Public Event OnTimeout()
private _Interval as ushort = 0
private _Enabled as boolean = True
private _Initial as Ushort = 0
#option SINGLETIMER_TIMER = USE_TIMER_1
'****************************************************************************
'* Name : OnTimer (Private) *
'* Purpose : Timer Event handler *
'****************************************************************************
#if SINGLETIMER_TIMER = USE_TIMER_1
private interrupt OnTimer(TIMER_1_VECTOR, ipl1Software)
#elseif SINGLETIMER_TIMER = USE_TIMER_2
private interrupt OnTimer(TIMER_2_VECTOR, ipl2Software)
#elseif SINGLETIMER_TIMER = USE_TIMER_3
private interrupt OnTimer(TIMER_3_VECTOR, ipl3Software)
#elseif SINGLETIMER_TIMER = USE_TIMER_4
private interrupt OnTimer(TIMER_4_VECTOR, ipl4Software)
#elseif SINGLETIMER_TIMER = USE_TIMER_5
private interrupt OnTimer(TIMER_5_VECTOR, ipl5Software)
#endif
If _Enabled Then
_Interval = _Interval - 1
If _Interval = 0 Then
_Enabled = False
RaiseEvent OnTimeout()
End If
End If
#if SINGLETIMER_TIMER = USE_TIMER_1
Isr.SetFlag(TIMER_1_VECTOR, 0)
#elseif SINGLETIMER_TIMER = USE_TIMER_2
Isr.SetFlag(TIMER_2_VECTOR, 0)
#elseif SINGLETIMER_TIMER = USE_TIMER_3
Isr.SetFlag(TIMER_3_VECTOR, 0)
#elseif SINGLETIMER_TIMER = USE_TIMER_4
Isr.SetFlag(TIMER_4_VECTOR, 0)
#elseif SINGLETIMER_TIMER = USE_TIMER_5
Isr.SetFlag(TIMER_5_VECTOR, 0)
#endif
End Interrupt
'****************************************************************************
'* Name : Set *
'* Purpose : Set timer for shot every Interval mSes *
'****************************************************************************
Public Sub Set(Interval As UShort)
_Initial = Interval
_Interval = Interval
_Enabled = True
Enable(OnTimer)
End Sub
'****************************************************************************
'* Name : Reload *
'* Purpose : Called when handler has make his job *
'****************************************************************************
Public inline Sub Reload()
_Interval = _Initial
_Enabled = True
end sub
'****************************************************************************
'* Name : Start *
'* Purpose : Start interrupt handling *
'****************************************************************************
Public Sub Start()
Enable(OnTimer)
End Sub
'****************************************************************************
'* Name : Halt *
'* Purpose : Stop interrupt handling *
'****************************************************************************
Public Sub Halt()
Disable(OnTimer)
End Sub
'****************************************************************************
'* Name : Main *
'* Purpose : Initialise the module *
'****************************************************************************
private Inline Sub Main()
' TnCON = 0 ' clear Timer n - no prescaler
' TnCON = 16 ' clear Timer n - and 1:8 prescaler (80Mhz devices)
#if SINGLETIMER_TIMER = USE_TIMER_1
isr.SetPriority(TIMER_1_VECTOR, ipl1)
isr.SetSubPriority(TIMER_1_VECTOR,0)
T1CON = _prescalerValue ' clear Timer 1 - prescaler 1:8 if 16
PR1 = _reloadTimerValue ' set timer period
#elseif SINGLETIMER_TIMER = USE_TIMER_2
isr.SetPriority(TIMER_2_VECTOR, ipl2)
isr.SetSubPriority(TIMER_2_VECTOR,0)
T2CON = _prescalerValue ' clear Timer 1 - prescaler 1:8 if 16
PR2 = _reloadTimerValue ' set timer period
#elseif SINGLETIMER_TIMER = USE_TIMER_3
isr.SetPriority(TIMER_3_VECTOR, ipl3)
isr.SetSubPriority(TIMER_3_VECTOR,0)
T3CON = _prescalerValue ' clear Timer 1 - prescaler 1:8 if 16
PR3 = _reloadTimerValue ' set timer period
#elseif SINGLETIMER_TIMER = USE_TIMER_4
isr.SetPriority(TIMER_4_VECTOR, ipl4)
isr.SetSubPriority(TIMER_4_VECTOR,0)
T4CON = _prescalerValue ' clear Timer 1 - prescaler 1:8 if 16
PR4 = _reloadTimerValue ' set timer period
#elseif SINGLETIMER_TIMER = USE_TIMER_5
isr.SetPriority(TIMER_5_VECTOR, ipl5)
isr.SetSubPriority(TIMER_5_VECTOR,0)
T5CON = _prescalerValue ' clear Timer 1 - prescaler 1:8 if 16
PR5 = _reloadTimerValue ' set timer period
#endif
IFS0.3 = 0 ' clear timer interrupt flag
#if SINGLETIMER_TIMER = USE_TIMER_1
T1CON.15 = 1 ' switch timer on
#elseif SINGLETIMER_TIMER = USE_TIMER_2
T2CON.15 = 1
#elseif SINGLETIMER_TIMER = USE_TIMER_3
T3CON.15 = 1
#elseif SINGLETIMER_TIMER = USE_TIMER_4
T4CON.15 = 1
#elseif SINGLETIMER_TIMER = USE_TIMER_5
T5CON.15 = 1
#endif
End Sub
End Module