static void Main() { const ConnectorPin adcClock = ConnectorPin.P1Pin23; const ConnectorPin adcMiso = ConnectorPin.P1Pin21; const ConnectorPin adcMosi = ConnectorPin.P1Pin19; const ConnectorPin adcCs = ConnectorPin.P1Pin24; Console.Clear(); Console.WriteLine("MCP-3208 Sample: Reading ADC points in all channels"); Console.WriteLine(); Console.WriteLine("\tClock: {0}", adcClock); Console.WriteLine("\tCS: {0}", adcCs); Console.WriteLine("\tMOSI: {0}", adcMosi); Console.WriteLine("\tMISO: {0}", adcMiso); Console.WriteLine(); var driver = new GpioConnectionDriver(); { Console.CursorVisible = false; var adcConnection = new Mcp3208SpiConnection( driver.Out(adcClock), driver.Out(adcCs), driver.In(adcMiso), driver.Out(adcMosi)); while (!Console.KeyAvailable) { Console.CursorTop = 0; Console.Clear(); Mcp3208Channel chan = Mcp3208Channel.Channel0; for (int i = 0; i < 8; i++) { AnalogValue p = adcConnection.Read(chan); decimal points = p.Value; Console.WriteLine(i.ToString() + " ADC points " + points.ToString()); using (StreamWriter sw = File.AppendText(".\\prova.txt")) { sw.WriteLine(chan.ToString() + " ADC points " + points.ToString()); } chan++; // enum increase sends to the next channel } Thread.Sleep(500); } } Console.CursorTop++; Console.CursorVisible = true; }
/// <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); } }
/// <summary> /// Reads the specified channel. /// </summary> /// <param name="channel">The channel.</param> /// <returns>The value.</returns> public AnalogValue Read(Mcp3208Channel channel) { using (this.spiConnection.SelectSlave()) { // Start bit this.spiConnection.Write(true); // Channel is single-ended this.spiConnection.Write(true); this.spiConnection.Write((byte)channel, 3); // Let one clock to sample this.spiConnection.Synchronize(); // Read 12 bits var data = (int)this.spiConnection.Read(12); return(new AnalogValue(data, 0xFFF)); } }
/// <summary> /// Initializes a new instance of the <see cref="Mcp3208InputAnalogPin" /> class. /// </summary> /// <param name="connection">The connection.</param> /// <param name="channel">The channel.</param> public Mcp3208InputAnalogPin(Mcp3208Device connection, Mcp3208Channel channel) { this.connection = connection; this.channel = channel; }
/// <summary> /// Creates an analog input pin. /// </summary> /// <param name="connection">The connection.</param> /// <param name="channel">The channel.</param> /// <returns>The pin.</returns> public static Mcp3208InputAnalogPin In(this Mcp3208SpiConnection connection, Mcp3208Channel channel) { return(new Mcp3208InputAnalogPin(connection, channel)); }
/// <summary> /// Creates an analog input pin. /// </summary> /// <param name="connection">The connection.</param> /// <param name="channel">The channel.</param> /// <returns>The pin.</returns> public static Mcp3208InputAnalogPin In(this Mcp3208SpiConnection connection, Mcp3208Channel channel) { return new Mcp3208InputAnalogPin(connection, channel); }
/// <summary> /// Initializes a new instance of the <see cref="Mcp3208InputAnalogPin" /> class. /// </summary> /// <param name="connection">The connection.</param> /// <param name="channel">The channel.</param> public Mcp3208InputAnalogPin(Mcp3208SpiConnection connection, Mcp3208Channel channel) { this.connection = connection; this.channel = channel; }