Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
nz_tick.h File Reference

Tick Functions. More...

Go to the source code of this file.

Macros

#define __INLINE_FUNCTION__   extern inline __attribute__((always_inline))
 
#define nzINT_PRIORITY_TICK   ( 5 )
 
#define nzINT_PRIORITY_UTICK   ( 6 )
 
#define NZSYS_TICK_TMR2AND3_UTICK   ( 0 )
 
#define tickService()
 
#define tick16TestTmr(tmr)   ( (((((tmr) - (tick_val.w[0]) ) & 0x8000))==0) ? 0 : 1)
 
#define tick16SetTmrMS(tmr, msVal)   (tmr = (tick16Get() + msVal))
 
#define tick16GetElapsedMS(oldTick)   ((WORD)((tick_val.w[0]) - (oldTick)))
 
#define tick16UpdateTmrMS(tmr, msVal)   (tmr += msVal)
 
#define tick16ConvertFromMS(msVal)   (msVal)
 
#define tick32Get_noDisi()   (tick_val.Val)
 
#define tick32SetTmrMS(tmr, msVal)   (tmr = (tick32Get() + msVal))
 
#define tick32TestTmr_noDisi(tmr)   ((((tmr-tick_val.Val)&0x80000000)==0) ? 0 : 1)
 
#define tick32UpdateTmrMS(tmr, msVal)   (tmr += msVal)
 
#define tick32UpdateTmrSec(tmr, secVal)   (tmr += (((DWORD)secVal)*1000))
 
#define tick32UpdateTmrMin(tmr, minVal)   (tmr += ( ((DWORD)minVal)*60000))
 
#define tick32ConvertFromMS(msVal)   ((DWORD)(msVal))
 
#define utick16TestTmr(tmr)   ( (((((tmr) - (utick16Get()) ) & 0x8000))==0) ? 0 : 1)
 
#define utick16GetElapsedUS(oldTick)   utick16ConvertToUS(((WORD)((utick16Get()) - (oldTick))))
 
#define utick16ConvertToUS(utickVal)   ( ((WORD)utickVal) / ((WORD)(NZ_UTICKS_PER_MS/1000)) )
 
#define utick16ConvertFromUS(usVal)   (WORD)( ((WORD)usVal) * (NZ_UTICKS_PER_MS/1000) )
 
#define utick32TestTmr(tmr)   ((((tmr-utick32Get())&0x80000000)==0) ? 0 : 1)
 
#define utick32ConvertToUS(utickVal)   ( ((DWORD)utickVal) / ((DWORD)(NZ_UTICKS_PER_MS/1000)) )
 
#define utick32ConvertFromUS(usVal)   (DWORD)( ((DWORD)usVal) * (NZ_UTICKS_PER_MS/1000) )
 

Typedefs

typedef WORD TICK16
 
typedef DWORD TICK32
 

Functions

void tickInit (void)
 
TICK16 tick16Get (void)
 
DWORD tick32Get (void)
 
BOOL tick32TestTmr (DWORD tmr)
 
WORD utick16Get (void)
 
WORD utick16Get_noDisi (void)
 
DWORD utick32Get (void)
 
DWORD utick32Get_noDisi (void)
 
WORD tick16Get_8us (void)
 
WORD tick16Get_8us_noDisi (void)
 
WORD tick32Get_8us (void)
 
WORD tick32Get_8us_noDisi (void)
 

Variables

volatile DWORD_VAL tick_val
 

Detailed Description

Tick Functions.

Author
Modtronix Engineering
Compiler:
MPLAB XC16 compiler

Description

To use this module:

The system tick uses Timer 1, and has a period of 1ms. Each 1ms an interrupt is triggered, and the 32-bit system tick is incremented. To make code smaller and more efficient, default tick functions use the lower 16 bits of the 32-bit system tick. This limits the default functions to a maximum time of 32,768ms = 32.7 seconds. If this is not sufficient, the 32-bit tick functions (tick32Xxx) can be used, which have a maximum time of 4,294,967,296 ms = 1,193 hours = 49.7 Days.

The 16-bit variants are the most efficient, and create the smallest and fastest code. Only use the 32-bit variants when required!

