Skip to main content
Skip table of contents

Advanced LJFuse: Timers and Counters

This is an advanced section. Visit it only after you’ve outgrown the material on the LJFuse page.

Requirements

This section requires LJFuse created on 9/16/2010 or later.

Timers and Counters with Modbus

In this section, we will use LJFuse with the timers and counters on a U3. By default, LJFuse does not provide an interface to timers and counters. There are no connections for them in the connection/ subdirectory, and there are no Modbus addresses for them in the modbus/ directory. What we will do in this section is create the files in the modbus/ directory for the necessary Modbus registers. While working through this section, you’ll need a copy of the LabJack Modbus Map:

/support/modbus/ud-modbus

open in a new window.

Accessing the Modbus directory

All the commands in this section work only in the modbus/ subdirectory of a device, so go there first.


# Change to the modbus/ directory
$ cd root-ljfuse/My\ U3-LV/modbus/
$ pwd
/Users/mikec/src/LJFuse/root-ljfuse/My U3-LV/modbus
# View the Modbus registers
$ ls
0 5002 6002 6006 6102 6106
2 6 6003 6007 6103 6107
4 6000 6004 6100 6104 README.txt
5000 6001 6005 6101 6105

Using the counters

Looking at the LabJack Modbus Map (you do have that open, right?), notice that register 50502 is a “Counter Mask”. We can write to that register to enable a counter.

But wait, look at the output of “ls” above. There is no file for 50502. This is the key point of this whole section:

In the modbus/ directory, you may create files for the Modbus addresses that are missing.

In the example below, we create files for 50502, 7300 (the value of Counter 0), and 7302 (the value of Counter 1). To follow this shell example, connect a jumper wire to GND so that you can tap it to FIO4 and FIO5 on a U3.


# Register 50502 is a bit field of which counters to enable
# This enables Counter 0
$ echo 1 > 50502
# Create the file for register 7300
$ touch 7300
# Register 7300 is the number of counts on Counter 0
$ cat 7300
0
# Tap GND to FIO4 to generate counts
$ cat 7300
52
# More counts
$ cat 7300
113
# Because 50502 is a bit field, this enables Counter 0 and Counter 1
$ echo 3 > 50502
# It also resets Counter 0
$ cat 7300
0
# Create the file for register 7300
$ touch 7302
# Register 7300 is the number of counts on Counter 1
$ cat 7302
0
# Tap GND to FIO5 to generate counts on Counter 1
$ cat 7302
431
# Counter 0 is unaffected
$ cat 7300
0
# Disable Counter 0 and Counter 1
$ echo 0 > 50502

Enabling a timer for PWM

We ended the shell example above by disabling the counters. In the remaining sessions, we will enable timers on FIO4 and FIO5. To follow this session, wire an LED from FIO4 to GND. We’ll put Timer 0 in PWM output mode. We’re going to use Modbus addresses 50501 (number of timers enabled), 7000 (Timer 0 clock), 7100 (Timer 0 config), 7200 (Timer 0 value)


# Enable one timer, Timer 0
$ echo 1 > 50501
# Wire an LED from FIO4 to GND
# Set the Timer 0 clock to 12 MHz (Section 2.9)
$ echo 1 > 7000
# Set the timer config to mode 0, which is PWM
$ echo 0 > 7100
# Set the timer value, which is the PWM duty cycle in this mode, to almost off
$ echo 65000 > 7200
# Set PWM duty cycle to a little less than 50%. LED should be dim.
$ echo 30000 > 7200
# Set PWM duty cycle 1o 100%. LED should be solid on.
$ echo 0 > 7200

Enabling a timer with a divisor

Timer 0’s clock divisor requires Modbus register 7002. Note that we disabled Counter 0 above, and we must do so to use a timer’s clock divisor.


# Set the Timer 0 clock to 1 MHz/Divisor
$ echo 3 > 7000
# Set the Timer 0 clock divisor to 5
$ echo 5 > 7002
# At this duty cycle the LED will blink rapidly
$ echo 30000 > 7100

Enable a second timer in system timer mode

The number of timers enabled, register 50501, is not a bit field, so we simply write a 2 to enable 2 timers. We will put Timer 1 in System Timer mode. It’s relevant registers are 7102 for Timer 1 config and 7202 for Timer 1 value.


# Confirm that one timer is enabled
$ cat 50501
1
# Enable 2 timers, Timer 0 and Timer 1
$ echo 2 > 50501
# This is hard to do in Modbus. Set Timer 1's mode to mode 10 and its value to 0
# The mode is the upper 16 bits, so the number to write is
# 10 * 2^16 + 0 = 655360
$ echo 655360 > 7102
# Mode 10 is the system timer mode
# Timer 1 is returning the number of ticks on the system clock
# The system clock runs at 4 MHz.
$ cat 7202
1054231929
$ cat 7202
1057235871
# Here's a loop that prints Timer 1 once per second
# The difference between the values is a little over 4,000,000.
$ while true; do cat 7202; sleep 1; done
1091411397
1096891158
1100939244
1104987085
1109035017
1113082859
1117130709
^C

Wrapup

There are a ton of Modbus registers in the LabJack Modbus Map. Now that you can create registers in the Modbus directory, you can use only the ones that you need without the clutter of listing them all.

JavaScript errors detected

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

If this problem persists, please contact our support.