Using multiple channels on the MCP3208
CumQuaT
Posts: 156
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 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?
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
Try ADC.start(20, 21, 18, 3) to enable both channels 0 and 1.
%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)
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.