Frequency measurement with U3 Timer for gate time (counting time) determination


Salefix

Recommended Posts

Start with the pseudocode in Section 4.3.6 of the U3 User's Guide. You want to use "System Timer Low (Mode 10)" instead of "Duty Cycle Measurement (Mode 4)", so in the LJ_ioPUT_TIMER_MODE request for Timer1 change the value to LJ_tmSYSTIMERLOW. That is the only change needed in the configuration.

For the reads, the given pseudocode does an eGet to read the value of Timer1 and then another eGet to read the Value of Counter0. You want to combine these two reads into a single Add/Go/Get block (Add/Add/Go/Get/Get actually), or use a single call to eTCValues.

Then, each time read the value from Timer1, you subtract the previous value to get the difference. Divide the difference value by 4 to get microseconds, since the system timer has a frequency of 4 MHz. Keep in mind that mode 10 reads the lower 32 bits of the system timer, which will roll every 18 minutes or so. You might want logic to detect and handle the roll, or you might want to use mode 11 also to get the full 64 bits.

Link to comment
Share on other sites

  • 1 month later...
Start with the pseudocode in Section 4.3.6 of the U3 User's Guide. You want to use "System Timer Low (Mode 10)" instead of "Duty Cycle Measurement (Mode 4)", so in the LJ_ioPUT_TIMER_MODE request for Timer1 change the value to LJ_tmSYSTIMERLOW. That is the only change needed in the configuration.

For the reads, the given pseudocode does an eGet to read the value of Timer1 and then another eGet to read the Value of Counter0. You want to combine these two reads into a single Add/Go/Get block (Add/Add/Go/Get/Get actually), or use a single call to eTCValues.

Then, each time read the value from Timer1, you subtract the previous value to get the difference. Divide the difference value by 4 to get microseconds, since the system timer has a frequency of 4 MHz. Keep in mind that mode 10 reads the lower 32 bits of the system timer, which will roll every 18 minutes or so. You might want logic to detect and handle the roll, or you might want to use mode 11 also to get the full 64 bits.

I did as you wrote...

The problem with the sniplet is that there are several activities at once - Its no to easy to get it to work...

Throwing some errors- see attached

tanks

U3_Stream_Simple_dig.ctl

Link to comment
Share on other sites

What are you trying to do? Since you have attached a stream example, and based on your initial post, I am guessing that you want to enable 1 timer (mode 10, Timer0, FIO0) and 1 counter (Counter0, FIO1), and then you want to stream readings from Timer0 and Counter0 at 500 scans/second.

Link to comment
Share on other sites

What are you trying to do? Since you have attached a stream example, and based on your initial post, I am guessing that you want to enable 1 timer (mode 10, Timer0, FIO0) and 1 counter (Counter0, FIO1), and then you want to stream readings from Timer0 and Counter0 at 500 scans/second.

Hello, attached my example- I want to measure frequency (period time). Stream does not work, but it would be sufficient to have average readings of the period each 0.1 sec.

I tried to use the DAQFactory functionality to set the timer 0 and had to restrict to 24Mhz but this is not the problem- apparently the timer has some kind of memory- of course it waits for the next event....

So the measured frequency does not return to zero.

thanks

U3_Timers_Counters_Sequence_Timer0_logger.ctl

Link to comment
Share on other sites

Hello, I have now solved the problem mastering DAQFactory sequences- having 2 Timers at FIO0+1 operating at rising and falling edge on the same signal and writing to a file. Last question remains physical - using LJDivider leaves out periods measurements for small Voltages but removing it delivers some very infeasible short period measurements. I have to admid that the AC signal is ranging from 0 to 20V and 0 to 150 Hz where voltage and frequency correlate. May be I have to abandon the more accurate frequency measurement or level the voltage somehow...

thanks

Gerfried

Link to comment
Share on other sites

So when you do not use the LJTick-Divider, you have the signal connected directly to the FIO line? And when you do this, you get some period measurement readings returned by the timer that are too short to be possible?

Which timer mode are you using?

I would think that the best option for you is to put a 10 kohm resistor in series with the signal and connect that resistor to the FIO line. The signal will not be divided, but rather it will just be clamped at 5.8 volts. That means that the signal side of the 10k resistor will have 20 volts on it, but on the other side of the resistor the FIO line will only see 5.8 volts. The 10k resistor limits the amount of current going into the FIO line to (20-5.8)/10k = 1.4 mA.

Link to comment
Share on other sites

So when you do not use the LJTick-Divider, you have the signal connected directly to the FIO line? And when you do this, you get some period measurement readings returned by the timer that are too short to be possible?

Which timer mode are you using?

2+3

I would think that the best option for you is to put a 10 kohm resistor in series with the signal and connect that resistor to the FIO line. The signal will not be divided, but rather it will just be clamped at 5.8 volts. That means that the signal side of the 10k resistor will have 20 volts on it, but on the other side of the resistor the FIO line will only see 5.8 volts. The 10k resistor limits the amount of current going into the FIO line to (20-5.8)/10k = 1.4 mA.

Thank you, I will buy the resistor tomorrow - meanwhile I was streaming the signal to be able to calculate the periods afterwards...

