3

I've been trying to read data from a TI TLC2578 ADC. I am using it in default mode. I modified the Microsoft IOT Pot Sensor sample code. I am able to read data but it seems like maybe the timing is off. I have the input wired through a 10k pot with a max of 3.3v.

At 0v I'm getting 1.8v reading. At 1.2v I'm reading 3.9v. Then at 1.4v the ADC reads 0v and starts climbing back up again as voltage is increased.

I'm new to interfacing ADC's so I'm prob doing something wrong. Any help is appreciated. Here is the code.

namespace PotentiometerSensor{

public sealed partial class MainPage : Page{

    enum AdcDevice { NONE, MCP3002, MCP3208, TLC2578 };

    /* Important! Change this to either AdcDevice.MCP3002 or AdcDevice.MCP3208 depending on which ADC you chose     */
    private AdcDevice ADC_DEVICE = AdcDevice.TLC2578;

  //  private const int LED_PIN = 4;
   // private GpioPin ledPin;

    private const string SPI_CONTROLLER_NAME = "SPI0";  /* Friendly name for Raspberry Pi 2 SPI controller          */
    private const Int32 SPI_CHIP_SELECT_LINE = 0;       /* Line 0 maps to physical pin number 24 on the Rpi2        */
    private SpiDevice SpiADC;

  //  private const byte MCP3002_CONFIG = 0x68; /* 01101000 channel configuration data for the MCP3002 */
  //  private const byte MCP3208_CONFIG = 0x06; /* 00000110 channel configuration data for the MCP3208 */

    private Timer periodicTimer;
    private int adcValue;

    public MainPage()
    {
        this.InitializeComponent();

        /* Register for the unloaded event so we can clean up upon exit */
        Unloaded += MainPage_Unloaded;

        /* Initialize GPIO and SPI */
        InitAll();
    }

    /* Initialize GPIO and SPI */
    private async void InitAll()
    {
        if (ADC_DEVICE == AdcDevice.NONE)
        {
            StatusText.Text = "Please change the ADC_DEVICE variable to either MCP3002 or MCP3208";
            return;
        }

        try
        {
            InitGpio();         /* Initialize GPIO to toggle the LED                          */
            await InitSPI();    /* Initialize the SPI bus for communicating with the ADC      */

        }
        catch (Exception ex)
        {
            StatusText.Text = ex.Message;
            return;
        }

        /* Now that everything is initialized, create a timer so we read data every 500mS */
        periodicTimer = new Timer(this.Timer_Tick, null, 0, 500);

        StatusText.Text = "Status: Running";
    }

    private async Task InitSPI()
    {
        try
        {
            var settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE);
            settings.ClockFrequency = 15000000;   /* 15MHz clock rate       chip rated for 25MHz                                 */
            settings.Mode = SpiMode.Mode0;      /* The ADC expects idle-low clock polarity so we use Mode0  */

            string spiAqs = SpiDevice.GetDeviceSelector(SPI_CONTROLLER_NAME);
            var deviceInfo = await DeviceInformation.FindAllAsync(spiAqs);
            SpiADC = await SpiDevice.FromIdAsync(deviceInfo[0].Id, settings);



        }

        /* If initialization fails, display the exception and stop running */
        catch (Exception ex)
        {
            throw new Exception("SPI Initialization Failed", ex);
        }
    }

    private void InitGpio()
    {
        var gpio = GpioController.GetDefault();

        /* Show an error if there is no GPIO controller */
        if (gpio == null)
        {
            throw new Exception("There is no GPIO controller on this device");
        }


    }


    /* Read from the ADC, update the UI, and toggle the LED */
    private void Timer_Tick(object state)
    {
        ReadADC();
      //  LightLED();
    }

    public void ReadADC()
    {
        byte[] readBuffer = new byte[12]; /* Buffer to hold read data*/

        byte[] writeBuffer = new byte[12] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

        /* Setup the appropriate ADC configuration byte */
        // switch (ADC_DEVICE)
        //  {
        //     case AdcDevice.MCP3002:
        //        writeBuffer[0] = MCP3002_CONFIG;
        //         break;
        //     case AdcDevice.MCP3208:
        //         writeBuffer[0] = MCP3208_CONFIG;
        //         break;
        //  }

        SpiADC.TransferFullDuplex(writeBuffer, readBuffer); /* Read data from the ADC       */           
        adcValue = convertToInt(readBuffer);                /* Convert the returned bytes into an integer value */


        /* UI updates must be invoked on the UI thread */
        var task = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            double conversionResult;
            textPlaceHolder.Text = adcValue.ToString();         /* Display the value on screen  */
            conversionResult = Convert.ToDouble(adcValue);
            conversionResult = (adcValue / 4095.00) * 4.00;
            BusInfoBox.Text = conversionResult.ToString();
        });
    }

    /* Convert the raw ADC bytes to an integer */
    public int convertToInt(byte[] data)
    {
        int result = 0;   
        result = data[0] & 0x0F;
        result <<= 8;
        result += data[1];
        return result;
    }

    private void MainPage_Unloaded(object sender, object args)
    {
        /* It's good practice to clean up after we're done */
        if(SpiADC != null)
            {
             SpiADC.Dispose();
            }

    }

}
BigSquirmy
  • 80
  • 1
  • 8
  • `All converters are specified for bipolar input range of ± 10 V. The input signal is scaled to 0–4 V at the SAR ADC input via the bipolar scaling circuit (see the functional block diagram and the equivalent analog input circuit): –10 V to 0 V, 10 V to 4 V, and 0 V to 2 V.` What's wrong ? default mode is 0-10V. You can't use on **default mode** – dsgdfg Sep 29 '15 at 07:14
  • Why cant you use on default mode? – BigSquirmy Sep 29 '15 at 21:53
  • I understand the signal is scaled through the ADC but why would it start off at 1.2 go to 4 then back down to zero when feeding 5vdc through a 10k pot. – BigSquirmy Sep 29 '15 at 21:54
  • we are see your code but `what is your input style?`. Signal is `OK` (Voltage level), but how much current need to full scale ? Can you told me `i haven't input current 1.6mA ?` Where `A1 to AGND or A0 to A4` differences ? – dsgdfg Sep 30 '15 at 12:52
  • Input style? Not sure i understand what you mean. – BigSquirmy Oct 15 '15 at 03:13

0 Answers0