I'm a newbie here and I'm working on a keypad project. I'm using a shift register(74hc595) scanning columns and a other one reading rows(74hc165) through SPI comm.
I got a question: why I have to use a for loop to analyze every bit in a serial data. Why I cannot read it directly or I got nothing?
for (Col = 0; Col < EIBIT; Col++){
if(bitRead(input,Col)==HIGH && Row > 0){
bitWrite(RowOut, Row-1, 1);
bitWrite(ColOut, Col+4, 1);
ledOutput = RowOut | ColOut;
Serial.print(Row);
Serial.println(Col);
}else{
ledOutput = RowOut | ColOut;
}
} //end of Col
Why this is working and
if(digitalRead(interruptPin)==HIGH && Row > 0){
// or: if(input > 0 && Row > 0){
bitWrite(RowOut, Row+3, 1);
Serial.println(input);
}
ledOutput = input | RowOut;
and this is not? When I run this code even the interruption won't triggered.
Here is my full code:
/*
* ))()ButtBox Proto.Ver t6)()(SPI)(
*
* comes with USB and Midi ports.
* 74HC595 as the row Scanner
* 74hc165 sa the col Receiver
* 16 bits write and 8 bits read
* ****This Version is a*******
* ****test cod for bread board***
*/
#include <SPI.h>
# define ROW_SIZE 4
# define COL_SIZE 4
# define EIBIT 8
const int latchPin = 10; // 595:ST_CP
const int PLPin = 4;// 165 latch
const int interruptPin = 2;
byte input = B11111111;
byte check = B00000001;
byte RowOut, ColOut;
byte ledOutput= B01010010;
int Row, Col, counter;
void setup() {
// Setup Mode for data pins
pinMode(13,OUTPUT);// SCK pin
pinMode(interruptPin,INPUT);// button trigger
pinMode(latchPin,OUTPUT);// latch
pinMode(PLPin,OUTPUT);
// Initializing chips'SPI communication.
SPI.beginTransaction(SPISettings(800000,// Reduced for 165
MSBFIRST,SPI_MODE0));
SPI.usingInterrupt(digitalPinToInterrupt(interruptPin));
SPI.begin();
// *** NEW: 16 bits in total
SPI.transfer(B11111111);//0xFF = 11111111 in binary
SPI.transfer(ledOutput);
// Latch the 595 foward one bit
digitalWrite(latchPin,HIGH);
digitalWrite(latchPin,LOW);
Serial.begin(9600);
// ***** TEST setup ******
attachInterrupt(digitalPinToInterrupt(interruptPin),
pin_read, RISING);
Serial.println("Serial OK & Setup done!");
}
void loop(){
}
void pin_read(){
// pause for make sure
for(int j = 0; j < 25; j++){
delayMicroseconds(1000);
}
ColOut = 0;
RowOut = 0;
input = 0;
check = B00000001;
for(Row = 0; Row < EIBIT; Row++){
/* Stage 1
* 595: send a bit to keypad
* 165: setup and latch the data
*/
// ***NEW: 16 bits in total
digitalWrite(PLPin,LOW); //165
SPI.transfer(check);
digitalWrite(PLPin,HIGH); //165
input = SPI.transfer(ledOutput);
digitalWrite(latchPin,HIGH); //595:ST_CP
digitalWrite(latchPin,LOW); //595:ST_CP
/* Stage 2
* 165 comm: read
* deal with the flickr
* when Multi triggered
*/
for (Col = 0; Col < EIBIT; Col++){
if(bitRead(input,Col)==HIGH && Row > 0){
bitWrite(RowOut, Row-1, 1);
bitWrite(ColOut, Col+4, 1);
ledOutput = RowOut | ColOut;
Serial.print(Row);
Serial.println(Col);
}else{
ledOutput = RowOut | ColOut;
}
} //end of Col
/*
* Wrong Code:
*
if(digitalRead(interruptPin)==HIGH && Row > 0){
bitWrite(RowOut, Row+3, 1);
Serial.println(input);
}
ledOutput = input | RowOut;
*
* but why?
*/
check = check<< 1;
} //`end of Row
/* ***NEW: 16 bits in total
* Lid the led
*/
SPI.transfer(255);
SPI.transfer(ledOutput);
// Latch the 595 foward one bit
digitalWrite(latchPin,HIGH);
digitalWrite(latchPin,LOW);
} //`pin_read