64 channel multiplexer with read and write

Hello,

I am trying to develop a 64-channel multiplexer board with both read and write options for each channel so that at the end I can inject current through one and reach voltage from the other. I wish to control it using a nano by SPI. I use ADG731 and ADG738 ICs (datasheets in attachment), where I use two 731s ([32:1] x 2) in conjunction with ADG819 [2:1] mux for reading output from a channel. On the otherhand, I use eight 738s ([8:1] x 2) for writing into a channel. Now, when I code the nano as shown below, I observe that all my channels show the same output when I try to read, whilst I code to only use one switch. The same thing is happening to read as well.

#include <SPI.h>

#define sclkPin 13
#define sync_u1 2
#define sync_u2 12
#define adg819_in 7
#define mosiPin 11
#define sync_write A5
#define reset_ic1 A0
#define reset_ic2 A1
#define reset_ic3 A2
#define reset_ic4 A3
#define reset_ic5 3
#define reset_ic6 4
#define reset_ic7 5
#define reset_ic8 6

int readEnabled = 0;
int writeEnabled = 0;

void setup() {

Serial.begin(9600);
SPI.begin();

pinMode(mosiPin, OUTPUT);
pinMode(sclkPin, OUTPUT);
pinMode(adg819_in, OUTPUT);

pinMode(sync_write, OUTPUT);
digitalWrite(sync_write, HIGH);

pinMode(sync_u1, OUTPUT);
digitalWrite(sync_u1, HIGH);

pinMode(sync_u2, OUTPUT);
digitalWrite(sync_u2, HIGH);

pinMode(reset_ic1, OUTPUT); // TODO: set output it for all reset pins connected
digitalWrite(reset_ic1, LOW); // TODO: set high it for all the reset pins connected

pinMode(reset_ic2, OUTPUT);
digitalWrite(reset_ic1, LOW);

pinMode(reset_ic3, OUTPUT);
digitalWrite(reset_ic1, LOW);

pinMode(reset_ic4, OUTPUT);
digitalWrite(reset_ic1, LOW);

pinMode(reset_ic5, OUTPUT);
digitalWrite(reset_ic1, LOW);

pinMode(reset_ic6, OUTPUT);
digitalWrite(reset_ic1, LOW);

pinMode(reset_ic7, OUTPUT);
digitalWrite(reset_ic1, LOW);

pinMode(reset_ic8, OUTPUT);
digitalWrite(reset_ic1, LOW);

delay(10);

disable_all();
enable_write(64);
}

void loop() {

// TODO: What interface is needed here?
// I’m providing some functions to interface the pogo pins → disable_all, enable_read, enable_write
}

void _switch_u1_u2(int pinNumber, bool u1)
{
if (u1)
digitalWrite(sync_u1, LOW);
else
digitalWrite(sync_u2, LOW);
if(pinNumber > 32 || pinNumber < 1)
SPI.transfer(1 << 7); // Switches off all the pins
else
SPI.transfer(pinNumber - 1);

if (u1)
digitalWrite(sync_u1, HIGH);
else
digitalWrite(sync_u2, HIGH);

delay(10);
}

void _switch_writer_ic(int switchNumber, int icNumber)
{
digitalWrite(sync_write, LOW);
SPI.transfer(1 << switchNumber);
digitalWrite(sync_write, HIGH);

if (icNumber == 0)
digitalWrite(reset_ic1, HIGH);
if (icNumber == 1)
digitalWrite(reset_ic2, HIGH);
if (icNumber == 2)
digitalWrite(reset_ic3, HIGH);
if (icNumber == 3)
digitalWrite(reset_ic4, HIGH);
if (icNumber == 4)
digitalWrite(reset_ic5, HIGH);
if (icNumber == 5)
digitalWrite(reset_ic6, HIGH);
if (icNumber == 6)
digitalWrite(reset_ic7, HIGH);
if (icNumber == 7)
digitalWrite(reset_ic8, HIGH);

delay(10);
}

void _disable_read()
{
digitalWrite(sync_u1, LOW);
digitalWrite(sync_u2, LOW);
SPI.transfer(1<<6);
digitalWrite(sync_u1, HIGH);
digitalWrite(sync_u2, HIGH);
readEnabled = 0;
delay(10);
}

void _disable_write()
{
digitalWrite(sync_write, HIGH);
digitalWrite(reset_ic1, LOW);
digitalWrite(reset_ic2, LOW);
digitalWrite(reset_ic3, LOW);
digitalWrite(reset_ic4, LOW);
digitalWrite(reset_ic5, LOW);
digitalWrite(reset_ic6, LOW);
digitalWrite(reset_ic7, LOW);
digitalWrite(reset_ic8, LOW);
writeEnabled = 0;
delay(10);
}

void disable_all()
{
_disable_write();
_disable_read();
}

void enable_read(int pinNumber)
{
if(writeEnabled == pinNumber)
{
Serial.println(String("Pin “) + pinNumber + " already in write mode, cannot enable read”);
delay(10);
return;
}
else if(readEnabled > 0)
_disable_read();

if (pinNumber < 1 || pinNumber > 64)
Serial.println(“Valid pin number between 1 and 64 needed”);
else if (pinNumber > 32)
{
_switch_u1_u2(pinNumber-32, false);
digitalWrite(adg819_in, HIGH);
}
else
{
_switch_u1_u2(pinNumber, true);
digitalWrite(adg819_in, LOW);
}
readEnabled = pinNumber;
delay(10);

}

void enable_write(int pinNumber)
{
if(readEnabled == pinNumber)
{
Serial.println(String("Pin “) + pinNumber + " already in read mode, cannot enable write”);
delay(10);
return;
}
else if(writeEnabled > 0)
_disable_write();

int icNumber = pinNumber >> 3; // divide by 8
int switchNumber = pinNumber & 0x7; // last 3 bits

_switch_writer_ic(switchNumber, icNumber);
writeEnabled = pinNumber;
delay(10);
}

ADG725_731.pdf (258 KB)

ADG738_739.pdf (345 KB)

Mod.txt (4.15 KB)

Why do you assume that you have a coding problem? Perhaps it is a wiring problem.

Your code was small enough to post inline.

IN CODE TAGS!