|
發表於 2013-12-26 12:53:33
|
顯示全部樓層
本帖最後由 a48602 於 2013-12-26 12:55 PM 編輯
Arduino 有一個 shiftOut(...) 跟 74HC595 的用法。 0000~9999 計數器。
int clockPin = 5; // 74HC595 SH_CP (pin 11, clock pin)
int latchPin = 6; // 74HC595 ST_CP (pin 12,latch pin)
int dataPin = 7; // 74HC595 DS (pin 14)
int zeroPin = 4; // MC14017B RESET PIN
int numStr[] = {
63, 6, 91, 79, 102, 109, 125, 39, 127, 111, 0 // 0,1,2,3,4,5,6,7,8,9
};
void setup() {
Serial.begin(9600);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(zeroPin, OUTPUT);
}
int iNum = 0, iNumA = 0, iNumB = 0, iNumC = 0;
int iStart = 0;
void display_num()
{
int ii;
switch( iStart ) {
case 0: ii = iNum;
digitalWrite( zeroPin, HIGH );
break;
case 1: ii = iNumA;
digitalWrite( zeroPin, LOW );
break;
case 2: ii = iNumB; break;
case 3: ii = iNumC; break;
}
if ( iStart < 3 ) iStart++;
else iStart = 0;
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, numStr[ii]);
digitalWrite(latchPin, HIGH);
delay(3);
}
unsigned long previousTimeMark;
void loop() {
display_num();
unsigned long now = millis();
if(now - previousTimeMark > 1000) {
previousTimeMark = now;
iNum++;
if ( iNum > 9 ) {
iNum = 0;
iNumA++;
if ( iNumA > 9 ) {
iNumA = 0;
iNumB++;
if ( iNumB > 9 ) {
iNumB = 0;
iNumC++;
if ( iNumC > 9 ) {
iNumC = 0;
}
}
}
}
}
} |
|