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

Tick Functions, Complex version! For the simple version, use nz_tick.h and nz_tick.c files. More...

#include "nz_tick.h"

Go to the source code of this file.

Macros

#define getTick16bit_256ms()   tick16Get_256ms()
 
#define tick16Update_256ms(tmr, msVal)   (tmr += msVal)
 
#define tick8Get_1ms()   (tick_val.v[0])
 
#define tick8HasExpired_1ms(v)   ((v - TICK.v[0]) & 0x80)
 
#define tick8HasExpired_16ms(v)   ((v - ((UINT8)(tick_val.w[0]>>4))) & 0x80)
 
#define tick8Get_256ms()   (tick_val.v[1])
 

Functions

WORD tick16Get_256ms (void)
 
BOOL tick16HasExpired_256ms (WORD v)
 
BYTE tick8Get_16ms (void)
 

Detailed Description

Tick Functions, Complex version! For the simple version, use nz_tick.h and nz_tick.c files.

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, various functions are available for using 8, 16 and 32-bit variables for implementing timers and delays. The 16-bit variants are the most efficient, and create the smallest code, seeing that the native data size for this CPU is 16-bits. The 8-bit variants use the least RAM. The 32-bit variables can create the longest delays.

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 ....
}

Example using 8-bit, 16ms tick. Can be used for a maximum of 1,024ms = 1 second delay. Requires only 8-bit timer variable, but has small maximum delay of 1 second. Can use 8-bit 256ms timer for longer delay with 8 bit timer.

WORD tmr8bit16ms_ToggleX5=0; //8-bit, 16ms Timer
tmr8bit16ms_ToggleX5 = tick8Get_16ms(); //Set with current 8-bit, 16 ms tick
//Do something every 800ms
if (tick8HasExpired_16ms(tmr8bit16ms_ToggleX5)) {
tick8_16ms_Update(tmr8bit16ms_ToggleX5, 800); //Set to trigger again in 50x16 = 800ms
LAT_05 = !LAT_05; //Toggle state of port 5 (old port name X5)
}

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
}

Example using 16-bit, 256ms tick. Can be used for a maximum of 8,338 seconds = 2.3 Hours delay. Requires 16-bit timer variable, which is the native data width of the processor! Produces fastest code.

WORD tmr16bit256ms_Task1Hour=0; //Do something (approximately) every hour (16-bit, 256ms Timer)
tmr16bit256ms_Task1Hour = tick16Get_256ms(); //Set with current 16-bit tick, 256 ms tick
//Do something (approximately) each hour = 3,600 seconds = 3,600,000 ms
if (tick16HasExpired_256ms(tmr16bit256ms_Task1Hour)) {
//Set to trigger again in 1 hour (3,600,000 ms) = 3,600,000/256 = 14,062 (actually 3,599,872)
tick16Update_256ms(tmr16bit256ms_Task1Hour, 14062);
//..... 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_tickCx.h) ------------------
// *********************************************************************
//Indicates that the complex tick file nz_tickCx must be used, and not nz_tick
#define NZSYS_ENABLE_COMPLEX_TICK

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 getTick16bit_256ms ( )    tick16Get_256ms()

Alternative function name for tick16Get_256ms(). Gets 16-bit, 256ms tick.

#define tick16Update_256ms (   tmr,
  msVal 
)    (tmr += msVal)

Update the given timer to expire in the given multiple of 256ms. This timer has a 256ms base, so the given value will be multiplied by 256ms. For example, if 5 is given, it will cause the timer to expire in 5x256ms = 1.28 seconds

Parameters
tmrTimer to add the given milliseconds too
msValMultiple of 256ms when timer will exprire!
#define tick8Get_1ms ( )    (tick_val.v[0])

Current system 8-bit tick. Has a resolution of 1ms. Has a maximum value of 128ms

#define tick8Get_256ms ( )    (tick_val.v[1])

Second byte of current system 8-bit tick. Has a resolution of 256ms. Has a maximum value of 32,768ms = 32.7 seconds

#define tick8HasExpired_16ms (   v)    ((v - ((UINT8)(tick_val.w[0]>>4))) & 0x80)

Will return true once the "8 bit tick" is > given value. Can have a maximum value of 1,024ms = 1 second

Parameters
vGiven value to compare it
Examples:
tick_demo1_complex/main.c.
#define tick8HasExpired_1ms (   v)    ((v - TICK.v[0]) & 0x80)

Will return true once the "8 bit tick" is > given value. Can have a maximum value of 128ms

Function Documentation

WORD tick16Get_256ms ( void  )

Get the current 16-bit, 256ms Tick. Has a maximum value 8,338 seconds = 2.3 Hours

Returns
The current 16-bit, 256ms Tick.
Examples:
tick_demo1_complex/main.c.
BOOL tick16HasExpired_256ms ( WORD  v)

Will return true once the 16 bit(256ms) tick is > given value. Tick is a 256ms counter. Can have a maximum value of 8,338 seconds = 2.3 Hours

Parameters
vGiven value to compare it
Examples:
tick_demo1_complex/main.c.
BYTE tick8Get_16ms ( void  )

Get the current 8-bit, 16ms Tick. Has a maximum value of 2,048ms = 2 seconds

Returns
The current 8-bit, 16ms Tick.
Examples:
tick_demo1_complex/main.c.