Teensy LC SPI problem xra1402ig16tr-f

Kdub

New member
Hello everyone, this is my first post and I thank you for any help you can give.
I have a teensy LC and am trying to add two SPI GPIO expanders ICs to it. The expansion modules are the xra1402ig16tr-f and the datasheet can be found here. I have some simple code that I was attempting to use to test and make sure that the IC's were working properly. The code (below) enables the pullup resistors on the ICs, reads data from both ICs every 100ms, and then outputs the data of each bit on the serial monitor. However when I run the code it only returns zero. I should get 255 because 8 input bits are being returned and the pullup resistors are being enabled. The weird thing is that when I comment out the line "data_1 = SPI.transfer(0x80)" but leave all the same wiring I then get a response from the other module and it works as intended. I get 255 and and when I short one of the input pins to GND the respective bit goes low. The same thing happens the other way around; if I comment out the line "data_2 = SPI.transfer(0x80)" I get a response for data_1 = SPI.transfer(0x80) and the IC works as intended.

On top of that, I have taken my multimeter and found that all the pullup resistors on the pins are turning on for both ICs no matter what the rest of the code says. So both are responding to their respective "SPI.transfer16(0x8FF)" in void setup which sets the pullup resistors to HIGH. The same IC's pins won't go high if I comment out its "SPI.transfer16(0x8FF)" or comment out its digitalWrite(ss, LOW) before that. This leads me to believe that my wiring is correct.

I don't have a good way to show the wiring but here is how it is wired in reference to the data sheet:

IC_1 Teensy LC
CS# ---> 8
SI -----> 11
Reset# -> 4
GND ---> GND
VCC ----> 3.3V
SO -----> 12
SCL ----> 13
IRQ# ---> none

IC_2 Teensy LC
CS# ----> 7
SI ------> 11
Reset# -> 5
GND ---> GND
VCC ----> 3.3V
SO -----> 12
SCL ----> 13
IRQ# --> none

I tried to make the code below hopefully easy to follow. Any variable with an _1 is reference to IC1 and _2 for IC2. I hope that it is something obvious that I am overlooking and again thank you to anyone that can help.:)

Code:
#include <SPI.h>
// IO expander Slave Selects 
const int ss_1 = 8;
const int ss_2 = 7;

const int reset_1 = 4;
const int reset_2 = 5;

// IC 1
  bool P7_1;
  bool P6_1;
  bool P5_1;
  bool P4_1;
  bool P3_1;
  bool P2_1;
  bool P1_1;
  bool P0_1;

// IC 2
  bool P7_2;
  bool P6_2;
  bool P5_2;
  bool P4_2;
  bool P3_2;
  bool P2_2;
  bool P1_2;
  bool P0_2;

  int data_1;
  int data_2;
  
void setup() {
  SPI.begin();
  Serial.begin(9600);

  // Set SS Pins to HIGH
  pinMode(ss_1, OUTPUT);
  digitalWrite(ss_1, HIGH);
  pinMode(ss_2, OUTPUT);
  digitalWrite(ss_2, HIGH);

  // Set Reset Pin State
  pinMode(reset_1, OUTPUT);
  digitalWrite(reset_1, HIGH);
  pinMode(reset_2, OUTPUT);
  digitalWrite(reset_2, HIGH);

  // Initalizing the pullUp resistors
  digitalWrite(ss_1, LOW);
  SPI.transfer16(0x8FF);
  digitalWrite(ss_1, HIGH);
  digitalWrite(ss_2, LOW);
  SPI.transfer16(0x8FF);
  digitalWrite(ss_2, HIGH);
}

void loop() {
  // Getting the value of the register for input states IC1
  digitalWrite(ss_1, LOW);
  data_1 = SPI.transfer(0x80);
  digitalWrite(ss_1, HIGH);
  
  // Setting the Value of each bit for IC1 to a bool variable representing each input
  P7_1 = bitRead(data_1, 7);
  P6_1 = bitRead(data_1, 6);
  P5_1 = bitRead(data_1, 5);
  P4_1 = bitRead(data_1, 4);
  P3_1 = bitRead(data_1, 3);
  P2_1 = bitRead(data_1, 2);
  P1_1 = bitRead(data_1, 1);
  P0_1 = bitRead(data_1, 0);

  // Getting the value of the register for input states IC2
  digitalWrite(ss_2, LOW);
  data_2 = SPI.transfer(0x80);
  digitalWrite(ss_2, HIGH);
  
  // Setting the Value of each bit for IC2 to a bool variable representing each input
  P7_2 = bitRead(data_2, 7);
  P6_2 = bitRead(data_2, 6);
  P5_2 = bitRead(data_2, 5);
  P4_2 = bitRead(data_2, 4);
  P3_2 = bitRead(data_2, 3);
  P2_2 = bitRead(data_2, 2);
  P1_2 = bitRead(data_2, 1);
  P0_2 = bitRead(data_2, 0);

  // Printing the Output to Serial Monitor
  Serial.print("Data 1: ");
  Serial.print(data_1);
  Serial.print('\t');

  Serial.print("P7_1: ");
  Serial.print(P7_1);
  Serial.print('\t');

  Serial.print("P6_1: ");
  Serial.print(P6_1);
  Serial.print('\t');

  Serial.print("P5_1: ");
  Serial.print(P5_1);
  Serial.print('\t');

  Serial.print("P4_1: ");
  Serial.print(P4_1);
  Serial.print('\t');

  Serial.print("P3_1: ");
  Serial.print(P3_1);
  Serial.print('\t');

  Serial.print("P2_1: ");
  Serial.print(P2_1);
  Serial.print('\t');

  Serial.print("P1_1: ");
  Serial.print(P1_1);
  Serial.print('\t');

  Serial.print("P0_1: ");
  Serial.print(P0_1);
  Serial.print('\t');
  Serial.print('\t');

  Serial.print("Data 2: ");
  Serial.print(data_2);
  Serial.print('\t');

  Serial.print("P7_2: ");
  Serial.print(P7_2);
  Serial.print('\t');

  Serial.print("P6_2: ");
  Serial.print(P6_2);
  Serial.print('\t');

  Serial.print("P5_2: ");
  Serial.print(P5_2);
  Serial.print('\t');

  Serial.print("P4_2: ");
  Serial.print(P4_2);
  Serial.print('\t');

  Serial.print("P3_2: ");
  Serial.print(P3_2);
  Serial.print('\t');

  Serial.print("P2_2: ");
  Serial.print(P2_2);
  Serial.print('\t');

  Serial.print("P1_2: ");
  Serial.print(P1_2);
  Serial.print('\t');

  Serial.print("P0_2: ");
  Serial.println(P0_2);
  
  delay(100);

}
 
Here is an example of the output with the same wiring each time.

"data_1 = SPI.transfer(0x80)" and "data_2 = SPI.transfer(0x80)" both active:
Both.jpg

"data_1 = SPI.transfer(0x80)" commented and "data_2 = SPI.transfer(0x80)" active:
Data1_disabled.jpg

"data_1 = SPI.transfer(0x80)" active and "data_2 = SPI.transfer(0x80)" commented:
Data2_disabled.jpg
 
Back
Top