displaying output of adc on pc

Thread Starter

nawraj

Joined Jun 24, 2009
2
hi everyone;
I m new here,
:)I have to convert a real time signal into digital form.......have to get the output in binary form first...........
I m using mcp3201 with max232 and rs 232.............
my sorce code is as follows;
using System;
using System.IO.Ports;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test_MCP3201_Serial
{
class Program
{
static void Main(string[] args)
{

const double VREF = 5.00;

int b = 0x4000, k = 0;



SerialPort serialPort1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);



serialPort1.Open();

// Reading the data from ADC MCP3201

serialPort1.RtsEnable =
true; // CLK goes low
serialPort1.DtrEnable = false; // 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);
}
}
}
can it work for real time signal???????????
 

beenthere

Joined Apr 20, 2004
15,819
You seem to have several areas of confusion here. When you say -
I have to convert a real time signal into digital form.......have to get the output in binary form first
- it seems as if you do not that digitizing an analog signal turns it into a binary number (unless you use a BCD converter - but even that is Binary Coded Decimal).

The number you get on the SPI interface is the digitized, binary representation of the analog signal.

When you say -
can it work for real time signal???????????
- it is hard to tell if you are asking about the code you posted or the A to D converter. The PC probably will not work with the mcp3201 because the interface it (the mcp3201) uses is SPI. That is, indeed, a serial interface, but not at all like the RS232 serial interface associated with a COM port.

You might want to read up on the SPI interface.

As far as being able to digitize an analog signal in real time, that has to do with the signal. The mcp3201 can do up to 100 Ksamples/second. That is more than enough for any conversion up to the Nyquist limit, which is 50 KHz. The hard part is in sending that data stream to the computer and storing it.

You need to see what sampling rate is necessary to accurately sample the analog signal you have in mind. Just as an exersize, see if you can calculate how much data, in 12 bit words, you could have transferred to the PC if your COM port code had worked.
 
Top