using mcp3208x lib with multiple adc's

I have 2 mcp3208 adc’s. on a custom bd. The 1st (ch0 - ch7) is selected by Pin 10, the second by pin 9.
(ch8- 15).

When i change to the second adc I subtract 8 from the channel (0-15). I also change the select pin
AH_MCP320x ADC_SPI() and disble the select pin from the other ADC.

I get valid data out but…

When I run the code attached the printout values repeat 0 and 8 1 and 9 all the way up to 7 and 15.

Appreciate a look .

Thanks :slight_smile:

  / DATAOUT  11   //MOSI
// DATAIN   12   //MISO
// SPICLOCK 13   //Clock

#include "AH_MCP320x.h"
#include <SPI.h>

// tests ch 0 to 15 of the Robtrol clone
//outputs on pin 2 tx

int ADCValue = 0;

AH_MCP320x ADC_SPI(10);             //Declaration required

void setup()
{
  Serial.begin(9600);
}
void loop(){

Serial.println ("       adc lib test");

for(int i=0;i<17;i++){
  int val = ADCread(i); 
    Serial.print ("ch");
    Serial.print("\t");
    Serial.print (i);
    Serial.print("\t");
    Serial.print ("value");
    Serial.print("\t");
    Serial.println (val,HEX);
}
}
int ADCread(int channel){
  
  int ADCValue = 0;
    if (channel < 8 )
    {
      bitSet(PORTB,1);        //DELESECT                  
      AH_MCP320x ADC_SPI(10);    //select adco
    }
    else
    {
      bitSet(PORTB,2);      //DELESECT  
      AH_MCP320x ADC_SPI(9);  //select adc1
      channel-= 8;
    }
  
  ADCValue = ADC_SPI.readCH(channel);      //read analog value from IC (SPI mode)
  return ADCValue;
}

Why not:

Declare 2 global objects:
AH_MCP320x ADC_SPI1(10 ) ;
AH_MCP320x ADC_SPI2(9 );

and in your function ADCread() simply select the appropriate object depending on the channel number e.g.
ADCValue = ADC_SPI1.readCH(channel) ;
Or
ADCValue = ADC_SPI2.readCH(channel) ;
instead of the re-declarations.

If you still have trouble, use pinMode() and digitalWrite () explicitly to control the select pins.
Your for statement should have upper bound 15.