Skip to content

Commit

Permalink
examples: add analog_out_set_voltage example
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
Adrian-Stanea committed Dec 8, 2023
1 parent 5ce2e27 commit 33a0f5f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/analog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ 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)
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})
Expand Down
95 changes: 95 additions & 0 deletions examples/analog/analog_out_set_voltage.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/
// 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 <iostream>
#include <libm2k/m2k.hpp>
#include <libm2k/contextbuilder.hpp>
#include <libm2k/analog/m2kanalogin.hpp>
#include <libm2k/analog/m2kanalogout.hpp>

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;
}

0 comments on commit 33a0f5f

Please sign in to comment.