Tick module for Firewing 16

Discuss the Firewing language

Tick module for Firewing 16

Postby Timbo » Thu Apr 02, 2015 10:39 am

Hi,

Doe anyone have a tick module for Firewing16?

I find it very useful and want to use on the 16 series device.


Thanks

Tim
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Tick module for Firewing 16

Postby Coccoliso » Thu Apr 02, 2015 11:41 am

I use this module for MQTT library recovered from a module written by David Barker: :D

Tick.Bas module
Code: Select all
module Tick

private const _fosc as single = _clock * 1000000 / 2     // instructions per second
private const _ms as single = _fosc / 1000               // instructions every 1 ms
public ms as uinteger = 0                                // increment every millisecond

// interrupt will increment tick every millisecond...
private Interrupt OnTimer(Pic.T2Interrupt)   
   ms += 1                  ' increment tick       
   IFS0.7 = 0               ' clear interrupt flag
End Interrupt

// init module...
sub main()
   enable(OnTimer)
   T2CON = 0                ' clear Timer 1 - no presccale
   PR2 = _ms                ' set timer period
   IFS0.7 = 0               ' clear timer interrupt flag     
   T2CON.15 = 1             ' start timer
end sub

End module


My use example:

Code: Select all
// timeout variable...
private _timeoutValue                   as uinteger = 0
private _lastTime                       as uinteger = 0

'****************************************************************************
'* Name    : SetKeepAliveTimeout                                            *
'* Purpose : Set keepalive timeout value                                    *
'****************************************************************************
private sub SetKeepAliveTimeout(value as uinteger)
   _timeoutValue = value
   _lastTime = tick.ms
end sub

'****************************************************************************
'* Name    : KeepAliveTimeout                                               *
'* Purpose : Check to see if keepalive timeout ocurred                      *
'****************************************************************************
private function KeepAliveTimeout() as boolean
   return (tick.ms - _lastTime) >= _timeoutValue
end function


Testing timeout for do something..
Code: Select all
    If KeepAliveTimeout() Then
        // do something
        SetKeepAliveTimeout((mKeepAliveSeconds * 1000))
    End If


First setting before previous timeout test..
Code: Select all
    SetKeepAliveTimeout(mKeepAliveSeconds * 1000)
User avatar
Coccoliso
 
Posts: 174
Joined: Sat Sep 27, 2014 10:02 am

Re: Tick module for Firewing 16

Postby Timbo » Thu Apr 02, 2015 12:17 pm

Brilliant!

Thanks

Tim
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Tick module for Firewing 16

Postby Jerry Messina » Thu Apr 02, 2015 4:53 pm

There's an issue with using that code.

The variable Tick.ms is an unsigned 32-bit integer, so it takes multiple instructions to read its value.
If an interrupt occurs during that time it's possible that the value returned will be incorrect.

Let's say that Tick.ms = $0000FFFF. If you read the lower word, get an interrupt, and then return to finish reading the upper word you'll get the value $0001FFFF returned for Tick.ms when in actuality it should be $00010000.

Two of the simpler ways to deal with this:
- disable the tick timer intr, read tick.ms, and then re-enable the interrupt
or
- when you go to read tick.ms, read it in a loop until two successive values are the same.
Jerry Messina
 
Posts: 278
Joined: Thu Feb 14, 2013 10:16 am

Re: Tick module for Firewing 16

Postby Timbo » Thu Apr 02, 2015 9:05 pm

HI,

Yes that is very valid.


Tim
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Tick module for Firewing 16

Postby Jerry Messina » Fri Apr 03, 2015 10:33 am

You could change it so that it's compatible w/both the existing MQTT library and the MCU08 Tick.bas module...

Code: Select all
module Tick

private const _osc = _clock                             // alias for Code Explorer
private const _fosc as uinteger = _osc * 1000000 / 2     // instructions per second
private const _ms as ushort = _fosc / 1000               // instructions every 1 ms

private _ticks as uinteger = 0                             // increment every millisecond

// interrupt will increment tick every millisecond...
private Interrupt OnTimer(Pic.T2Interrupt)   
   _ticks += 1               ' increment tick       
   IFS0.7 = 0               ' clear interrupt flag
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()
   T2CON = 0                ' clear Timer 1 - no presccale
   PR2 = _ms                ' set timer period
   IFS0.7 = 0               ' clear timer interrupt flag     
   T2CON.15 = 1             ' start timer
   enable(OnTimer)
end sub

End module
Jerry Messina
 
Posts: 278
Joined: Thu Feb 14, 2013 10:16 am

Re: Tick module for Firewing 16

Postby Coccoliso » Fri Apr 03, 2015 11:00 am

Tanks Jerry ;)
User avatar
Coccoliso
 
Posts: 174
Joined: Sat Sep 27, 2014 10:02 am


Return to Language

Who is online

Users browsing this forum: No registered users and 4 guests

cron

x