Shop OBEX P1 Docs P2 Docs Learn Events
Using multiple channels on the MCP3208 — Parallax Forums

Using multiple channels on the MCP3208

CumQuaTCumQuaT Posts: 156
edited 2010-12-08 06:19 in Propeller 1
Hello all.

I'm using an object from the OBEX to get fancy with a set of new MCP3208 12bit ADCs I just purchased, but am having difficulty getting them to behave when dealing with multiple input channels. The chip in question has 8 inputs, but no matter what I do, only channel 0 will work for me. First of all, this is the object I'm using:

http://obex.parallax.com/objects/180/

And here is the code I'm using:
    ADC.Start(20, 21, 18, 1)

    Current := ADC.in(0)
    IF (Current < Lowest)
      Lowest := Current
    waitcnt(clkfreq / 10 + cnt)
    Current := ADC.in(1)
    IF (Current > Highest)
      Highest := Current
    waitcnt(clkfreq / 10 + cnt)

(ADC is the MCP3208 object)

Basically what happens is that using ADC.in(0) always returns the Analogue value nicely, however, using ADC.in(1) chucks a wobbly and returns 0. ALWAYS 0. Oh, and I've checked on the scope. There IS an analogue voltage going to that pin on the chip.

Does anyone have an idea why this is doing this? I'm fairly certain it has something to do with the MODE declared during the ADC.start function, but there's no good doco on how to use this object, and I've tried various other values in there and they all do the same thing...

Any advice would be great! Perhaps even from the great Mr Gracey who wrote the object?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-12-07 07:27
    You really have to read the documentation. In this case, the comments in the MCP3208 object for the .start method. The 4th parameter is a mode setting. There's an enable bit for each of the ADC channels (bits 0-7) and a bit for each channel that indicates whether that channel is single-ended or differential (bits 8-15). In your code, you only enable the 1st channel, so that's the only one that works.

    Try ADC.start(20, 21, 18, 3) to enable both channels 0 and 1.
  • CumQuaTCumQuaT Posts: 156
    edited 2010-12-07 08:29
    Thanks Mike, I did read that in the code, but it didnt make any sense to me. How would I enable other numbers? It doesn't really make sense to me how it works...
  • Mike GreenMike Green Posts: 23,101
    edited 2010-12-07 08:39
    The mode parameter value consists of a 16-bit number, divided up into two 8-bit numbers. Using the Spin notation for binary, this would be

    %0000_0000_0000_0000

    If I substitute bit numbers for the bits, you'd have

    %7654_3210_7654_3210

    Each bit corresponds to one channel of the ADC. The rightmost 8 bits enable (1) the channel or disable (0) the channel. The leftmost bits specify whether the channel is single-ended (0) or differential (1). What you gave in your program fragment was

    %0000_0000_0000_0001 = 1

    What I gave (to enable the 1st two channels) was

    %0000_0000_0000_0011 = 3

    If you want, you can substitute the binary notation for the decimal notation (it's all the same as far as Spin is concerned) like

    ADC.start(20, 21, 18, %0000_0000_0000_0011)

    You can leave out some of the underlines if you want like

    ADC.start(20, 21, 18, %00000000_00000011)
  • CumQuaTCumQuaT Posts: 156
    edited 2010-12-07 09:00
    I think I get it... So to enable them all, I would use %00000000_11111111 ?
  • Mike GreenMike Green Posts: 23,101
    edited 2010-12-07 09:06
    Yes, that's how you'd enable them all.
  • AJMAJM Posts: 171
    edited 2010-12-07 10:57
    That was a very clean explanation Mike. I liked the way you explained it
  • CumQuaTCumQuaT Posts: 156
    edited 2010-12-07 16:54
    Thanks again, Mike. Just for the sake of my learning, are you able to explain the process of translating the binary notation to the integer like you did?
  • Mike GreenMike Green Posts: 23,101
    edited 2010-12-07 21:27
    The process is identical to how you interpret numbers in any base. For example, 1234 is equivalent to 1 x 1000 + 2 x 100 + 3 x 10 + 4 x 1.
    The same thing (not the same value) in base 8 (octal) is 1 x 512 + 2 x 64 + 3 x 8 + 4 x 1 (using successive powers of 8)
    The same thing in base 16 (hexadecimal) is 1 x 4096 + 2 x 256 + 3 x 16 + 4 x 1 (using successive powers of 16)

    In binary, you only have 0 and 1, so the above examples won't work, but %0000_0011 is equivalent to
    0 x 128 + 0 x 64 + 0 x 32 + 0 x 16 + 0 x 8 + 0 x 4 + 1 x 2 + 1 x 1

    If you have further questions, read the Wikipedia article on Positional Notation.
  • CumQuaTCumQuaT Posts: 156
    edited 2010-12-08 06:19
    I think I understand now. Thanks for the help. This forum is, after all, a place of help and learning.
Sign In or Register to comment.