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