Hi,
here a FW-32 version of tick module.
Download the compressed file and, with Firewing IDE opened, dragged it on editor window.. this copy the module in your "UserLibrary\MCU32" folder.
Coccoliso wrote:Hi,
here a FW-32 version of tick module.
Download the compressed file and, with Firewing IDE opened, dragged it on editor window.. this copy the module in your "UserLibrary\MCU32" folder.
module Tick
Imports ISR
private const _osc = _clock // alias for Code Explorer
private const _fosc as uinteger = _osc * 1000000 // instructions per second
''private const _fosc as uinteger = _osc * 1000000 / 2 // instructions per second (originale)
'private const _ms as ushort = _fosc / 1000 // instructions every 1 ms
#if _clock = 40
private const _ms as single = _fosc / 1000 ' instructions every 1 ms (no prescaler)
private const _pr = 0
#elseif _clock = 80
private const _ms as single = _fosc / 1000 / 8 ' instructions every 1 ms (1:8 prescaler)
private const _pr = 16
#else
#warning "_prescalerValue not set for this speed in SingleTimer.bas"
#endif
private _ticks as uinteger = 0 // increment every millisecond
// interrupt will increment tick every millisecond...
private Interrupt OnTimer(TIMER_2_VECTOR, ipl1Software)
_ticks += 1 ' increment tick
Isr.SetFlag(TIMER_2_VECTOR, 0)
End Interrupt
public function Time() as uinteger
disable(OnTimer)
Time = _ticks
enable(OnTimer)
end function
// alias for code compatability with existing MQTT module
public dim ms as Time
// init module...
sub main()
isr.SetPriority(TIMER_2_VECTOR, ipl1)
isr.SetSubPriority(TIMER_2_VECTOR,0)
T2CON = _pr ' clear Timer 2 - no presccale
PR2 = _ms ' set timer period
Isr.SetFlag(TIMER_2_VECTOR, 0) ' clear timer interrupt flag
T2CON.15 = 1 ' start timer
enable(OnTimer)
end sub
End module
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
Users browsing this forum: No registered users and 1 guest