int latchPin = 8; // Latch pin (STCP)
int clockPin = 12; // Clock pin (SHCP)
int dataPin = 11; // Data pin (DS)
const int sevSegPins[] = {23,25,27,29,31,33,35};
/*VCC => 5V
OE => GND
MR => 5V
GND => GND*/
byte leds = 0; // 亮燈的LED數量
byte numbers [10][7]{
{1,1,1,1,1,1,0}, //0
{0,1,1,0,0,0,0}, //1
{1,1,0,1,1,0,1}, //2
{1,1,1,1,0,0,1}, //3
{0,1,1,0,0,1,0}, //4
{1,0,1,1,0,1,1}, //5
{1,0,1,1,1,1,1}, //6
{1,1,1,0,0,1,0}, //7
{1,1,1,1,1,1,1}, //8
{1,1,1,0,0,1,1} //9
};
void setup()
{
// Set all the pins of 74HC595 as OUTPUT
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(sevSegPins[7], OUTPUT);
for (int i=0;i<7,i++){
pinMode(sevSegPins[i], OUTPUT);
}
}
void displayNumber(int num){
for(int i=0;i<7;i++){
digitalWrite(sevSegPins[i],numbers[num][i]);
delay(100);
}
}
void loop()
{
leds = 0; // 一開始燈全滅,所以設為0
updateShiftRegister(); //送資料
delay(100);
for (int i = 0; i < 4; i++) //依序亮燈
{
bitSet(leds, i); // 經由多少燈要亮的i,換算成10進位,並把結果交給leds
updateShiftRegister(); //進行送資料的副程式
delay(25);
}
displayNumber();
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}