Hi,
I have 3 74HC165in series, the first two(right and middle in the schematic) give me the correct value, but the 3rd (left)shows always on for all 8 inputs...
Trying to debug I found out that the 74HC165 that is connected to the arduino pin D11 gives always 1s. That does not make sense (at least to me), if there was a problem in hardware none of the 3 IC should work...
What I get as output is buttons 0 to 15 working, buttons 16 to 23 always on.
The code I am using is below, the schematic is attached.
Thanks.
#include <UTFT.h>
// How many shift register chips are daisy-chained.
#define NUMBER_OF_SHIFT_CHIPS 3
/* Width of data (how many ext lines).
*/
#define DATA_WIDTH NUMBER_OF_SHIFT_CHIPS * 8
/* Width of pulse to trigger the shift register to read and latch.
*/
#define PULSE_WIDTH_USEC 5
/* Optional delay between shift register reads.
*/
#define POLL_DELAY_MSEC 1
/* You will need to change the "int" to "long" If the
* NUMBER_OF_SHIFT_CHIPS is higher than 2.
*/
#define BYTES_VAL_T unsigned long
extern uint8_t BigFont[];
UTFT myGLCD (ILI9325D_8,38,39,40,41);
int ploadPin = 8; // Connects to Parallel load pin the 165
int clockEnablePin = 9; // Connects to Clock Enable pin the 165
int dataPin = 11; // Connects to the Q7 pin the 165
int clockPin = 12; // Connects to the Clock pin the 165
int b[24]; //keeps the value of each switch (TO PRINT)
BYTES_VAL_T pinValues;
BYTES_VAL_T oldPinValues;
/* This function is essentially a "shift-in" routine reading the
* serial Data from the shift register chips and representing
* the state of those pins in an unsigned integer (or long).
*/
BYTES_VAL_T read_shift_regs()
{
byte bitVal;
BYTES_VAL_T bytesVal = 0;
/* Trigger a parallel Load to latch the state of the data lines,
*/
digitalWrite(clockEnablePin, HIGH);
digitalWrite(ploadPin, LOW);
delayMicroseconds(PULSE_WIDTH_USEC);
digitalWrite(ploadPin, HIGH);
digitalWrite(clockEnablePin, LOW);
/* Loop to read each bit value from the serial out line
* of the SN74HC165N.
*/
for(int i = 0; i < DATA_WIDTH; i++)
{
bitVal = digitalRead(dataPin);
/* Set the corresponding bit in bytesVal.
*/
bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));
/* Pulse the Clock (rising edge shifts the next bit).
*/
digitalWrite(clockPin, HIGH);
delayMicroseconds(PULSE_WIDTH_USEC);
digitalWrite(clockPin, LOW);
}
return(bytesVal);
}
/* Dump the list of zones along with their current status.
*/
void display_pin_values()
{
for(int i = 0; i < DATA_WIDTH; i++)
{
if((pinValues >> i) & 1)
b[i]=1;
else
b[i]=0;
}
myGLCD.setColor(VGA_WHITE);
myGLCD.setBackColor(VGA_BLACK);
myGLCD.printNumI(b[0], 0, 20);
myGLCD.printNumI(b[1], 36, 20);
myGLCD.printNumI(b[2], 72, 20);
myGLCD.printNumI(b[3], 108, 20);
myGLCD.printNumI(b[4], 144, 20);
myGLCD.printNumI(b[5], 180, 20);
myGLCD.printNumI(b[6], 216, 20);
myGLCD.printNumI(b[7], 252, 20);
myGLCD.printNumI(b[8], 0, 120);
myGLCD.printNumI(b[9], 36, 120);
myGLCD.printNumI(b[10], 72, 120);
myGLCD.printNumI(b[11], 108, 120);
myGLCD.printNumI(b[12], 144, 120);
myGLCD.printNumI(b[13], 180, 120);
myGLCD.printNumI(b[14], 216, 120);
myGLCD.printNumI(b[15], 252, 120);
myGLCD.printNumI(b[16], 0, 220);
myGLCD.printNumI(b[17], 36, 220);
myGLCD.printNumI(b[18], 72, 220);
myGLCD.printNumI(b[19], 108, 220);
myGLCD.printNumI(b[20], 144, 220);
myGLCD.printNumI(b[21], 180, 220);
myGLCD.printNumI(b[22], 216, 220);
myGLCD.printNumI(b[23], 252, 220);
}
void buttonNumberDisplay()
{//prints a number for each button, below this number 0 or 1 will be printed by display_pin_values()
myGLCD.setColor(VGA_WHITE);
myGLCD.setBackColor(VGA_BLUE);
myGLCD.printNumI(1, 0, 0);
myGLCD.printNumI(2, 36, 0);
myGLCD.printNumI(3, 72, 0);
myGLCD.printNumI(4, 108, 0);
myGLCD.printNumI(5, 144, 0);
myGLCD.printNumI(6, 180, 0);
myGLCD.printNumI(7, 216, 0);
myGLCD.printNumI(8, 252, 0);
myGLCD.printNumI(9, 0, 100);
myGLCD.printNumI(10, 36, 100);
myGLCD.printNumI(11, 72, 100);
myGLCD.printNumI(12, 108, 100);
myGLCD.printNumI(13, 144, 100);
myGLCD.printNumI(14, 180, 100);
myGLCD.printNumI(15, 216, 100);
myGLCD.printNumI(16, 252, 100);
myGLCD.printNumI(17, 0, 200);
myGLCD.printNumI(18, 36, 200);
myGLCD.printNumI(19, 72, 200);
myGLCD.printNumI(20, 108, 200);
myGLCD.printNumI(21, 144, 200);
myGLCD.printNumI(22, 180, 200);
myGLCD.printNumI(23, 216, 200);
myGLCD.printNumI(24, 252, 200);
}//end firstDisplay
void setup()
{
// Serial.begin(9600);
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(BigFont);
myGLCD.setBackColor(VGA_BLACK);
/* Initialize our digital pins...
*/
pinMode(ploadPin, OUTPUT);
pinMode(clockEnablePin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
digitalWrite(clockPin, LOW);
digitalWrite(ploadPin, HIGH);
buttonNumberDisplay();
/* Read in and display the pin states at startup.
*/
pinValues = read_shift_regs();
display_pin_values();
oldPinValues = pinValues;
}
void loop()
{
/* Read the state of all zones.
*/
pinValues = read_shift_regs();
/* If there was a chage in state, display which ones changed.
*/
if(pinValues != oldPinValues)
{
display_pin_values();
oldPinValues = pinValues;
}
delay(POLL_DELAY_MSEC);
}