Created
May 25, 2015 10:18
-
-
Save t-kashima/691811983b6fe0d41c67 to your computer and use it in GitHub Desktop.
LED display timer for Arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 参考: http://garretlab.web.fc2.com/arduino/introduction/beginning_with_7segment_led/ | |
int num[10][8] = { | |
{0, 0, 1, 0, 1, 0, 0, 0}, // 0 | |
{1, 1, 1, 0, 1, 0, 1, 1}, // 1 | |
{0, 0, 1, 1, 0, 0, 1, 0}, // 2 | |
{1, 0, 1, 0, 0, 0, 1, 0}, // 3 | |
{1, 1, 1, 0, 0, 0, 0, 1}, // 4 | |
{1, 0, 1, 0, 0, 1, 0, 0}, // 5 | |
{0, 0, 1, 0, 0, 1, 0, 0}, // 6 | |
{1, 1, 1, 0, 1, 0, 0, 0}, // 7 | |
{0, 0, 1, 0, 0, 0, 0, 0}, // 8 | |
{1, 0, 1, 0, 0, 0, 0, 0} // 9 | |
}; | |
int count = 0; | |
void setup() { | |
// put your setup code here, to run once: | |
for (int i = 2; i <= 13; i++) { | |
pinMode(i, OUTPUT); | |
} | |
OCR2A = 255; | |
TCCR2B = 0b100; | |
bitWrite(TIMSK2, OCIE2A, 1); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
delay(1000); | |
count += 1; | |
if (10000 <= count) { | |
count = 0; | |
} | |
} | |
// 数字を全てクリアする | |
void clearNumbers() { | |
for (int i = 0; i < 8; i++) { | |
int pinNo = i + 6; | |
digitalWrite(pinNo, HIGH); | |
} | |
} | |
// 複数桁の数字を表示する | |
void showNumbers(int count) { | |
int n = count; | |
for (int i = 0; i < 4; i++) { | |
digitalWrite(i + 2, LOW); | |
showNumber(n % 10); | |
delayMicroseconds(100); | |
clearNumbers(); | |
digitalWrite(i + 2, HIGH); | |
n = n / 10; | |
} | |
} | |
// 1桁の数字を表示する | |
void showNumber(int n) { | |
// LED show | |
for (int i = 0; i < 8; i++) { | |
int pinNo = i + 6; | |
digitalWrite(pinNo, num[n][i]); | |
} | |
} | |
// 割り込み | |
ISR(TIMER2_COMPA_vect) { | |
showNumbers(count); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment