Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
using using using using using using System; System.IO.Ports; System.Threading; System.Collections.Generic; System.Linq; System.Text; namespace Test_MCP3201_Serial { class Program { static void Main(string[] args) { // Here we define the voltage reference (VREF) applied to // the pin 1 of MCP3201 device. You should assign // the appropriate value of the voltage reference of your circuit // for the constant VREF const double VREF = 5.00; // The variable k will contain the digital code after conversion is //done. The variable b is a mask int b = 0x4000, k = 0; // Creating a new instance of the SerialPort class with appropriate // parameters (COM1, baud rate 9600, without parity // bit, 8 data bits and one stop bit) SerialPort serialPort1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); // Opening the serial port COM1 serialPort1.Open(); // Reading the data from ADC MCP3201 serialPort1.RtsEnable = true; serialPort1.DtrEnable = false; // CLK goes low // CS goes high // CS goes low allowing the conversion process to be started serialPort1.DtrEnable = true; // Processing all 15 CLK pulses for (int i = 0; i < 15; i++) { // CLK goes low serialPort1.RtsEnable = true; // Reading appropriate bit into variable b if (serialPort1.CtsHolding == false) k |= b; // shift bits of the variable b right to check the next bit b = b >> 1; // CLK goes high serialPort1.RtsEnable = false; } // Conversion is done, so CS line goes high serialPort1.DtrEnable = false; // Closing the serial porter serialPort1.Close(); // We need only lower 12 bits of the binary result k = k & 0xFFF; // Now the variable k contains the binary result of the conversion. // The value of k must be transformed into float value which will be // saved in variable total int binRes = k; double total = Convert.ToDouble(binRes); // Calculating the final result total = VREF / 4096 * total; // Outputting the data on to screen Console.WriteLine("ADC result: {0}", total); } } }