Example default tick (16-bit, 1ms). Can be used for a maximum of 32,768 ms = 32 seconds delay. Requires 16-bit timer variable, which is the native data width of the processor! Produces fastest code.

WORD tmrFlashLed=0; //16-bit, 1ms Timer
tmrFlashLed = tick16Get(); //Set with current 16-bit tick
//Do something every 200ms
if (tick16TestTmr(tmrFlashLed)) {
tick16UpdateTmrMS(tmrFlashLed, 200); //Update timer to expire in 200ms again
..... Do Something ....
}

An alternative method is:

WORD tmrFlashLed=0; //16-bit, 1ms Timer
tmrFlashLed = tick16Get(); //Set with current 16-bit tick
//Do something every 200ms (IMPORTANT - ensure value is not more than 32,767!)
if (tick16TestTmr(tmrFlashLed + tick16ConvertFromMS(200))) {
tmrFlashLed = tick16Get(); //Undate with current 16-bit tick
//..... Do Something ....
}

Example using 32-bit, 1ms tick. Can be used for a maximum of 4,294,967,296 ms = 1,193 hours = 49.7 Days delay. Requires 32-bit timer variable, generates more code, and is slower than other 8 and 16-bit functions.

DWORD tmr32Task1Hour=0; //Do something every hour (32-bit, 1ms Timer)
tmr32Task1Hour = tick32Get(); //Set with current 32-bit tick.
//Do something each hour = 3,600 seconds = 3,600,000 ms
if (tick32TestTmr(tmr32Task1Hour)) {
tick32UpdateTmrMin(tmr32Task1Hour, 60); //Update timer to expire in 60 minutes
//..... Add code here to be executed every hour
}

Configuration

The following defines are used to configure this module, and should be placed in projdefs.h. Note that all items marked [-DEFAULT-] are defaults, and do not have to be placed in projdefs.h if they contain desired configuration! For details, see Project Configuration.

// *********************************************************************
// -------------- Tick Configuration (from nz_tick.h) ------------------
// *********************************************************************
//Define the interrupt priority level for system tick(TMR1), 1-7 (7 is highest)
#define nzINT_PRIORITY_TICK ( 5 ) //[-DEFAULT-]
//Define the interrupt priority level for TMR2 & TMR3 if uTick is configured to use them
#define nzINT_PRIORITY_UTICK ( 6 ) //[-DEFAULT-]
//Use Timer 2 and 3 for a dedicated uTick counter - for utickXxx() functions
#define NZSYS_TICK_TMR2AND3_UTICK ( 0 ) //[-DEFAULT-]

Software License Agreement

The software supplied herewith is owned by Modtronix Engineering, and is protected under applicable copyright laws. The software supplied herewith is intended and supplied to you, the Company customer, for use solely and exclusively on products manufactured by Modtronix Engineering. The code may be modified and can be used free of charge for commercial and non commercial applications. All rights are reserved. Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws, as well as to civil liability for the breach of the terms and conditions of this license.

THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.

File History

2012-08-08, David Hosken (DH):

  • Initial version

Macro Definition Documentation

#define tick16ConvertFromMS (   msVal)    (msVal)
Function:
void tick16ConvertFromMS(WORD msVal)

Convert given value to a tick millisecond value. Seeing that the system tick is 1ms, no conversion is required (1 to 1).

For example, to initialize a timer with a value that will expire in 500 ms, following code can be used.

WORD tmrStartupDelay; //Startup delay
tmrStartupDelay = tick16Get() + tick16ConvertFromMS(500);
while() {
if (tick16TestTmr(tmrStartupDelay)) {
tick16UpdateTmrMS(tmrStartupDelay, 100); //Update timer to expire in 100ms again
// Do something ......
}
}
Parameters
msValValue to convert to a tick milliseconds value
Examples:
ow_ds2482_async_debug/main.c.
#define tick16GetElapsedMS (   oldTick)    ((WORD)((tick_val.w[0]) - (oldTick)))
Function:
WORD tick16GetElapsedMS(TICK16 oldTick)

Returns the time in milliseconds that has elapsed since the given old tick value.

For example, to calculate how long some code takes:

