Tick.BAS module for Firewing 32

Share code examples with other users

Tick.BAS module for Firewing 32

Postby Coccoliso » Thu Oct 19, 2017 9:28 am

Hi,

here
Tick-32.zip
Tick.BAS
(763 Bytes) Downloaded 1058 times
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.
User avatar
Coccoliso
 
Posts: 177
Joined: Sat Sep 27, 2014 10:02 am

Re: Tick.BAS module for Firewing 32

Postby funlw65 » Fri Jul 30, 2021 7:07 pm

Coccoliso wrote:Hi,

here
Tick-32.zip
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.


Archive is corrupted! Can you re-upload it, please?
Thank you!
funlw65
 
Posts: 36
Joined: Thu Jan 16, 2014 2:32 pm

Re: Tick.BAS module for Firewing 32

Postby Coccoliso » Fri Jul 30, 2021 9:07 pm

Hi,
the original zip file I don't have anymore ..
below the source that I have in my folder "UserLibrary\MCU32" named tick.bas

Code: Select all
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

User avatar
Coccoliso
 
Posts: 177
Joined: Sat Sep 27, 2014 10:02 am

Re: Tick.BAS module for Firewing 32

Postby funlw65 » Fri Jul 30, 2021 9:10 pm

Thank you very much!
funlw65
 
Posts: 36
Joined: Thu Jan 16, 2014 2:32 pm

Re: Tick.BAS module for Firewing 32

Postby Coccoliso » Fri Jul 30, 2021 9:11 pm

to be sure, take a look at the clock division.
The module as it is worked for me but it's been a long time
..and I see a "SingleTimer.bas" :roll:
User avatar
Coccoliso
 
Posts: 177
Joined: Sat Sep 27, 2014 10:02 am

Re: Tick.BAS module for Firewing 32

Postby Coccoliso » Fri Jul 30, 2021 9:23 pm

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
User avatar
Coccoliso
 
Posts: 177
Joined: Sat Sep 27, 2014 10:02 am

Re: Tick.BAS module for Firewing 32

Postby funlw65 » Fri Jul 30, 2021 10:12 pm

Thank you, much appreciated!
funlw65
 
Posts: 36
Joined: Thu Jan 16, 2014 2:32 pm


Return to Code Examples

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x