/// <summary> /// Writes the specified data. /// </summary> /// <param name="channel">The channel.</param> /// <param name="data">The data.</param> public void Write(Mcp4822Channel channel, AnalogValue data) { using (spiConnection.SelectSlave()) { var value = (uint)(data.Relative * 0xFFF); if (value > 0xFFF) { value = 0xFFF; } // Set active channel spiConnection.Write(channel == Mcp4822Channel.ChannelB); // Ignored bit spiConnection.Synchronize(); // Select 1x Gain spiConnection.Write(true); // Active mode operation spiConnection.Write(true); // Write 12 bits data (some lower bits are ignored by MCP4802/4812 spiConnection.Write(value, 12); } }
/// <summary> /// Reads the specified channel. /// </summary> /// <param name="channel">The channel.</param> /// <returns>The value</returns> public AnalogValue Read(Mcp3208Channel channel) { using (spiConnection.SelectSlave()) { // Start bit spiConnection.Write(true); // Channel is single-ended spiConnection.Write(true); spiConnection.Write((byte)channel, 3); // Let one clock to sample spiConnection.Synchronize(); // Read 12 bits var data = (int)spiConnection.Read(12); return(new AnalogValue(data, 0xFFF)); } }
public decimal Read(Mcp3008Channel channel) { using (spiConnection.SelectSlave()) { // Start bit spiConnection.Write(true); // Channel is single-ended spiConnection.Write(true); // Channel Id spiConnection.Write((byte)channel, 3); // Let one clock to sample spiConnection.Synchronize(); // Read 10 bits var data = spiConnection.Read(10); return(data * scale / 1024m); } }