Perhaps others are also interested to know how you might reduce the file size of the data logged.

Since with streaming I have no possibility to interfere - I'm looking forward to record only seconds from the start and not from 1970- to save space.

thanks

CU

gerfried

Link to comment
Share on other sites

The 32-bit period timer modes (2 & 3) were improved in U3 firmware version 1.35, so update your firmware if you have an older version. We recommend all U3s be upgraded to firmware version 1.36 or higher anyway.

I found the following topic about adding a column of data to your file with time in a different format:

http://www.labjack.com/forums/index.php?showtopic=1427

Link to comment
Share on other sites

The 32-bit period timer modes (2 & 3) were improved in U3 firmware version 1.35, so update your firmware if you have an older version. We recommend all U3s be upgraded to firmware version 1.36 or higher anyway.

already done thank you - indeed I need more insight how the period measurements are done- signals are unsigned. The problem is that I cannot use 2 timers on two differential channels to get the real signal period with rising and falling edge (mode 2 and 3) in one , or may I?

The other problem I have is DAQtech related - how to insert the right time into the channel- period measurements deliver the time only at the end of the period, so I'm trying mychannel(InsertTime(time in sec) - but this does not work...

thanks

Link to comment
Share on other sites

Inserttime is a function, just like Sine. Lets say you have a variable that you want to put time into. There are actually two ways you can do this:

myvar = inserttime(myvar,systime(),-1)

or:

myvar.time[0] = systime()

Then you can put the value in a channel using AddValue():

Mychannel.addvalue(myvar)

You cannot change the time of a channel value once it is in a channel.

Link to comment
Share on other sites

The problem is that I cannot use 2 timers on two differential channels to get the real signal period with rising and falling edge (mode 2 and 3) in one , or may I?

I don't follow what you are trying to measure. The period would be either rising-edge to rising-edge, or falling-edge to falling-edge, and the inverse of the period gives you frequency. Maybe, though, you just want to know the high-time (pulse width) or low-time? What exactly are you trying to measure about the signal?

Link to comment
Share on other sites

The problem is that I cannot use 2 timers on two differential channels to get the real signal period with rising and falling edge (mode 2 and 3) in one , or may I?

I don't follow what you are trying to measure. The period would be either rising-edge to rising-edge, or falling-edge to falling-edge, and the inverse of the period gives you frequency. Maybe, though, you just want to know the high-time (pulse width) or low-time? What exactly are you trying to measure about the signal?

I have a sinoidal AC signal with varying Voltage (the faster the higher - hub dynamo) and tried to measure period lenght with the timers 2+3 (both to increase accuracy) - the question is - what signal state triggers the time exactly?

many thanks

gerfried

Link to comment
Share on other sites

... the question is - what signal state triggers the time exactly?

Each rising edge causes a new period to be calculated and stored in the timer value register in the U3. From the first paragraph of Section 2.9.1.3 of the U3 User's Guide:

"Mode 2: On every rising edge seen by the external pin, this mode records the number of clock cycles (clock frequency determined by TimerClockBase/TimerClockDivisor) between this rising edge and the previous rising edge. The value is updated on every rising edge, so a read returns the time between the most recent pair of rising edges."

Link to comment
Share on other sites

... the question is - what signal state triggers the time exactly?

Each rising edge causes a new period to be calculated and stored in the timer value register in the U3. From the first paragraph of Section 2.9.1.3 of the U3 User's Guide:

"Mode 2: On every rising edge seen by the external pin, this mode records the number of clock cycles (clock frequency determined by TimerClockBase/TimerClockDivisor) between this rising edge and the previous rising edge. The value is updated on every rising edge, so a read returns the time between the most recent pair of rising edges."

Sorry this is not helpful for my uneven signal- I need the definitions for rising and falling edge exactly!

thanks

Link to comment
Share on other sites

The LabJack guys will have to confirm this, but I believe for the timers work with discrete signals and not analog signals like you have. Technically you could probably stream the signal as an analog signal and let DAQFactory calculate the timing, but this won't give you the precision since the timers have a much higher time resolution than the streaming analog. So, personally, I'd use a schmidt trigger to convert the analog signal into a digital signal. Again, the LabJack guys (who are much better at hardware than I am) may want to chime in on this and make some recommendations. For example, I don't know if there is anything special in getting a schmidt trigger to work on fast signals. In general, a schmidt trigger is a very simply circuit that you should be able to create with components from Radio Shack.

Link to comment
Share on other sites

See the digital I/O logic levels in Appendix A of the U3 User's Guide. A rising edge is specified as when your signal changes from less than 0.8 volts to greater than 2.0 volts. A falling edge is when the signal changes from greater than 2.0 volts to less than 0.8 volts. The exact point where the transition happens is somewhere between 0.8 and 2.0 volts.

As AzeoTech suggested, if you need a more specific transition, you should use a comparator, where you use a reference voltage to specify whatever threshold you want. In addition, since your signal is not a nice clean square wave, you might want to use hysteresis, which is discussed in the comparator datasheet and gives you the Schmitt trigger effect. See the following:

http://www.labjack.com/forums/index.php?showtopic=2028

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.