Jump to content
  • 0

How do I measure the voltage after I set the voltage on MCU? (Using pydwf)


Oznur Caliskan

Question

Hi,

I would like to set the voltage from my Python code and then read it.

Here is the code (class) I use for reading:

class AnalogIn:
    def __init__(self, analogOut, analogIn, analog_out_channel, analog_in_channel, voltage_in):
        self.analogOut = analogOut
        self.analogIn = analogIn
        self.analog_out_channel = analog_out_channel
        self.analog_in_channel = analog_in_channel
        self.voltage_in = voltage_in

    def measurement(analogIn):
        # Get analog input voltage.
        # The easiest way to read a static voltage is by querying the status and using the statusSample() method.
        analogIn.voltage_in = analogIn.statusSample(analogIn.analog_in_channel)
        print("Voltage in:", analogIn.voltage_in)
        time.sleep(0.010)

And here I am setting the voltage and try to read it by calling AnalogIn class:

        elif analogOut.signal_type == "2":  # DC
            # Initialize channel output of analog-out voltage controlled by Python.
            # We do this by a trick where we keep the channel disabled, and use the first value
            # of a square wave, which is output in Idle mode.
            CH1 = 0
            CH2 = 1
            for channel_index in (CH1, CH2):
                analogOut.nodeFunctionSet(channel_index, DwfAnalogOutNode.Carrier, DwfAnalogOutFunction.Square)
                analogOut.idleSet(channel_index, DwfAnalogOutIdle.Initial)
                analogOut.nodeEnableSet(channel_index, DwfAnalogOutNode.Carrier, True)
                analogOut.configure(channel_index, True)  # Necessary to give sine wave configured

            print("Processing...\n")
            # Offset değerini input olarak alır
            analogOut.voltage_DC = input("Please enter the DC Voltage value:")  # V
            while True:
                try:
                    analogOut.voltage_DC = float(analogOut.voltage_DC)
                    break
                except ValueError:
                    analogOut.voltage_DC = input("Please enter a valid number:")
                    continue
            print("DC Voltage:", analogOut.voltage_DC, "\n")

            # We now have a valid voltage. Push set the emplitude of the (inactive) square wave.
            # Since the channel is configured to replicate the first value of the waveform when idle
            # (disabled), this value will show up on the output.
            t_stopwatch = 0.0
            counter = 0
            t0 = time.monotonic()

            while True:
                t = time.monotonic() - t0
                for channel_index in (CH1, CH2):
                    analogOut.amplitudeSet(channel_index, analogOut.voltage_DC)
                counter += 1
                if counter == 500:
                    for channel_index in (CH1, CH2):
                        duration = (t - t_stopwatch)  # pylint: disable=superfluous-parens
                        print("{:8.3f} loops/sec. Press Q to quit.".format(counter / duration))
                        print("Giving output for Channel", CH1, "is:", analogOut.amplitudeGet(CH1))
                        print("Giving output for Channel", CH2, "is:", analogOut.amplitudeGet(CH2), "\n")
                        AnalogIn.measurement(analogIn=analogOut.voltage_DC)
                    counter = 0
                    t_stopwatch = t

However, I set the voltage apparently but can't do the reading. So what am I missing if anybody can tell I would be appreciated :) thank you

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

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...