internal AdcBackgroundWorkerArgs(AdcMode mode, MCP3424.AdcChannel channel, MCP3424.AdcSize size, MCP3424.AdcPga pga) { this._mode = mode; this._channel = channel; this._size = size; this._pga = pga; }
private void checkBoxReadVoltage_CheckedChanged(object sender, EventArgs e) { if (_usbI2C.IsOpen()) { if (checkBoxReadVoltage.Checked) { checkBoxReadVoltage.Text = "Stop"; comboBoxAdcMode.Enabled = false; comboBoxChannel.Enabled = false; comboBoxSize.Enabled = false; comboBoxGain.Enabled = false; _backgroundWorkerAdc = new BackgroundWorker(); _backgroundWorkerAdc.DoWork += BackgroundWorkerAdcDoWork; _backgroundWorkerAdc.ProgressChanged += BackgroundWorkerAdcProgressChanged; _backgroundWorkerAdc.WorkerReportsProgress = true; _backgroundWorkerAdc.WorkerSupportsCancellation = true; AdcMode mode = AdcMode.Raw; MCP3424.AdcChannel channel = MCP3424.AdcChannel.Channel0; MCP3424.AdcSize size = MCP3424.AdcSize.Size12Bit; MCP3424.AdcPga pga = MCP3424.AdcPga.Gain1; try { Enum.TryParse <AdcMode>(comboBoxAdcMode.SelectedValue.ToString(), out mode); Enum.TryParse <MCP3424.AdcChannel>(comboBoxChannel.SelectedValue.ToString(), out channel); Enum.TryParse <MCP3424.AdcSize>(comboBoxSize.SelectedValue.ToString(), out size); Enum.TryParse <MCP3424.AdcPga>(comboBoxGain.SelectedValue.ToString(), out pga); } catch (Exception ex) { string error = ex.Message; } AdcBackgroundWorkerArgs args = new AdcBackgroundWorkerArgs(mode, channel, size, pga); _backgroundWorkerAdc.RunWorkerAsync(args); } else { checkBoxReadVoltage.Text = "Start"; comboBoxAdcMode.Enabled = true; comboBoxChannel.Enabled = true; comboBoxSize.Enabled = true; comboBoxGain.Enabled = true; if (_backgroundWorkerAdc != null && _backgroundWorkerAdc.IsBusy) { _backgroundWorkerAdc.CancelAsync(); } Thread.Sleep(1000); } } }
/// <summary> /// Get an ADC current reading (requires current connection to I2C-AI418ML) /// </summary> /// <param name="channel">MCP3424.AdcChannel</param> /// <param name="size">MCP3424.AdcSize</param> /// <param name="pga">MCP3424.AdcPga</param> /// <param name="current">current of ADC channel read at size & gain (voltage / 249R)</param> /// <returns>StatusResults(FT260_STATUS, FT260_CONTROLLER_STATUS)</returns> public UsbI2C.StatusResults ReadCurrent(MCP3424.AdcChannel channel, MCP3424.AdcSize size, MCP3424.AdcPga pga, out double current) { UsbI2C.StatusResults statusResults; double voltage = 0.0; current = 0.0; if ((statusResults = ReadVoltage(channel, size, pga, out voltage)).HasErrors()) { return statusResults; } current = voltage /249; return statusResults; }
/// <summary> /// Get an ADC voltage reading (requires voltage connection to I2C-AI418ML) /// </summary> /// <param name="channel">MCP3424.AdcChannel</param> /// <param name="size">MCP3424.AdcSize</param> /// <param name="pga">MCP3424.AdcPga</param> /// <param name="voltage">voltage of ADC channel read at size & gain</param> /// <returns>StatusResults(FT260_STATUS, FT260_CONTROLLER_STATUS)</returns> public UsbI2C.StatusResults ReadVoltage(MCP3424.AdcChannel channel, MCP3424.AdcSize size, MCP3424.AdcPga pga, out double voltage) { UsbI2C.StatusResults statusResults; voltage = 0.0; uint reading = 0; if ((statusResults = _mcp3424.ReadAdc(channel, size, pga, out reading)).HasErrors()) { return statusResults; } voltage = (((reading * MCP3424.VRef) / (33 * _mcp3424.Gains[(int)pga])) * 180) / _mcp3424.MaxReadings[(int) size]; return statusResults; }
/// <summary> /// Get a raw ADC reading /// </summary> /// <param name="channel">MCP3424.AdcChannel</param> /// <param name="size">MCP3424.AdcSize</param> /// <param name="pga">MCP3424.AdcPga</param> /// <param name="reading">value of ADC channel read at size & gain</param> /// <returns>StatusResults(FT260_STATUS, FT260_CONTROLLER_STATUS)</returns> public UsbI2C.StatusResults ReadAdc(MCP3424.AdcChannel channel, MCP3424.AdcSize size, MCP3424.AdcPga pga, out uint reading) { return _mcp3424.ReadAdc(channel, size, pga, out reading); }