« Close

Datasheets and User Guides

App Notes

Software & Driver

 

10.3 - Basic Counter and Timer Setup

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 PWM
LJ_tmPWM8                 // 8 bit PWM
LJ_tmRISINGEDGES32        // 32-bit rising to rising edge measurement
LJ_tmFALLINGEDGES32       // 32-bit falling to falling edge measurement
LJ_tmDUTYCYCLE            // duty cycle measurement
LJ_tmFIRMCOUNTER          // firmware based rising edge counter
LJ_tmFIRMCOUNTERDEBOUNCE  // firmware counter with debounce
LJ_tmFREQOUT              // frequency output
LJ_tmQUAD                 // Quadrature
LJ_tmTIMERSTOP            // stops another timer after n pulses
LJ_tmSYSTIMERLOW          // read lower 32-bits of system timer
LJ_tmSYSTIMERHIGH         // read upper 32-bits of system timer
LJ_tmRISINGEDGES16        // 16-bit rising to rising edge measurement
LJ_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 khz
LJ_tcSYS        // UE9: system clock
LJ_tc2MHZ       // U3: Hardware Version 1.20 or lower
LJ_tc6MHZ       // U3: Hardware Version 1.20 or lower
LJ_tc24MHZ      // U3: Hardware Version 1.20 or lower
LJ_tc500KHZ_DIV // U3: Hardware Version 1.20 or lower
LJ_tc2MHZ_DIV   // U3: Hardware Version 1.20 or lower
LJ_tc6MHZ_DIV   // U3: Hardware Version 1.20 or lower
LJ_tc24MHZ_DIV  // U3: Hardware Version 1.20 or lower
LJ_tc4MHZ       // U3: Hardware Version 1.21 or higher
LJ_tc12MHZ      // U3: Hardware Version 1.21 or higher
LJ_tc48MHZ      // U3: Hardware Version 1.21 or higher
LJ_tc1MHZ_DIV   // U3: Hardware Version 1.21 or higher
LJ_tc4MHZ_DIV   // U3: Hardware Version 1.21 or higher 
LJ_tc12MHZ_DIV  // U3: Hardware Version 1.21 or higher
LJ_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.

4 comments

Is it possible to use the timer as a counter? I am not really sure how to go about doing this.

Yes, see timer modes 5 & 6.  So for the U3, look at Sections 2.9.1.5 and 2.9.1.6 of the U3 User's Guide.  Section 10.5 of this DF - LJ App Guide is also useful.

I have a T7 data logger.  On the support page for that it says that the digital I/O port only shows the current state of the port.  So does the counter show more than just a 1 or a 0?  How high can the counter go up to?

The information on this page is for UD devices (U3, U6, UE9) and does not really apply to LJM devices (T7) at all.  Go to the DAQFactory-LJM page, and if you still get stuck post there.