Shop OBEX P1 Docs P2 Docs Learn Events
MCP3208 code — Parallax Forums

MCP3208 code

72sonett72sonett Posts: 82
edited 2013-08-16 15:03 in BASIC Stamp
I am planning a project wih an MCP3208 ADC, got the https://www.parallax.com/Portals/0/Downloads/docs/prod/compshop/MCP3208Demo.zip example code and I understand how that works, but one line puzzles me;
...
[B]Offset [/B]         CON     24       
...
SHIFTOUT DataIn, Clock, MSBFIRST, [ [B]Offset |[/B] channel ]

What does the ´Offset | channel´ part do? Why do I need an offset? The example code does not explain that.
..

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-08-16 12:04
    If you read the datasheet for the MCP3208, you'll see that the microcontroller has to send a 5-bit value to the MCP3208 to start a conversion. The most significant bit of this is always a 1 and the next bit is a 1 if the MCP3208 functions as a single-ended device (measuring the voltage relative to ground) vs. a differential measurement. The least significant 3 bits contain the channel number, so you're always adding 24 to the channel number. The "Offset | channel" effectively does that.
  • 72sonett72sonett Posts: 82
    edited 2013-08-16 13:07
    Thanks, that makes sense, but I found a datasheet at http://ww1.microchip.com/downloads/en/DeviceDoc/21298e.pdf that on page 19, table 5.1, only mentions a control nibble (4 bits) to be sent to the ADC's Din pin;
    bit 4 = 1 for single ended analog input, 0 for differential
    bits 3..0 = channel 0 .. 7 (BIN000 ..BIN111)
    so why not just add 8 to the channel number for single ended input?
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-08-16 14:19
    It also needs the start bit to be a "1", so for single ended you either ADD or OR 24 to the channel number. You are adding 8 for single ended and 16 for the obligatory start bit.
    [SIZE=1][FONT=courier new]starter CON %10000
    single CON %1000
    startSingle CON starter+single  ' = 24
    [/FONT][/SIZE]
    [FONT=courier new][/FONT][SIZE=1][FONT=courier new]' all equivalent:
    SHIFTOUT DataIn, Clock, MSBFIRST, [ startSingle | channel ]
    SHIFTOUT DataIn, Clock, MSBFIRST, [ 24 + channel ]
    SHIFTOUT DataIn, Clock, MSBFIRST, [ starter + single + channel ][/FONT][/SIZE]
    
  • 72sonett72sonett Posts: 82
    edited 2013-08-16 15:03
    Right, in the data sheet figure 6-1 it shows that start bit, making 5 in total... I thought making CS low was enough to start the ADC.

    I'll use
    SHIFTOUT ADCdataIn, ADCclock, MSBFIRST, [BIN11000 + channel]
    
Sign In or Register to comment.