Shop Learn P1 Docs P2 Docs
Propeller Flip with MCP3002 comms — Parallax Forums

Propeller Flip with MCP3002 comms

Hi there,
New to the propeller world.
I got myself a Flip, want to add the capability to read analog inputs from a sensor, in this case a temperature sensor, using a MCP3002

I guess the comms will be SPI, so....
1) someone out there can point in the right direction how to program the Flip to "talk" to MCP 3200
2) how many MCP3200 can be talking to the flip?
3) MCP3208 can be used as well?

Thats it for now

Comments

  • JonnyMacJonnyMac Posts: 8,238
    edited 2022-09-04 16:41

    Easy-peasy. I don't have an MCP3002, but I have used the MCP3202 (12-bit), so I made a copy and modified it for the 10-bit variant. I have used the MCP3208 many times. My friend John at jonyJib.com uses two of these in many of his products. You can share the SPI bus pins and use a chip select for each; five IOs will give you 16 analog channels. Note that if you want to use 5v for the ADC, put a 4.7K resistor between the DO pin of the ADC and the MISO connection to the Propeller.

    If you wanted to read sixteen channels with the object you could create two copies like this:

    obj
    
      adclo : "jm_mcp3208_spi"
      adchi : "jm_mcp3208_spi"
    

    You have to start them; I tend to do this in a method called setup():

      adclo.start(CS0, SCLK, MOSI, MISO)
      adchi.start(CS1, SCLK, MOSI, MISO)
    

    Then, let's say your app wants to do single-ended readings of 16 channels; you could write a shell interface like this:

    pub read_adc(ch) : result
    
      if (ch => 0) and (ch =< 7)
        return adclo.read(ch, adclo#SE)
      elseif (ch => 8) and (ch =< 15)
        return adchi.read(ch-8, adchi#SE)
      else
        return -1
    

    Of course, you should not take my word for anything, you should hook up a circuit and test it. Talk is cheap. Doing is valuable. Doing is better.

    Have fun!

  • DigitalBob/JM

    I appreciate your help and time but the propeller flip is mainly programmed in C++, because of that my application is being developed in c++. Don't know how to "translate" that to c++

    Regards,

  • Not sure if you've already been down this road, but there's an ASM driver at GitHub that might be close to what you need ? : https://github.com/parallaxinc/propeller/tree/master/libraries/community/p1/All/ADC MCP3204 SPI Driver

    If you're developing in C using FlexProp, I think you can include ASM libraries ? (hmm, Probably you could include SPIN also that way, and use Jon's example... you might have two possible solutions depending on the IDE you are working with).

  • Arh-- I see DigitalBob already posted much the same example in ASM. So seems you might need some help integrating that to your IDE.... What are you using ?

  • I'm using a propeller flip 32123 with an MCP3002 for know just to get familiar with it (thats what I have laying around) later on would like to use a MCP3204.
    I found a library in SimpleIDE but I'm investigating now how can I add that library to my libraries folder to test.

  • I'd think if nothing else, SimpleIDE will look in the folder your code is in.
    I've added some other ideas to your other thread : https://forums.parallax.com/discussion/174816/how-to-add-a-downloaded-library-to-my-library-folder-simpleide

  • JonnyMacJonnyMac Posts: 8,238
    edited 2022-09-05 20:25

    I appreciate your help and time but the propeller flip is mainly programmed in C++

    No, the FLiP is not mainly programmed in C++, it is programmed in a variety of languages, including it's native language, Spin (which exposes everything about the Propeller architecture, hence is good to know, even if you choose to use C for your application ). I have translated thousands of lines of C to Spin, so the question becomes: What's stopping you from translating my simple Spin code to C (other than the very small effort required to do it)? It would be a fun exercise and get you closer to the P1.

    Can't be bothered? Eric Smith's FlexProp is quite popular with C programmers and he has a mechanism that allows C programs to use Spin objects as libraries.

    #include <stdio.h>
    #include <propeller.h>
    
    #define  CS0  7
    #define  SCLK 6
    #define  MISO 5
    #define  MOSI 4
    
    
    struct __using("jm_mcp3204_spi.spin") adc;
    
    void main()
    {
        uint8_t  ch;
        uint16_t level;
    
        adc.start(CS0, SCLK, MOSI, MISO);
    
        while(1) {
            for(ch = 0; ch < 4; ch++) {
                level = adc.read(ch, adc.SE);
                printf("Ch %d: %4d\r", ch, level);
            }
            printf("\n");
            _waitms(1000);
        }
    }
    

    This works. I tested on a P1-powered (same as the FLiP) EFX-TEK HC-8 controller with an ADC-4 (MCP3204) add-on board) (I designed both boards for EFX-TEK).

  • JonnyMacJonnyMac Posts: 8,238
    edited 2022-09-05 22:55

    This also works, demonstrating that with just a little effort, Spin can be translated to C (again, I'm using FlexProp because it seems to be the most popular among Propeller users who prefer C).

    #include <stdio.h>
    #include <propeller.h>
    
    #define  CS0  7
    #define  MISO 5
    #define  MOSI 4
    #define  SCLK 6
    
    
    void     setupAdc();
    uint16_t readAdcSE(uint8_t ch);
    
    
    void main()
    {
        uint8_t  ch;
        uint16_t level;
    
        setupAdc();
    
        while (1) {
            for(ch = 0; ch < 4; ch++) {
                level = readAdcSE(ch);
                printf("Ch %d: %4d\r", ch, level);
            }
            printf("\n");
            _waitms(1000);
        }
    }
    
    
    void setupAdc()
    {
        _pinh(CS0);
        _pinl(SCLK);
        _pinl(MOSI);
        _pinf(MISO); 
    }
    
    
    uint16_t readAdcSE(uint8_t ch)
    {
        uint8_t  x;
        uint8_t  mux;
        uint16_t level = 0;
    
        if (ch > 3) return 0;                           // 0 if bad channel #
    
        _pinl(CS0);                                     // activate ADC
    
        mux = 0b11000 | ch;                             // setup mux bits, single-ended
    
        for (x = 0; x < 5; x++) {                       // output 5 mux bits, MSBFIRST
            _pinw(MOSI, (mux & 0b10000) >> 4); 
            _pinh(SCLK);
            _pinl(SCLK);
            mux <<= 1;
        }
    
        for (x = 0; x < 13; x++) {                      // read null + 12 data bits, MSBFIRST
            _pinh(SCLK);
            _pinl(SCLK);
            level = (level << 1) | _pinr(MISO);
        }
    
        _pinh(CS0);                                     // deactivate
    
        return level & 0xFFF;                           // clean-up & return 
    }
    
  • @JonnyMac said:
    ... I'm using FlexProp because it seems to be the most popular among Propeller users who prefer C).

    Thumbs-up for that.

    Trying out and comparing the two IDEs for your own preference well recommended. SimpleIDE has it's quirks, not least because it hides away functionality in an attempt to simplify the coding process for a certain audience, whereas FlexProp is functionally open and accessible, especially more likely to appeal to an existing C coder.

    The actively developed FlexProp in many ways is also more capable, compared to the many-years-old and end-of-development SimpleIDE project. Good to try both and choose what suits your style.

Sign In or Register to comment.