From 33a0f5f45bb1f974711aedb67ba26363ff180b2a Mon Sep 17 00:00:00 2001 From: Adrian Stanea Date: Fri, 8 Dec 2023 15:21:55 +0200 Subject: [PATCH] examples: add analog_out_set_voltage example - show how the setVoltage and setVoltageRaw functions work - Used to test correct implementation of the set functions. - NOTE: raw_enable attribute should be set to enalbed in order for the DAC output to be updated with the new value. Signed-off-by: Adrian Stanea --- examples/analog/CMakeLists.txt | 2 + examples/analog/analog_out_set_voltage.cpp | 95 ++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 examples/analog/analog_out_set_voltage.cpp diff --git a/examples/analog/CMakeLists.txt b/examples/analog/CMakeLists.txt index 07f65836..26f31e4b 100644 --- a/examples/analog/CMakeLists.txt +++ b/examples/analog/CMakeLists.txt @@ -20,6 +20,7 @@ add_executable(streaming_synchronized "streaming_synchronized.cpp") add_executable(sync_stream_diff_frequencies "sync_stream_diff_frequencies.cpp") add_executable(stream_test_adc "stream_test_adc.cpp") add_executable(stream_test_dac "stream_test_dac.cpp") +add_executable(analog_out_set_voltage "analog_out_set_voltage.cpp") target_link_libraries(analog_in_out PRIVATE libm2k::libm2k) target_link_libraries(streaming_one_channel PRIVATE libm2k::libm2k) @@ -27,6 +28,7 @@ target_link_libraries(streaming_synchronized PRIVATE libm2k::libm2k) target_link_libraries(sync_stream_diff_frequencies PRIVATE libm2k::libm2k) target_link_libraries(stream_test_adc PRIVATE libm2k::libm2k) target_link_libraries(stream_test_dac PRIVATE libm2k::libm2k) +target_link_libraries(analog_out_set_voltage PRIVATE libm2k::libm2k) if (PTHREAD_LIBRARIES) target_link_libraries(sync_stream_diff_frequencies PRIVATE ${PTHREAD_LIBRARIES}) diff --git a/examples/analog/analog_out_set_voltage.cpp b/examples/analog/analog_out_set_voltage.cpp new file mode 100644 index 00000000..edc81591 --- /dev/null +++ b/examples/analog/analog_out_set_voltage.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2023 Analog Devices Inc. + * + * This file is part of libm2k + * (see http://www.github.com/analogdevicesinc/libm2k). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + */ +// This example assumes the following connections: +// W1 -> 1+ +// W2 -> 2+ +// GND -> 1- +// GND -> 2- +// +// The application is a demo that will output a voltage on W1 and W2. The signal is fed back into the analog input. +// The voltage can be set either as raw value or as voltage value. The voltage values are displayed on the screen. + +#include +#include +#include +#include +#include + +using namespace std; +using namespace libm2k; +using namespace libm2k::analog; +using namespace libm2k::context; + +void usingSetVoltage(M2kAnalogIn *, M2kAnalogOut *); +void usingSetVoltageRaw(M2kAnalogIn *, M2kAnalogOut *); + +int main(int argc, char *argv[]) +{ + M2k *ctx = m2kOpen("ip:192.168.2.1"); + if (!ctx) + { + std::cout << "Connection Error: No ADALM2000 device available/connected to your PC." << std::endl; + return 1; + } + + // Get the analog input and output interfaces and calibrate + M2kAnalogIn *ain = ctx->getAnalogIn(); + M2kAnalogOut *aout = ctx->getAnalogOut(); + ain->reset(); + aout->reset(); + ctx->calibrate(); + + usingSetVoltage(ain, aout); + usingSetVoltageRaw(ain, aout); + + contextClose(ctx); + return 0; +} + +void usingSetVoltage(M2kAnalogIn *ain, M2kAnalogOut *aout) +{ + auto out_voltage_0 = 2.5; + auto out_voltage_1 = -2.5; + auto out_raw_0 = aout->setVoltage(0, out_voltage_0); + auto out_raw_1 = aout->setVoltage(1, out_voltage_1); + std::cout << "Example using setVoltage:" << std::endl; + std::cout << "\tSetting VOLTAGE at channel 0: " << out_voltage_0 << " [V] = " << out_raw_0 << " [raw] " << std::endl; + std::cout << "\tSetting VOLTAGE at channel 1: " << out_voltage_1 << " [V] = " << out_raw_1 << " [raw] " << std::endl; + + auto in_voltage_0 = ain->getVoltage(0); + auto in_voltage_1 = ain->getVoltage(1); + std::cout << "\tRedback value at channel 0: " << in_voltage_0 << " [V]" << std::endl; + std::cout << "\tRedback value at channel 1: " << in_voltage_1 << " [V]" << std::endl; +} + +void usingSetVoltageRaw(M2kAnalogIn *ain, M2kAnalogOut *aout) +{ + auto out_raw_0 = aout->setVoltageRaw(0, 49000); + auto out_raw_1 = aout->setVoltageRaw(1, 16000); + std::cout << "Example using setVoltageRaw:" << std::endl; + std::cout << "\tSetting RAW at channel 0: " << out_raw_0 << " [raw]" << std::endl; + std::cout << "\tSetting RAW at channel 1: " << out_raw_1 << " [raw]" << std::endl; + + auto in_voltage_0 = ain->getVoltage(0); + auto in_voltage_1 = ain->getVoltage(1); + std::cout << "\tRedback value at channel 0: " << in_voltage_0 << " [V]" << std::endl; + std::cout << "\tRedback value at channel 1: " << in_voltage_1 << " [V]" << std::endl; +}