TICK16 timeStart;
timeStart = tick16Get();
//Do something that takes time....
//Print the time taken to the debug console
debugPutString("\nTime taken in ms = ");
debugPutWord(tick16GetElapsedMS(timeStart));
Parameters
oldTickOld TICK16 value
Returns
Returns elapsed time in milliseconds
#define tick16SetTmrMS (   tmr,
  msVal 
)    (tmr = (tick16Get() + msVal))
Function:
void tick16SetTmrMS(TICK16 tmr, WORD msVal)

Set the given timer to a time in the future, as given by msVal parameter in milliseconds.

Parameters
tmrTimer to add set expirty time for
msValAmount of milliseconds in which the timer will expire
Examples:
sensor_dht11_dht22_block/main.c.
#define tick32ConvertFromMS (   msVal)    ((DWORD)(msVal))
Function:
void tick32ConvertFromMS(DWORD msVal)

Convert given value to a tick millisecond value. Seeing that the system tick is 1ms, no conversion is required (1 to 1).

For example, to initialize a timer with a value that will expire in 500 ms, following code can be used.

WORD tmr32StartupDelay; //Startup delay
tmr32StartupDelay = tick32Get() + tick32ConvertFromMS(500);
while() {
if (tick32TestTmr(tmr32StartupDelay)) {
tick32UpdateTmrMS(tmr32StartupDelay, 100); //Update timer to expire in 100ms again
// Do something ......
}
}
Parameters
msValValue to convert to a tick milliseconds value
#define tick32Get_noDisi ( )    (tick_val.Val)

Same as tick32Get() but does not disable interrupts. Only call this function from an ISR with higher priority than the system tick. That is an ISR with priority 5 - 7, for default system tick with priority 4.

#define tick32SetTmrMS (   tmr,
  msVal 
)    (tmr = (tick32Get() + msVal))
Function:
void tick32SetTmrMS(TICK32 tmr, WORD msVal)

Set the given timer to a time in the future, as given by msVal parameter in milliseconds.

Parameters
tmrTimer to add set expirty time for
msValAmount of milliseconds in which the timer will expire
#define tick32TestTmr_noDisi (   tmr)    ((((tmr-tick_val.Val)&0x80000000)==0) ? 0 : 1)

Same as tick32TestTmr, but does not disable interrupts Only call this function from an ISR with higher priority than the system tick. That is an ISR with priority 5 - 7, for default system tick with priority 4.

Parameters
tmrGiven value to compare it
#define tick32UpdateTmrMin (   tmr,
  minVal 
)    (tmr += ( ((DWORD)minVal)*60000))

Update the given timer to expire in the given number of minutes.

Parameters
tmrTimer to add the given milliseconds too
minValAmount of minutes to add to timer
Examples:
tick_demo1/main.c, and tick_demo1_complex/main.c.
#define tick32UpdateTmrMS (   tmr,
  msVal 
)    (tmr += msVal)
Function:
void tick32UpdateTmrMS(TICK32 tmr, WORD msVal)

Add the given amount of milliseconds to the given timer. Updates the time when it will expire.

The given time is added to the current timer value! Ensure current timer has a valid value before calling this function!

Parameters
tmrTimer to add the given milliseconds too
msValAmount of milliseconds to add to timer
#define tick32UpdateTmrSec (   tmr,
  secVal 
)    (tmr += (((DWORD)secVal)*1000))

Update the given timer to expire in the given amount of seconds.

The given time is added to the current timer value! Ensure current timer has a valid value before calling this function!

Parameters
tmrTimer to add the given milliseconds too
secValAmount of seconds to add to timer
#define tickService ( )
Value:
{ \
/* Increment tick */ \
tick_val.Val++; \
}

Increments all tick values when called

#define utick16ConvertFromUS (   usVal)    (WORD)( ((WORD)usVal) * (NZ_UTICKS_PER_MS/1000) )
Function:
WORD utick16ConvertFromUS(WORD usVal)

Converts the given micro second value to system "16-bit Micro Tick".

Parameters
usValThe micro second value
Returns
The number of system ticks for the given micro tick value
#define utick16ConvertToUS (   utickVal)    ( ((WORD)utickVal) / ((WORD)(NZ_UTICKS_PER_MS/1000)) )
Function:
WORD utick16ConvertToUS(WORD utickVal)

