Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


counters: easy functions vs. IOType method

  • Please log in to reply
3 replies to this topic

#1 ElectroLund

  • Members
  • 14 posts

Posted 15 November 2012 - 08:56 AM

Hello again, Planet LabJack! I have a question regarding the use of the IOType method for reading a counter input, versus the easy function method (I prefer the readability of the latter).

Test setup
I have a signal input of 3.3V pk, 14KHz signal, 18% duty cycle.

Using the IOType code, I am reading in values to a double variable that look like 30.00...
Using the eTCConfig / eTCValues "easy" functions, I am reading values to a double variable like 8958324.00...

from the same invariant input signal. As far as I can tell, I am enabling the counters and timers in the same way. None of my function calls are returning errors.

Am I not seeing something obvious? My two code blocks:

double dblValue=0;		// timer read value

AddRequest (lngHandle, LJ_ioPUT_CONFIG, LJ_chTIMER_COUNTER_PIN_OFFSET, 4, 0, 0);	// Set the pin offset to 4, which causes the timers to start on FIO4.
AddRequest(lngHandle, LJ_ioPUT_CONFIG, LJ_chNUMBER_TIMERS_ENABLED, 0, 0, 0);	// disable timers on FIO4
AddRequest(lngHandle, LJ_ioPUT_COUNTER_ENABLE, 0, 1, 0, 0);		// Counter0 is enabled.  It will use the next available line, FIO4.
AddRequest(lngHandle, LJ_ioPUT_COUNTER_ENABLE, 1, 0, 0, 0);		// Counter1 is disabled.
AddRequest(lngHandle, LJ_ioPUT_TIMER_MODE, 0, LJ_tmRISINGEDGES32, 0, 0);		// set the timer mode to Period input (32-bit, rising edges)
AddRequest(lngHandle, LJ_ioPUT_CONFIG, LJ_chTIMER_CLOCK_BASE, LJ_tcSYS, 0, 0);	// Set the clock frequency to use system clock
AddRequest(lngHandle, LJ_ioPUT_CONFIG, LJ_chTIMER_CLOCK_DIVISOR, 24, 0, 0); 	// Set the divisor

GoOne(lngHandle);		// execute the ahove commands
eGet (lngHandle, LJ_ioGET_COUNTER, 0, &dblValue, 0);		// read the Counter0 input

easy functions:
// variables for the "easy" function to configure and initialize all timers and counters:
long alngEnableTimers[2]; 	// timer enable array
long alngEnableCounters[2];	// counter enable array
long TCPinOffset;			// Value  from 4-8 specifies where to start assigning timers and counters.
long TimerClockBaseIndex;	// Pass a constant to set the timer base clock. The default is LJ_tc48MHZ.
long TimerClockDivisor;		// Pass a divisor from 0-255 where 0 is a divisor of 256.
long alngTimerModes[2]; 	// timer mode array
double adblTimerInitValues[2];	// timer initial value array

// variables for the "easy" function to update and read all timers and counters:
long alngReadTimers[2]; 		// timer read command array
long alngUpdateResetTimers[2];	// timer update/reset command array
long alngReadCounters[2];		// counter read command array
long alngResetCounters[2];		// counter reset command array
double adblTimerValues[2];		// new timer value array
double adblCounterValues[2]; 	// new counter value array

alngEnableTimers[LABJACK_TIMER0] = FALSE;				// Disable Timer0
alngEnableTimers[LABJACK_TIMER1] = FALSE;				// Disable Timer1
alngEnableCounters[LABJACK_COUNTER0] = TRUE; 			// Enable Counter0
alngEnableCounters[LABJACK_COUNTER1] = FALSE; 			// Disable Counter1
TCPinOffset = 4;										// start at pin FIO4
TimerClockBaseIndex = LJ_tcSYS;							// set index to system clock
TimerClockDivisor = 24;									// set divisor to 24
alngTimerModes[LABJACK_TIMER0] = 0;						// Timer0 not used
alngTimerModes[LABJACK_TIMER1] = 0;						// Timer1 not used

eTCConfig(lngHandle, alngEnableTimers, alngEnableCounters, TCPinOffset, TimerClockBaseIndex, TimerClockDivisor, alngTimerModes, adblTimerInitValues, 0, 0);	// set up the counter

alngReadTimers[LABJACK_TIMER0] = FALSE;			// Don't Read Timer0
alngReadTimers[LABJACK_TIMER1] = FALSE;			// Don't Read Timer1
alngUpdateResetTimers[LABJACK_TIMER0] = FALSE;	// Don't Update and reset Timer0
alngUpdateResetTimers[LABJACK_TIMER1] = FALSE;	// Don't Update and reset Timer1
alngReadCounters[LABJACK_COUNTER0] = TRUE; 		// Read Counter0
alngReadCounters[LABJACK_COUNTER1] = FALSE; 	// Don't Read Counter1
alngResetCounters[LABJACK_COUNTER0] = FALSE; 	// Is this Reset causing problems? Counter0
alngResetCounters[LABJACK_COUNTER1] = FALSE;	// Don't Reset Counter1
adblTimerValues[LABJACK_TIMER0] = 0; 			// Don't Change Timer0
adblTimerValues[LABJACK_TIMER1] = 0; 			// Don't Change Timer1

eTCValues(lngHandle, alngReadTimers, alngUpdateResetTimers, alngReadCounters, alngResetCounters, adblTimerValues, adblCounterValues, 0, 0);	// go get the values

#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 16 November 2012 - 03:31 PM

We are having a tough time figuring out just what is going on here. Let's start with the basics: What is your U3's hardware version? What is your U3's firmware version? The configuration looks like it might be a problem. Using 1 counter and no timers, but the timer clock is being set to a non-divisor type then a divisor is set. More interesting is that counter0 is used to create the divisor. So you may be reading some byproduct of the divisor setting.

#3 ElectroLund

  • Members
  • 14 posts

Posted 19 November 2012 - 08:45 AM

My U3-LV:
  • Firmware = 1.0
  • Bootloader = 0.27
  • Hardware = 1.3
This U3 was purchased by one of our sub-contractors, so I'm unable to upgrade the firmware at this time.

Since last week, I've discovered the errors you noted. For starters, I have switched to using a timer rather than a counter and fixed the divisor issue as well. I'm now getting very good timer readings to measure my test frequency (via period).

More interesting is that counter0 is used to create the divisor.

I'm not sure where this is. Can you be more specific to a line of code?

I did notice an interesting issue with the DAC block. In this same set of test code, I enabled the DAC to output 0.81V to set a piece of equipment. However, with the following:

TimerClockBaseIndex = LJ_tc48MHZ_DIV;
TimerClockDivisor = 48;

I get a sinusoid out of the DAC with a ton of overshoot (like 2V). If I change to an overall much faster clock, say DIV of only 1, then I get a better DC output with no AC coupling. Not really a big issue, since I can do a software divisor of my results from the timer read.

#4 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 19 November 2012 - 10:49 AM

Firmware 1.0 is really old. I highly recommend you run LJSelfUpgrade and upgrade to the latest. Also make sure your UD driver version is current.

On the Counter0 & divisor comment, what someone was trying to say is that Counter0 is not available when you use a divisor clock. In your case you would not get an error, though, because you are using the non-divisible SYS clock and thus divisor is ignored.

The DAC issue with lower timer clocks is discussed in the 4th paragraph here:


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users