TPIC6B595 ควบคุม LED
Shift Register Example
for TPIC6B595 shift register by Jens C Brynildsen
This sketch turns reads serial input and uses it to set the pins
of a TPIC6B595 shift register.
Hardware:
* TPIC6B595 shift register attached to pins 7, 8, 11 and 12 of the Arduino,
as detailed below.
* LEDs attached to each of the outputs of the shift register
Based on the example created 23 Mar 2010 by Tom Igoe
*/
//Pin to clear the register
const int clearPin = 13;
//Pin connected to latch pin (RCK) of TPIC6B595
const int latchPin = 10;
//Pin connected to clock pin (SRCK) of TPIC6B595
const int clockPin = 12;
////Pin connected to Data in (SER IN) of TPIC6B595
const int dataPin = 11;
//Pin input switch
int in1 = 2;
int in2 = 3;
int in3 = 4;
int in4 = 5;
int in5 = 6;
int in6 = 7;
int in7 = 8;
int in8 = 9;
int k =0;
int counter = 0;
int numLedsInUse = 8;
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(clearPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(9, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("*");
// Always start by setting SRCLR high
digitalWrite( clearPin, LOW);
// delay a little and then set
delay(100);
}
void loop() {
// // Display LED's running
// if( counter >= (numLedsInUse-1) ){
// counter = 0;
// } else {
// counter++;
// }
//
// write to the shift register with the correct bit set high:
for(int i=2;i<=9;i++){
if(digitalRead(i) == LOW && k == 0){
registerWrite(i-2, HIGH);
k = 1;
}
if(digitalRead(i) == HIGH && k == 1){
registerWrite(i-2, LOW);
k = 0;
}
}
}
// This method sends bits to the shift register:
void registerWrite(int whichPin, int whichState) {
Serial.println(whichPin);
// the bits you want to send
byte bitsToSend = 0;
// write number as bits
bitWrite(bitsToSend, whichPin, whichState);
// turn off the output so the pins don't light up
// while you're shifting bits:
digitalWrite(latchPin, LOW);
Serial.println(bitsToSend);
Serial.println("_");
// shift the bits out
shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
// turn on the output so the LEDs can light up:
digitalWrite(latchPin, HIGH);
}