Convert given utick value to micro seconds.

Parameters
utickValValue to convert to micro seconds
#define utick16GetElapsedUS (   oldTick)    utick16ConvertToUS(((WORD)((utick16Get()) - (oldTick))))
Function:
TICK16 utick16GetElapsedUS(TICK16 oldTick)

Returns the time in microseconds that has elapsed since the given old tick value. Seeing that this is a 16-bit function, maximum value of difference in uticks is 32,768. For the default SBC66 boards there are 2 ticks per US (2000 ticks per millisecond), meaning maximum elapsed time that can be measured is 32,768/2 = 16,384

For example, to calculate how long some code takes:

TICK16 utickStart;
utickStart = utick16Get();
//Do something that takes time....
//Print the time taken to the debug console
debugPutString("\nTime taken in us = ");
debugPutWord(utick16GetElapsedUS(utickStart));
Parameters
oldTickOld TICK16 value
Returns
Returns elapsed time in microseconds
#define utick16TestTmr (   tmr)    ( (((((tmr) - (utick16Get()) ) & 0x8000))==0) ? 0 : 1)
Function:
BOOL utick16TestTmr(TICK16 tmr)

Tests if the current 16-bit utick(micro tick) has passed the given timer's value.

Will return true once the 16 bit utick is > given value. Can have a maximum value of 32,768

Parameters
tmrTimer to test
Returns
Returns TRUE if given timer has expired, else FALSE
#define utick32ConvertFromUS (   usVal)    (DWORD)( ((DWORD)usVal) * (NZ_UTICKS_PER_MS/1000) )
Function:
DWORD utick32ConvertFromUS(DWORD usVal)

Converts the given micro second value to system "32-bit Micro Tick".

Parameters
usValThe micro second value
Returns
The number of system ticks for the given micro tick value
#define utick32ConvertToUS (   utickVal)    ( ((DWORD)utickVal) / ((DWORD)(NZ_UTICKS_PER_MS/1000)) )
Function:
DWORD utick32ConvertToUS(DWORD utickVal)

Convert given utick value to micro seconds.

Parameters
utickValValue to convert to micro seconds
#define utick32TestTmr (   tmr)    ((((tmr-utick32Get())&0x80000000)==0) ? 0 : 1)
Function:
BOOL utick32TestTmr(TICK32 tmr)

Tests if the current 32-bit utick(micro tick) has passed the given timer's value.

For efficiency and code size, try to use utick16 functions where possible (utick16TestTmr() for example).

Will return true once the 32 bit tick is > given value. Can have a maximum value of 2,147,483,648

Parameters
tmrTimer to test
Returns
Returns TRUE if given timer has expired, else FALSE

Function Documentation

TICK16 tick16Get ( void  )
inline

Current default tick, which is the 16-bit, 1ms tick. Has a resolution of 1ms. Has a maximum value of 32,768ms = 32.7 seconds

Returns
The current 16-bit, 1ms Tick.
Examples:
adc_lcd/main.c, i2c1_lcd2s_txt_key/main.c, i2c_debug_demo/main.c, ow_ds2482_async_debug/main.c, ow_ds2482_demo1_debug/main.c, port_read_write/main.c, pt66din6_debug_demo/main.c, pwm_demo1/main.c, sensor_dht11_dht22/main.c, sensor_dht11_dht22_block/main.c, tick_demo1/main.c, and tick_demo1_complex/main.c.
WORD tick16Get_8us ( void  )

Get the current 16 bit, 8us tick. Has a resolution of 8us. Has a maximum value of 262,144us = 262.1 milliseconds

Returns
The current 16-bit, 8us Tick.
WORD tick16Get_8us_noDisi ( void  )
inline

Same as tick16Get_8us(), but does NOT disable interrupts! Only call this function from an ISR with higher priority than the system tick. That is an ISR with priority 5 - 7, for default system tick with priority 4.

Returns
The current 16-bit, 8us Tick.
Examples:
sensor_dht11_dht22/main.c.
DWORD tick32Get ( void  )

Get the current 32-bit, 1ms Tick. Can have a maximum value of: 4,294,967,296 ms = 1,193 hours = 49.7 Days

