Trouble compiling with AH_MCP320x.h lib

Atempting to use this lib, woul like to use two seperate chip selects for 2 MCP3208 chips.

Getting errors

// 4/7/2016
#include <AH_MCP320x.h>
#include <SoftwareSerial.h>

#define tx A4              //pin 28
#define MISO 12       //pin 18
#define MOSI 11       // pin 17
# define SCK 13             //pin 19
# define adc0_select 9         // pin 15
# define adc1_select 10         // pin 16
# define led 13               //uno not std  board


SoftwareSerial mySerial(2,A4); /* rx,tx*/

 setCONFIG_allSingle(8);
/*******************************    SETUP RUN ONCE    *********************************************************/


/*prototypes*/
int new_readADC(int ch);
/*******************************/

void setup()
{

   

  mySerial.begin(9600);
  mySerial.println("Analog Bd online");
  delay(2000);

  pinMode(adc0_select, OUTPUT);                   // SPI chip selects
  pinMode(adc1_select, OUTPUT);                     //SPI chip selects
  pinMode(MOSI, OUTPUT);                      //SPI master out slave in
  pinMode(SCK, OUTPUT);                     //SPI clock
  pinMode(MISO, INPUT);                     //SPI master in slave out
  pinMode(tx, OUTPUT);

}

void loop() {
  
  
  for (int ch = 0; ch < 16; ch++){
    
    int adc_val = new_readADC(ch);
    delay(100);
    mySerial.print("adc ch " );
    mySerial.print(ch);
    mySerial.print(" value "  );
    mySerial.println(adc_val );
    delay(500);
    digitalWrite(13, LOW);
  }
}



int new_readADC(int ch){

  int cs;
  int val;

  if (ch < 8)  {
    cs = adc0_select;
  }

  else{
    cs = adc1_select;
  }
  AH_MCP320x(cs, MOSI, MISO, SCK);
  val = readCH(ch);
  return val;
}

Could someone look at this and try to compile?

Thanks

Getting errors

But not sharing...

Could someone look at this and try to compile?

I don't have the AH_MCP320x library, and you didn't say where to get it, so, no.

You definitely can’t do what you’re trying to do.

You need to globally declare two instances of “AH_MCP320x” and give each it’s own name, passing a different chip select pin to each, something like this:-

AH_MCP320x ahm1(adc0_select, MOSI, MISO, SCK);
AH_MCP320x ahm2(adc1_select, MOSI, MISO, SCK);

Then, you call ‘readCH()’ for each separately:-
(For this example, I’ve set both to channel 0, but you’ll need to set things up how you want in your code.)

ahm1.readCH(0);
ahm2.readCH(0);

Didn’t you take any notice of how it was done in the examples for a single MCP320x?
Note the instance name, (ADC_pin).

AH_MCP320x ADC_pin(10,11,12,13);

Then note that the name is used in the call to ‘readCH()’:-

ADC_pin.readCH(0);

This cannot work:-

int new_readADC(int ch)
{
    int cs;
    int val;
    if (ch < 8)
    {
        cs = adc0_select;
    }
    else
    {
        cs = adc1_select;
    }
    AH_MCP320x(cs, MOSI, MISO, SCK);
    val = readCH(ch);
    return val;
}

It shouldn’t be too hard to rewrite your code properly. Just add the declarations for the two instances at the top of your sketch, then rewrite your ‘int new_readADC()’ function to suit.

Edit: And if you need to call this, you’ll need to call it once for each instance:-

ahm1.setCONFIG_allSingle(8);
ahm2.setCONFIG_allSingle(8);

And it must be inside a function. ‘setup()’ I guess.

Edit2: You’ll also need to include the “SPI” library, so your includes should look like this:-

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

For anyone else following this thread, the “AH_MCP320x” library is available here:-
Arduino Library für MCP3204/MCP3208 IC