Please try the two sketches below.
Instructions are at the top of the sketches.
The first sketch uses a ShiftIn function to read the shift register data.
The second uses SPI to read the shift register data.
Both sketches work here on a 74HC165 but I do not have a CD4021B.
Please try your CD4021B on both sketches and let me know what your results are.
You will have to comment the #define HC165 line (near the top of the sketch) to get your CD4021B to work with the sketches.
Uses Arduino pins 9, 12 and 13
//
//74HC165 and CD4021B Using SPI.ino
//
//Example of using SPI to read the inputs from a shift register
//*****************************************************************************
//uncomment your part number line, comment the other line <------<<<<<
#define HC165
//#define CD4021
//*****************************************************************************
#ifdef HC165
#define LOAD LOW //data is loaded on a LOW going level
#define nLOAD HIGH
#else
#define LOAD HIGH //data is loaded on a HIGH going level
#define nLOAD LOW
#endif
//*****************************************************************************
//74HC165
//pin 1 (/PL) goes to LATCH (D9) LOW loads the register <------<<<<<
//pin 2 (CP) goes to SCK (D13)
//pin 9 (Q7) goes to MISO (D12)
//CD4021B
//pin 9 (PL) goes to LATCH (D9) HIGH loads the register <------<<<<<
//pin 10 (CP) goes to SCK (D13)
//pin 3 (Q7) goes to MISO (D12)
#include <SPI.h>
const byte LATCH = 9;
int shiftRegisterInputs;
//*****************************************************************************
void setup ()
{
SPI.begin ();
Serial.begin (9600);
pinMode (LATCH, OUTPUT);
digitalWrite (LATCH, nLOAD);
} //END of setup
//*****************************************************************************
void loop ()
{
digitalWrite (LATCH, LOAD);
digitalWrite (LATCH, nLOAD);
shiftRegisterInputs = SPI.transfer (0);
Serial.print("switchRegisterInputs = ");
printBits(shiftRegisterInputs);
delay (500);
} //END of loop
//*****************************************************************************
//print in binary with leading zeros
void printBits(unsigned int var)
{
for (unsigned int x = 0x80; x; x = x >> 1)
{
Serial.print(var & x ? 1 : 0);
}
//Or use:
// for (int x = 7; x >= 0; x--)
// {
// if ((var & 1 << x) != 0)
// {
// Serial.print(1);
// }
// else
// {
// Serial.print(0);
// }
// }
Serial.print("\n");
} //END of printBits()
Uses Arduino pins 9, 12 and 13
//
//74HC165 and CD4021B.ino
//
//Example of a shiftIn function to read the inputs from a shift register, however,
//SPI is a better way to get the same inputs.
//*****************************************************************************
//uncomment your part number line, comment the other line <------<<<<<
#define HC165
//#define CD4021
//*****************************************************************************
#ifdef HC165
#define LOAD LOW //data is loaded on a LOW going level
#define nLOAD HIGH
#else
#define LOAD HIGH //data is loaded on a HIGH going level
#define nLOAD LOW
#endif
//*****************************************************************************
//74HC165
//pin 1 (/PL) LOW loads the register <------<<<<<
//pin 2 (CP)
//pin 9 (Q7)
//CD4021B
//pin 9 (PL) HIGH loads the register <------<<<<<
//pin 10 (CP)
//pin 3 (Q7)
int latchPin = 9; // PL
int dataPin = 12; // Q7
int clockPin = 13; // CP
int switchRegisterInputs;
// s e t u p ( )
//*****************************************************************************
void setup()
{
Serial.begin(9600);
pinMode(latchPin, OUTPUT);
digitalWrite(latchPin, nLOAD);
pinMode(clockPin, OUTPUT);
digitalWrite(clockPin, LOW);
pinMode(dataPin, INPUT);
}
// l o o p ( )
//*****************************************************************************
void loop()
{
digitalWrite(latchPin, LOAD);
delayMicroseconds(2);
digitalWrite(latchPin, nLOAD);
delayMicroseconds(2);
switchRegisterInputs = ShiftIn(dataPin, clockPin);
Serial.print("switchRegisterInputs = ");
printBits(switchRegisterInputs);
delay (500);
} //END of loop()
// S h i f t I n ( )
//*****************************************************************************
byte ShiftIn(byte DataPin, byte ClockPin)
{
byte temp = 0;
byte DataIn = 0;
for (char i = 7; i >= 0; i--)
{
temp = digitalRead(DataPin);
if (temp == HIGH)
{
DataIn = DataIn | (1 << i);
}
//advance to the next bit
digitalWrite(ClockPin, HIGH);
digitalWrite(ClockPin, LOW);
}
return DataIn;
} //END of ShiftIn()
// p r i n t B i t s ( )
//*****************************************************************************
//print in binary with leading zeros
void printBits(unsigned int var)
{
for (unsigned int x = 0x80; x; x = x >> 1)
{
Serial.print(var & x ? 1 : 0);
}
//Or use:
// for (int x = 7; x >= 0; x--)
// {
// if ((var & 1 << x) != 0)
// {
// Serial.print(1);
// }
// else
// {
// Serial.print(0);
// }
// }
Serial.print("\n");
} //END of printBits()
Attached are the two sketches.
_74HC165_and_CD4021B_using_SPI.ino (2 KB)
_74HC165_and_CD4021B.ino (2.72 KB)