Returns
The current 32-bit, 1ms Tick.
Examples:
tick_demo1/main.c, and tick_demo1_complex/main.c.
WORD tick32Get_8us ( void  )

Get the current 32 bit, 8us tick. Has a resolution of 8us. Has a maximum value of 17,179 sec = 286 Minutes = 4.77 Hours

Returns
The current 32-bit, 8us Tick.
WORD tick32Get_8us_noDisi ( void  )
inline

Get the current 32 bit, 8us tick. Has a resolution of 8us. Has a maximum value of 17,179 sec = 286 Hours = 11.9 Days Only call this function from an ISR with higher priority than the system tick. That is an ISR with priority 5 - 7, for default system tick with priority 4.

Returns
The current 32-bit, 8us Tick.
BOOL tick32TestTmr ( DWORD  tmr)
Function:
BOOL tick32TestTmr(TICK32 tmr)

Tests if the current 32-bit(1ms) tick has passed the given timer's value.

For efficiency and code size, try to use tick16 functions where possible (tick16TestTmr() for example).

Will return true once the 32 bit(1ms) tick is > given value. Tick is a 1ms counter. Can have a maximum value of: 4,294,967,296 ms = 1,193 hours = 49.7 Days

Parameters
tmrTimer to test
Returns
Returns TRUE if given timer has expired, else FALSE
Examples:
tick_demo1/main.c, and tick_demo1_complex/main.c.
void tickInit ( void  )

Initializes tick values

Examples:
ledflash_int/main.c.
WORD utick16Get ( void  )

A "Micro Tick" has a resolution of 1us or less. It will always have a whole number of ticks per millisecond, as defined by NZ_UTICKS_PER_MS. The following are common values: 16MHz timer with 8 prescaler = 0.5us resolution = NZ_UTICKS_PER_MS = 2000 60MHz timer with 8 prescaler = 0.13333us resolution = NZ_UTICKS_PER_MS = 7500 72MHz timer with 8 prescaler = 0.88888us resolution = NZ_UTICKS_PER_MS = 1125 80MHz timer with 8 prescaler = 0.1us resolution = NZ_UTICKS_PER_MS = 10000 80MHz timer with 64 prescaler = 0.8us resolution = NZ_UTICKS_PER_MS = 800 120MHz timer with 64 prescaler = 0.53333us resolution = NZ_UTICKS_PER_MS = 1875 Get the current 16 bit, micro tick. The NZ_UTICKS_PER_MS defines how many micro ticks there are per milli second. For current range of SBC66 boards it is always 2000.

For NZ_UTICKS_PER_MS=1000, Has a maximum value of 32,768us = 32.7 milliseconds For NZ_UTICKS_PER_MS=2000, Has a maximum value of 16,384us = 16 milliseconds

Returns
The current 16-bit, Micro Tick.
WORD utick16Get_noDisi ( void  )
inline

Same as utick16Get(), but does not disable interrupts. Only call this function from an ISR with higher priority than the system tick. That is an ISR with priority 5 - 7, for default system tick with priority 4.

Returns
The current 16-bit, Micro Tick.
DWORD utick32Get ( void  )

Get the current 32 bit, micro tick. The NZ_UTICKS_PER_MS defines how many micro ticks there are per milli second. For current range of SBC66 boards it is always 2000.

For NZ_UTICKS_PER_MS=1000, Has a maximum value of 2,147 Seconds = 35.7 Minutes For NZ_UTICKS_PER_MS=2000, Has a maximum value of 1,073 Seconds = 17.9 Minutes For NZ_UTICKS_PER_MS=10000, Has a maximum value of 214 Seconds = 3.57 Minutes

Returns
The current 32-bit, Micro Tick.
DWORD utick32Get_noDisi ( void  )
inline

Same as utick32Get(), but does not disable interrupts. Only call this function from an ISR with higher priority than the system tick. That is an ISR with priority 5 - 7, for default system tick with priority 4.

Returns
The current 32-bit, Micro Tick.

Variable Documentation

volatile DWORD_VAL tick_val

System 32-bit tick. Be very carefull if using this varialble. Ensure all operations are atomic. If not sure, use SETTO_XX macros below!

Examples:
ledflash_int/main.c.