Jump to content
  • 0

SDK (Python) PWM duty cycle and frequency measurement




I want to measure PWM signal's parameters generated by other device using AD2 (+ Waveforms SDK + Python) At least duty cycle and frequency should be measured. I didn't find anything related to such measurement. One idea is to record samples and then try to calculate them, but this needs huge amount of work. 

Is there such functions which could speed up my task? It doesn't matter if i need to use analog or digital input. Using GUI it is easy to check, but i need to integrate it with my automated environment

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

I collect samples, then i can calculate duty cycle and frequency. But, sometimes i get samples with 16/17 value (depending on io state) instead of 0/1. Why? How can i fix it?
edit: This works for me, but please confirm:

# mask values from other DIO
for i in range(100):
    rgwSamples[i] = rgwSamples[i]&1

And how to avoid some noises (when i get one or few 0 during "1" pulse - which is not expected)?

Here is part of my code:

# sample rate = system frequency / divider
hzDI = c_double()
divider = c_uint()
dwf.FDwfDigitalInInternalClockInfo(hdwf, byref(hzDI))
dwf.FDwfDigitalInDividerSet(hdwf, c_int(int(hzDI.value/freq_measurement))) # 1200kHz

# 8bit per sample format
dwf.FDwfDigitalInSampleFormatSet(hdwf, c_int(8))
# set number of sample to acquire
cSamples = 100
rgwSamples = (c_uint8*cSamples)()
dwf.FDwfDigitalInBufferSizeSet(hdwf, c_int(cSamples))

# trigger settings - falling edge of at least 10 microseconds on DIO 0
dwf.FDwfDigitalInTriggerSourceSet(hdwf, c_ubyte(3)) # trigsrcDetectorDigitalIn
dwf.FDwfDigitalInTriggerPositionSet(hdwf, c_int(int(cSamples-1)))
dwf.FDwfDigitalInTriggerSet(hdwf, c_int(0), c_int(0), c_int(0), c_int(1<<0)) # DIO0 falling edge
dwf.FDwfDigitalInTriggerLengthSet(hdwf, c_double(1e-5), c_double(-1), c_int(0))

# begin acquisition
dwf.FDwfDigitalInConfigure(hdwf, c_int(1), c_int(1))

print("Waiting for acquisition...")
while True:
    dwf.FDwfDigitalInStatus(hdwf, c_int(1), byref(sts))
    if sts.value == stsDone.value :
print("   done")

# get samples, byte size
dwf.FDwfDigitalInStatusData(hdwf, rgwSamples, cSamples)

# mask values from other DIO
for i in range(100):
    rgwSamples[i] = rgwSamples[i]&1

# print samples
for i in range(100):
    print(rgwSamples[i],end = " ")


Edited by mjelec
Link to comment
Share on other sites

  • 0

Hi @mjelec

The samples represent levels for DIO channels 0 to 15
When a channel is left floating, the high impedance input can capture radiation, specially transitions from nearby wires or channels, or the grid's 50/60Hz
Mask the channels you are interested in, like for DIO-0 : rgwSamples[i]&1

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...