Trying to use four timers to read duty cycle, 4Mhz clock | LabJack

Trying to use four timers to read duty cycle, 4Mhz clock

7 posts / 0 new
Last post
Trying to use four timers to read duty cycle, 4Mhz clock

I'm working on a project that needs to read the dutycycle of an input. I have no problem using the control panel and it works fine. My problem is that I don't read/write in C.

Can you show me how to do this using Sorry, I know this is a reoccuring request, but many of us don't write C, only VB.

Thank you.

LabJack Support
labjack support's picture
Take a look at the .NET

Take a look at the .NET examples for VB .NET examples:

There is a U6_TIMERCOUNTER example to help get you started.

Specific to duty cycle inputs, use timer mode 4 which is .NET constant LJUD.TIMERMODE.DUTYCYCLE. After configuration use the IO constant LJUD.IO.GET_TIMER to read the timer value containing duty cycle information. Similar to this which is reading the timer value from timer 0 configured for duty cycle (duty cycle information returned by reference through dblValue):

LJUD.eGet(u6.ljhandle, LJUD.IO.GET_TIMER, 0, dblValue, dummyDoubleArray)

The duty cycle input timer mode is further documented here, discussing configuration and what the read timer value is:

Thanks, I'll give them a look

Thanks, I'll give them a look. The problem I was having was that the examples were all written in c, not vb.

We wrote this function:

We wrote this function:

Private Function DutyCycleCheck(channelNum As LJUD.CHANNEL, frequency As Double) As Double
        Dim dutyCyle As Double
        Dim labjack As U6 = New U6(CONNECTION.USB, "0", True)
        Dim MSW As Double
        Dim LSW As Double
        Dim factor As Double = 10 ^ 6
        Dim AverageDutyCycle As Double

        AddRequest(labjack.ljhandle, IO.PUT_TIMER_MODE, channelNum, TIMERMODE.DUTYCYCLE, 0, 0) ' sets the timer mode on the channel to look at duty cycle
        AddRequest(labjack.ljhandle, IO.PUT_TIMER_VALUE, channelNum, 1, 0, 0)    'Resets Timer to 0
        GoOne(labjack.ljhandle) 'execute request

        eGet(labjack.ljhandle, IO.GET_TIMER, channelNum, dutyCyle, 0) 'Gets the duty cycle from the timer channel and stores it in the dutyCycle variable

        LSW = dutyCyle Mod 2 ^ 16 'least significant word from first 2 bytes
        MSW = dutyCyle / (2 ^ 16) 'most significant word for the last 2 bytes

        LSW = LSW / factor
        MSW = MSW / factor

        LSW = LSW / (1 / frequency)
        MSW = MSW / (1 / frequency)

        AverageDutyCycle = (LSW + MSW) / 2.0

        Return AverageDutyCycle * 100.0 'multiply by 100%

    End Function

Unfortunately, when I use this I get an answer even if the target source is not connected to the Labjack. Can you see an error?

LabJack Support
labjack support's picture
You need to enable the timer

You need to enable the timer with a NUMBER_TIMERS_ENABLED before you can configure the timer and read from it. The U6_TimerCounter example demonstrates the standard way to configure the timers' clock, number of timers enabled, counters if needed, timer modes and read the current timer value.

Also, there is general UD driver documentation on timer and counter usage:

Note that constant prefixes in the docs. are this in .NET:

LJ_io  ->  LJUD.IO.

If you run into further problems, what error are you getting? You can use exception handling on the code to get the error.

Thanks! No more crash. BTW,

Thanks! No more crash. BTW, how to I handle getting four returns/dutycycles. I'm only working with one right now. How does the LJUD return from four?


Never mind, that was stupid.

Never mind, that was stupid. Inc the eGet lines with each channel and use a diff holder for the data.