static void Main() { int N_BITS = 4; M2k ctx = libm2k.m2kOpen(); M2kDigital dig = ctx.getDigital(); dig.setSampleRateIn(100000); dig.setSampleRateOut(100000); for (uint i = 0; i < N_BITS; i++) { dig.setDirection(i, DIO_DIRECTION.DIO_OUTPUT); dig.enableChannel(i, true); } VectorUS bufferOut = new VectorUS(); VectorUS bufferIn = new VectorUS(); for (ushort i = 0; i < (1 << N_BITS); i++) { bufferOut.Add(i); } dig.setCyclic(true); dig.push(bufferOut); bufferIn = dig.getSamples(1000); foreach (var val in bufferIn) { Console.Write(val + " \n"); } dig.stop(); libm2k.contextClose(ctx); }
static void Main() { M2k context = libm2k.m2kOpen("ip:192.168.2.1"); if (context == null) { Console.WriteLine("Connection Error: No ADALM2000 device available/connected to your PC."); return; } Console.WriteLine("Calibrating ADC . . ."); context.calibrateADC(); M2kPowerSupply powerSupply = context.getPowerSupply(); powerSupply.enableChannel(0, true); powerSupply.pushChannel(0, 3.3); var m2KI2CInit = new m2k_i2c_init(); m2KI2CInit.scl = 0; m2KI2CInit.sda = 1; m2KI2CInit.context = context; var i2cInitParam = new i2c_init_param(); i2cInitParam.max_speed_hz = 100000; i2cInitParam.slave_address = 0x48; i2cInitParam.extra = m2KI2CInit; var i2cDesc = libm2k.i2c_init(i2cInitParam); if (i2cDesc == null) { Console.WriteLine("I2C Error: Could not configure I2C"); return; } Console.WriteLine("Initiating I2C transfer . . ."); byte[] dataWriteConfig = { 0x0B }; libm2k.i2c_write(i2cDesc, dataWriteConfig, 1, i2c_transfer_mode.i2c_general_call | i2c_transfer_mode.i2c_repeated_start) byte[] dataReadConfig = { 0 }; libm2k.i2c_read(i2cDesc, dataReadConfig, 1, i2c_transfer_mode.i2c_general_call) Console.WriteLine("Reading the temperature . . ."); byte[] dataWriteTemperature = { 0 }; libm2k.i2c_write(i2cDesc, dataWriteTemperature, 1, i2c_transfer_mode.i2c_general_call | i2c_transfer_mode.i2c_repeated_start) byte[] dataReadTemperature = { 0, 0 }; libm2k.i2c_read(i2cDesc, dataReadTemperature, 2, i2c_transfer_mode.i2c_general_call) float temperature = ConvertTemperature(dataReadTemperature); Console.WriteLine("Temperature: " + temperature + "°C"); libm2k.i2c_remove(i2cDesc); libm2k.contextClose(context); }
public static void Main() { M2k context = libm2k.m2kOpen("ip:192.168.2.1"); if (context == null) { Console.WriteLine("Connection Error: No ADALM2000 device available/connected to your PC."); return; } var m2kUartInit = new m2k_uart_init(); m2kUartInit.bits_number = 8; m2kUartInit.parity = uart_parity.NO_PARITY; m2kUartInit.stop_bits = uart_stop_bits.ONE; m2kUartInit.context = context; var uartInitParam = new uart_init_param(); uartInitParam.device_id = 0; uartInitParam.baud_rate = 9600; uartInitParam.extra = m2kUartInit; var uartDescWrite = libm2k.uart_init(uartInitParam); uartInitParam.device_id = 1; var uartDescRead = libm2k.uart_init(uartInitParam); byte[] dataRead = new byte[3]; var threadRead = new Thread(() => { Thread.CurrentThread.IsBackground = true; lock (dataRead) { libm2k.uart_read(uartDescRead, dataRead, (byte)dataRead.Length); } }); threadRead.Start(); byte[] dataWrite = Encoding.ASCII.GetBytes("ADI"); Thread.Sleep(100); libm2k.uart_write(uartDescWrite, dataWrite, (byte)dataWrite.Length); threadRead.Join(); foreach (var c in dataRead) { Console.Write((char)c); } Console.WriteLine(); libm2k.uart_remove(uartDescWrite); libm2k.uart_remove(uartDescRead); libm2k.contextClose(context); }
static void Main() { M2k ctx = libm2k.m2kOpen(); ctx.calibrateADC(); // Setup analog in M2kAnalogIn ain = ctx.getAnalogIn(); ain.enableChannel(0, true); double voltage = ain.getVoltage(ANALOG_IN_CHANNEL.ANALOG_IN_CHANNEL_1); Console.WriteLine(voltage); libm2k.contextClose(ctx); }
static void Main() { M2k ctx = libm2k.m2kOpen(); ctx.calibrateADC(); // Will turn on the power supply if we need smth to measure M2kPowerSupply ps = ctx.getPowerSupply(); ps.enableChannel(0, true); ps.pushChannel(0, 1.7); // Setup analog in M2kAnalogIn ain = ctx.getAnalogIn(); ain.enableChannel(0, true); double voltage = ain.getVoltage(ANALOG_IN_CHANNEL.ANALOG_IN_CHANNEL_1); Console.WriteLine(voltage); libm2k.contextClose(ctx); }
static void Main() { M2k context = libm2k.m2kOpen("ip:192.168.2.1"); if (context == null) { Console.WriteLine("Connection Error: No ADALM2000 device available/connected to your PC."); return; } context.calibrateADC(); M2kAnalogIn analogIn = context.getAnalogIn(); M2kPowerSupply powerSupply = context.getPowerSupply(); M2kDigital digital = context.getDigital(); //setup analog in analogIn.setOversamplingRatio(1); analogIn.setSampleRate(1000000); analogIn.enableChannel(0, true); analogIn.enableChannel(1, false); analogIn.setRange((ANALOG_IN_CHANNEL)0, M2K_RANGE.PLUS_MINUS_25V); //enable LDAC digital.setDirection(3, DIO_DIRECTION.DIO_OUTPUT); digital.enableChannel(3, true); //enable CLR digital.setDirection(4, DIO_DIRECTION.DIO_OUTPUT); digital.enableChannel(4, true); //setup SPI var m2kSpiInit = new m2k_spi_init(); m2kSpiInit.clock = 1; m2kSpiInit.mosi = 2; m2kSpiInit.miso = 7; //dummy value - miso is not used in this example m2kSpiInit.bit_numbering = bit_numbering.MSB; m2kSpiInit.context = context; var spiInitParam = new spi_init_param(); spiInitParam.max_speed_hz = 1000000; spiInitParam.mode = spi_mode.SPI_MODE_3; spiInitParam.chip_select = 0; spiInitParam.extra = m2kSpiInit; spi_desc spiDesc = libm2k.spi_init(spiInitParam); if (spiDesc == null) { Console.WriteLine("SPI Error: Could not configure SPI"); return; } powerSupply.enableChannel(0, true); powerSupply.pushChannel(0, 5); //CLR and LDAC high as long as bits are transmitted digital.setValueRaw(3, DIO_LEVEL.HIGH); digital.setValueRaw(4, DIO_LEVEL.HIGH); byte[] data = GetRegisterData(Voltage); //transmitting data libm2k.spi_write_and_read(spiDesc, data, 2); //update with current shift register contents digital.setValueRaw(3, DIO_LEVEL.LOW); var samples = analogIn.getSamples(100); var average = samples[0].Average(); Console.WriteLine("Average value: " + average); libm2k.spi_remove(spiDesc); libm2k.contextClose(context, true); }
static void Main() { M2k ctx = libm2k.m2kOpen(); ctx.calibrateADC(); ctx.calibrateDAC(); M2kAnalogIn ain = ctx.getAnalogIn(); M2kAnalogOut aout = ctx.getAnalogOut(); M2kHardwareTrigger trig = ain.getTrigger(); // Setup analog in ain.enableChannel(0, true); ain.enableChannel(1, true); ain.setSampleRate(100000); ain.setRange((ANALOG_IN_CHANNEL)0, -10.0, 10.0); // nu are idxchannel ain.setRange((ANALOG_IN_CHANNEL)1, M2K_RANGE.PLUS_MINUS_25V); // Uncomment the following block to enable triggering // trig.setAnalogSource(M2K_TRIGGER_SOURCE.CHANNEL_1); // trig.setAnalogCondition(0, M2K_TRIGGER_CONDITION.RISING_EDGE); // trig.setAnalogLevel(0, 0.5); // trig.setAnalogDelay(0); // trig.setAnalogMode(0, M2K_TRIGGER_MODE.ALWAYS); // setup analog output aout.setSampleRate(0, 750000); aout.setSampleRate(1, 750000); aout.enableChannel(0, true); aout.enableChannel(1, true); // create output buffers var ch1 = new VectorD(); var ch2 = new VectorD(); var both = new VectorVectorD(); for (int i = 0; i < 1024; i++) { double val = 4.0; ch1.Add(val); ch2.Add(3.0); } both.Add(ch1); both.Add(ch2); aout.setCyclic(true); aout.push(both); var data = ain.getSamples(1024); for (int i = 0; i < 10; i++) { Console.WriteLine(data[0][i] + " "); } for (int i = 0; i < 10; i++) { Console.WriteLine(data[1][i] + " "); } aout.stop(); libm2k.contextClose(ctx); }
static int Main() { int elementSize = Marshal.SizeOf(typeof(IntPtr)); M2k ctx = libm2k.m2kOpen(); if (ctx == null) { Console.WriteLine("Connection Error: No ADALM2000 device available/connected to your PC."); return(1); } dig = ctx.getDigital(); M2kHardwareTrigger trig = dig.getTrigger(); long sr_divider = SR_DIVIDER_START; long sample_rate_in, sample_rate_out = MAX_SAMPLE_RATE; int k = 1; while (sr_divider > 1) { sample_rate_in = MAX_SAMPLE_RATE / sr_divider; sample_rate_out = MAX_SAMPLE_RATE / (sr_divider * 4); // set sample rates for in/out interface dig.setSampleRateIn(sample_rate_in + 1); dig.setSampleRateOut(sample_rate_out + 1); // set number of kernel buffers for the digital input interface dig.setKernelBuffersCountIn(KERNEL_BUFFERS_COUNT); trig.setDigitalStreamingFlag(true); for (uint j = 0; j < N_BITS; j++) { dig.setDirection(j, DIO_DIRECTION.DIO_OUTPUT); dig.enableChannel(j, true); } var bufferOut = new VectorUS(); for (uint j = 0; j < (1 << N_BITS); j++) { bufferOut.Add((ushort)j); } dig.setCyclic(true); dig.push(bufferOut); // Startup refill threads Thread refill_thread = new Thread(new ThreadStart(RefillThread)); Thread process_thread = new Thread(new ThreadStart(ProcessThread)); refill_thread.Start(); process_thread.Start(); refill_reset_event.Set(); process_done_reset_event.WaitOne(); refill_thread.Join(); process_thread.Join(); if (SHOW_BUFFERS) { Console.WriteLine("===================== BUFFER =================== " + k); foreach (var val in values) { Console.WriteLine((val)); } } bool stable = true; uint same_val = 0; uint same_val_cnt = 0; long dropped = 0; int i; for (i = 1; i < values.Length; i++) { // find first transition if (values[i] != values[i - 1]) { same_val = values[i]; break; } } for (; i < values.Length; i++) { if (values[i] == same_val) { same_val_cnt++; } else { int divider = (int)(sample_rate_in / sample_rate_out); if (same_val_cnt == divider) { { same_val = values[i]; same_val_cnt = 1; } } else { dropped = Math.Abs(values[i] - same_val) * divider + (same_val_cnt - divider); stable = false; break; } } } Console.Write("SR_DIVIDER: " + sr_divider + " SR_IN: " + sample_rate_in + " SR_OUT: " + sample_rate_out + " , " + ((stable) ? "STABLE " : "UNSTABLE ") + " dropped: " + dropped + " samples"); if (!stable) { Console.WriteLine(" @ buffer " + i / IN_NO_SAMPLES + " prev val: " + (same_val) + " next val: " + (values[i])); } Console.Write("\n"); sr_divider -= SR_DIVIDER_STEP; dig.stopBufferOut(); dig.stopAcquisition(); k++; } libm2k.contextClose(ctx); var exit = Console.ReadLine(); return(0); }