Can't get SPI to work on Teensy 3.2 [SOLVED]
I'm unable to get SPI to work reading 4 daisy chained CD4021 shift registers to read 32 switches that are pulled high. I'm an experienced Arduino developer, and the sample code shown below works fine testing on an Arduino Nano. I have tried running the shift registers on 5V, 3.3V & through a Sparkfun bi-directional logic level converter. The result is always the same: all bits are 1 in the 4 bytes I read. I have read somewhere that the CD4021s are troublesome with 3.3V, so I plan to try 74HC165s instead.
NOTE: Solution in post #8
Connections:
CD4021 pin 9 (Parallel/Serial Control) [On all chips] -> Teensy pin 10
CD4021 pin 3 (Q8) [On first chips] -> Teensy pin 12
CD4021 pin 10 (Clock) [On all chips] -> Teensy pin 13
First CD4021 pin 11 (Serial In) to next daisy-chained CD4021 pin 3 (Q8)
Last CD4021 pin 11 (Serial In) to GND
As I mentioned, the shift register circuit is in use on several Arduinos, so it works fine.
Interestingly enough if I use:
with the connections unchanged, I can read each one of the 32 switches connected to the shift registers. It appears it's this line that is failing:
I'm unable to get SPI to work reading 4 daisy chained CD4021 shift registers to read 32 switches that are pulled high. I'm an experienced Arduino developer, and the sample code shown below works fine testing on an Arduino Nano. I have tried running the shift registers on 5V, 3.3V & through a Sparkfun bi-directional logic level converter. The result is always the same: all bits are 1 in the 4 bytes I read. I have read somewhere that the CD4021s are troublesome with 3.3V, so I plan to try 74HC165s instead.
NOTE: Solution in post #8
- Teensy 3.2
- CD4021BM96 shift registers http://www.ti.com/lit/ds/symlink/cd4014b.pdf
- Arduino 1.6.12
- Teensyduino 1.31-beta1
- macOS Sierra 10.12
Code:
#include <SPI.h>
#define SS 10
byte bank1, bank2, bank3, bank4;
void setup() {
SPI.begin();
Serial.begin(57600);
pinMode(SS, OUTPUT);
}
void loop() {
digitalWrite(SS, HIGH); // Set latch pin to 1 to collect parallel data
delayMicroseconds(200);
digitalWrite(SS, LOW); // Set latch pin to 0 to transmit data serially
delayMicroseconds(200);
// read CD4021 IC
bank1 = SPI.transfer(0x00);
bank2 = SPI.transfer(0x00);
bank3 = SPI.transfer(0x00);
bank4 = SPI.transfer(0x00);
Serial.print("1:");
Serial.println(bank1, BIN); // Always 1:11111111
Serial.print("2:");
Serial.println(bank2, BIN);
Serial.print("3:");
Serial.println(bank3, BIN);
Serial.print("4:");
Serial.println(bank4, BIN);
Serial.println("-------------------");
delay(500); // Slow down serial monitor output
}
Connections:
CD4021 pin 9 (Parallel/Serial Control) [On all chips] -> Teensy pin 10
CD4021 pin 3 (Q8) [On first chips] -> Teensy pin 12
CD4021 pin 10 (Clock) [On all chips] -> Teensy pin 13
First CD4021 pin 11 (Serial In) to next daisy-chained CD4021 pin 3 (Q8)
Last CD4021 pin 11 (Serial In) to GND
As I mentioned, the shift register circuit is in use on several Arduinos, so it works fine.
Interestingly enough if I use:
Code:
shiftIn(DATAPIN, CLOCKPIN);
with the connections unchanged, I can read each one of the 32 switches connected to the shift registers. It appears it's this line that is failing:
Code:
SPI.transfer(0x00);
Last edited: