Tick module for Firewing 16

Posted:
Thu Apr 02, 2015 10:39 am
by Timbo
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
Re: Tick module for Firewing 16

Posted:
Thu Apr 02, 2015 11:41 am
by Coccoliso
I use this module for MQTT library recovered from a module written by David Barker:

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)
Re: Tick module for Firewing 16

Posted:
Thu Apr 02, 2015 4:53 pm
by Jerry Messina
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.
Re: Tick module for Firewing 16

Posted:
Thu Apr 02, 2015 9:05 pm
by Timbo
HI,
Yes that is very valid.
Tim
Re: Tick module for Firewing 16

Posted:
Fri Apr 03, 2015 10:33 am
by Jerry Messina
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