Scripting a "virtual" PWM without hardware timers


bull8042

Recommended Posts

I am having trouble getting a clean signal out of DAC0 while using a very low frequency (1Hz) timer on a U3 to generate a PWM signal on an output. That is basically the nature of the hardware. So.....

I want to take a 0-100 range signal from a PID loop, and in a script convert it to a PWM signal on a digital output on the Labjack.

In other words, generate a 1Hz PWM output on EIO0 without enabling any timers or counters on the device itself. Is this feasible? The more I think about, the more confused I get.

Can you give me some pointers with the sequence?

Link to comment
Share on other sites

It is feasible, but expect up to maybe 1 or 2% of noise in the timing due to the non-real time nature of Windows. Its possible also that you'll have a lot less noise. Anyhow, let's say you have a variable with a PWM level 0 - 100 called simply "level", where 100 is full on, and you called your output simply DAC0 and you want it to go between 0 and 5. The script would look something like:

while(1)
   DAC0 = 0
   wait((100 - level) * 10)
   DAC0 = 1
   wait(level * 10)
endwhile

The only problem with the above method is that when you are at 100 or 0 you'll get a very fast (2-4 ms because of the hardware comm latency) blip. You can eliminate this with an if or two:

while(1)
   if (level != 100)
	  DAC0 = 0   
	  wait((100 - level) * 10) 
   endif
   if (level != 0)
	  DAC0 = 1
	  wait(level * 10)
   endif
endwhile

Note: this is one of the few places where wait() is appropriate as it will ensure the loop runs every second and doesn't drift. Just make sure and save your document before running it the first time, as a typo might cause DAQFactory to hang.

Link to comment
Share on other sites

Archived

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