Skip to main content
Skip table of contents

10.3 - Basic Counter and Timer Setup (Applies to UD-Series)

To use the LabJack timers or counters you need to do some very basic setup.

Note: to enable this guide and the corresponding samples to work with all LabJacks, we use the System Clock (tcSYS) and a PIN_OFFSET of 4. Depending on your hardware, you should feel free to use other clocks and other pin offsets.

First, by default, once you enable a timer or counter, it will replace FIO0. If you'd prefer to keep FIO0 for analog or digital I/O, you can use the LJ_chTIMER_COUNTER_PIN_OFFSET, to select a different pin:

AddRequest (ID, LJ_ioPUT_CONFIG, LJ_chTIMER_COUNTER_PIN_OFFSET, 1, 0, 0)

The above, after a GoOne(ID), will set put the first enabled timer or counter on FIO1 instead of FIO0. The general form of the command is:

LabJack ID, LJ_ioPUT_CONFIG, LJ_chTIMER_COUNTER_PIN_OFFSET, FIO pin
# for first timer/counter, 0, 0

Please note that as of hardware revision 1.3 of the U3, timers and counters will start at pin offset 4. Therefore a pin offset of 0 to 4 will all result in FIO4 being the first timer. A pin offset of 5 will result in FIO5 being the first timer. FIO0 through FIO3 will no longer be usable as timers or counters.

Counters:

To use counters, all you really need to do is enable the counter. This is done with LJ_ioPUT_COUNTER_ENABLE:

AddRequest(ID, LJ_ioPUT_COUNTER_ENABLE,0,1,0,0)

This will enable the first counter. To disable, do the same thing, but change the 1 to a 0. The general form of this command is:

Labjack ID, LJ_ioPUT_COUNTER_ENABLE, Counter #, Enable (1) or Disable (0), 0, 0

Once the counter is enabled, you can read the counter using a channel, putting the ID in for the D#, select an I/O type of Counter, and putting the counter number for the channel number. If you are looking for the number of counts in a certain time period, please make sure and read the next section on resetting the counter.

Sample file: LJGuideSamples\BasicCounter.ctl

Timers:

Timers are slightly more complicated, mainly because they are a lot more flexible. There are a number of different timer modes and each has its own parameters and setup which is described in the following sections. A few common points though:

Like counters, you'll need to first enable the timers. The function to do so is very similar to counters except you are specifying how many timers to enable rather than enabling a specific timer. So, to enable two timers:

AddRequest(ID, LJ_ioPUT_CONFIG, LJ_chNUMBER_TIMERS_ENABLED, 2, 0, 0)

After you have enabled the timers, you'll need to set which mode you'd like to use for each timer. For example:

AddRequest(ID, LJ_ioPUT_TIMER_MODE, 0, LJ_tmPWM8, 0, 0)

will set the Timer mode of timer 0 to PWM8. The general form of this command is:

LabJack ID, LJ_ioPUT_TIMER_MODE, Timer #, Timer mode code, 0, 0

Possible Timer modes as of this writing include:

LJ_tmPWM16 // 16 bit PWMLJ_tmPWM8 // 8 bit PWMLJ_tmRISINGEDGES32 // 32-bit rising to rising edge measurementLJ_tmFALLINGEDGES32 // 32-bit falling to falling edge measurementLJ_tmDUTYCYCLE // duty cycle measurementLJ_tmFIRMCOUNTER // firmware based rising edge counterLJ_tmFIRMCOUNTERDEBOUNCE // firmware counter with debounceLJ_tmFREQOUT // frequency outputLJ_tmQUAD // QuadratureLJ_tmTIMERSTOP // stops another timer after n pulsesLJ_tmSYSTIMERLOW // read lower 32-bits of system timerLJ_tmSYSTIMERHIGH // read upper 32-bits of system timerLJ_tmRISINGEDGES16 // 16-bit rising to rising edge measurementLJ_tmFALLINGEDGES16 // 16-bit falling to falling edge measurement

Not all modes may be supported by all LabJacks. Please see the file LabJackUD.h in your LabJack installation directory for any new modes.

Finally, you'll probably need to set the clock base and divisor that the timer will use:

AddRequest(ID, LJ_ioPUT_CONFIG, LJ_chTIMER_CLOCK_BASE, LJ_tc24MHZ_DIV, 0, 0)
AddRequest(ID, LJ_ioPUT_CONFIG, LJ_chTIMER_CLOCK_DIVISOR, 24, 0, 0)

These both follow the standard form of PUT_CONFIG:

LabJack ID, LJ_ioPUT_CONFIG, Parameter, Value, 0, 0

The clock divisor is an integer. For timer base, there are several constants defined:

LJ_tc750KHZ // UE9: 750 khzLJ_tcSYS // UE9: system clockLJ_tc2MHZ // U3: Hardware Version 1.20 or lowerLJ_tc6MHZ // U3: Hardware Version 1.20 or lowerLJ_tc24MHZ // U3: Hardware Version 1.20 or lowerLJ_tc500KHZ_DIV // U3: Hardware Version 1.20 or lowerLJ_tc2MHZ_DIV // U3: Hardware Version 1.20 or lowerLJ_tc6MHZ_DIV // U3: Hardware Version 1.20 or lowerLJ_tc24MHZ_DIV // U3: Hardware Version 1.20 or lowerLJ_tc4MHZ // U3: Hardware Version 1.21 or higherLJ_tc12MHZ // U3: Hardware Version 1.21 or higherLJ_tc48MHZ // U3: Hardware Version 1.21 or higherLJ_tc1MHZ_DIV // U3: Hardware Version 1.21 or higherLJ_tc4MHZ_DIV // U3: Hardware Version 1.21 or higher
LJ_tc12MHZ_DIV // U3: Hardware Version 1.21 or higherLJ_tc48MHZ_DIV // U3: Hardware Version 1.21 or higher

Once again, not all clock bases are supported by all LabJacks and you should check the LabJackUD.h file for any new bases.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.