Shop OBEX P1 Docs P2 Docs Learn Events
A/D Converters and a Simple IDE Question — Parallax Forums

A/D Converters and a Simple IDE Question

Can someone post an example of code for using an A/D converter? I have a Quickstart Rev.A, and I'm trying to learn how to use C to program it. (It would be a bonus if you would also explain how the code works)

Also, I was wondering if you could reference another program with C in Simple IDE, and if that other program had to be in C or if it could be in Spin?

Thanks in advance!

Comments

  • There are all sorts of examples in the Propeller Object Exchange, but they're written in Spin. I think there are a few examples in C for the Board of Education and Activity Board on the Learn website, but they're for the A/D converter built into each of those boards.

    Do you have a particular A/D converter in mind?
  • Jeff15Jeff15 Posts: 6
    edited 2015-10-17 17:46
    I have a Quickstart Rev. A with a MCP3202 A/D converter, but I'm not sure if that's the A/D converter I want to use because I need a good amount of precision too. Does anyone have any recommendations for a good A/D converter as well?
  • Mike GreenMike Green Posts: 23,101
    edited 2015-10-17 18:30
    As usual, it depends on what you want to do with it. There are 8-bit resolution ADCs and 12-bit resolution ADCs. Whether the additional 4-bits of resolution mean anything depends on the specifics of the device, depends on the quality of your power supply (noise, stability, accuracy of regulation), on the layout of the portion of your board, on the signal processing "up front". 12-bits of resolution mean little if your signal is noisy, unstable, of low resolution. There are interfaces that use SPI or I2C. Some ADCs use the power supply as the reference source, some have an internal reference, and some allow for an external reference.

    If you use an ADC with an I2C bus, you can save some I/O pins and put the ADC on the I2C pins used for the EEPROM. An SPI bus is usually faster. If the ADC is slow, SPI won't make much difference.

    Remember that you can use a pair of Propeller I/O pins as an ADC. Look at the application note on the cog counters for more information and a Spin object that provides the ADC functionality.

    You might use the Board of Education or the Activity Board as models. Both have ADCs and the schematics are downloadable. Both are supported for tutorials and code examples.
  • twm47099twm47099 Posts: 867
    edited 2015-10-17 18:53
    The 3202 is an SPI device. I wrote a C library for a fast SPI driver that includes a number of SPI devices including the 3208 ADC. There are some differences between your 3202 (2 Channel ADC) and the 3208 (8 channel ADC) so my library function might need some modification. I believe that both devices are 12 bit ADCs (I know the 3208 is). The library is at the following post:

    forums.parallax.com/discussion/comment/1315065/#Comment_1315065

    The full thread is my adventures in learning how to make a C library that uses some PASM from a SPIN object.

    There is also a simple SPI function in the libraries that come with SimpleIDE (in the simpletools library). It's just not that fast, which for your application may not be an issue. There is an example (but different type) that uses that library here:

    learn.parallax.com/propeller-c-simple-protocols/spi-example

    There are also a couple of examples in the OBEX. One of the simplest that does not use PASM is at:

    obex.parallax.com/object/78
    It prints the result to an LCD (which I don't have, but you could examine the Spin file for the ADC to see how to send commands and read data.

    I hope this gives you a starting point.
    Tom
  • If you need a combo of speed & precision in a DIP device, get the MCP320x for single-ended and the MCP330x for differential. Even in a somewhat noisy environment, you can get 14 bits of precision with either one with 16x oversampling, while still converting 6Ksps @5V and 3Ksps @3.3V (using PASM with either SPIN or C). As stated above, there are drivers in the OBEX for the 3208; I have a very simple one in PASM for the 3304 that includes oversampling/decimation. You do need to run @ 88+ MHz to get (16)x6Ksps with it.

    If you need high precision and are OK with a few tens of sps, get the NAU7802; it's an I2C 24-bit sigma-delta in a DIP for about $2.50 the last time I checked. I've written a driver for it, but it's untested since I found that I could get what I need by using 1024-4096x oversampling with the MCP3304 and save board space, pins, & $$, since I needed it anyway. In any case, the NAU7802 uses standard I2C, so you can use the Simple Library functions to access it; the dedicated driver I wrote for it to maximize communication speed is superfluous when you're only getting a maximum of 80 sps (for 18 bits; 10 sps for 22 bits).
  • Jeff15 wrote: »
    I have a Quickstart Rev. A with a MCP3202 A/D converter, but I'm not sure if that's the A/D converter I want to use because I need a good amount of precision too. Does anyone have any recommendations for a good A/D converter as well?

    I've written an object for the MCP3xxx series ADCs in PropWare. You can find the example project here, which uses channel 1 of an MCP3000. To work with the MCP32xx series ADCs, simply change line 31 from
    const PropWare::MCP3000::PartNumber PART_NUMBER = PropWare::MCP3000::MCP300x
    
    to
    const PropWare::MCP3000::PartNumber PART_NUMBER = PropWare::MCP3000::MCP320x;
    

    and you'll be good to go. If you'd like help gathering all of the files necessary to make this work in SimpleIDE, let me know and I can upload a SimpleIDE archive.
  • jeff15,

    twm47099 already gave the link for SPI with C so here is the link for I2C.
    http://learn.parallax.com/propeller-c-simple-protocols/diy-i2c

    Unfortunately neither example involves an ADC but a very easy to use one is the ADC0831.
    You can find numerous examples of BS2 code for the ADC0831 and Propeller C has both the shiftin and shiftout functions that work just like the BS2 commands.

    Sometimes you don't even need an ADC.
    Mike Green mentioned using only 2 Propeller pins in what's called Sigma-Delta but I myself am not familiar with how it works.
    I myself was thinking I need an ADC because I was converting code written for another Microcontroller, but I realized I can just use rc_time to get a pot value just like on the BS2.

    I started with a QuickStart but the Activity Board is so much more useful and it comes with an ADC.
    Either save up for it or wait for it to go on sale.
    Propeller C also has functions for the Activity Board's ADC.
    You can see some of them under CO
  • Jeff15 wrote: »
    Can someone post an example of code for using an A/D converter? I have a Quickstart Rev.A, and I'm trying to learn how to use C to program it. (It would be a bonus if you would also explain how the code works)

    Also, I was wondering if you could reference another program with C in Simple IDE, and if that other program had to be in C or if it could be in Spin?

    Thanks in advance!

    if you have an ADC0831here's the code and the schematics below I'll be making a vid explaining the code in a few... : ]
    #include "simpletools.h"                      // Include simple tools
    
    const int DATA = 2;
    const int CLK = 1; 
    const int CS = 0;
    
    int main()                                    // Main function
    {   
      while(1)
      {  
        high(CS);
        low(CLK);
      
        low(CS);
        int ADC0831 = shift_in(DATA, CLK, MSBPOST, 9);
        high(CS);
        putChar(HOME);
        print("ADC0831 = %.2f V%c\n", ADC0831 * 0.0196);  
        pause(100);
      }  
    }
    
  • Schematics
    960 x 540 - 109K
Sign In or Register to comment.