A clock with Grove - 4 Digit Display
Dependencies: DigitDisplay mbed
main.cpp@0:99516934afee, 2014-04-28 (annotated)
- Committer:
- yihui
- Date:
- Mon Apr 28 03:05:15 2014 +0000
- Revision:
- 0:99516934afee
initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:99516934afee | 1 | #include "mbed.h" |
yihui | 0:99516934afee | 2 | #include "DigitDisplay.h" |
yihui | 0:99516934afee | 3 | |
yihui | 0:99516934afee | 4 | DigitDisplay display(P1_14, P1_13); // 4-Digit Display connected to UART Grove connector |
yihui | 0:99516934afee | 5 | DigitalOut led(LED1); |
yihui | 0:99516934afee | 6 | DigitalOut led2(LED2); |
yihui | 0:99516934afee | 7 | Ticker tick; |
yihui | 0:99516934afee | 8 | uint8_t hour = 0; |
yihui | 0:99516934afee | 9 | uint8_t minute = 0; |
yihui | 0:99516934afee | 10 | uint8_t second = 0; |
yihui | 0:99516934afee | 11 | uint8_t colon = 0; |
yihui | 0:99516934afee | 12 | |
yihui | 0:99516934afee | 13 | uint8_t display_buffer[4] = {0, 0, 0, 0}; |
yihui | 0:99516934afee | 14 | |
yihui | 0:99516934afee | 15 | void handler(void) |
yihui | 0:99516934afee | 16 | { |
yihui | 0:99516934afee | 17 | display.setColon(colon); |
yihui | 0:99516934afee | 18 | led2 = !led2; |
yihui | 0:99516934afee | 19 | if (colon) { |
yihui | 0:99516934afee | 20 | colon = 0; |
yihui | 0:99516934afee | 21 | |
yihui | 0:99516934afee | 22 | second++; |
yihui | 0:99516934afee | 23 | if (second >= 60) { |
yihui | 0:99516934afee | 24 | second = 0; |
yihui | 0:99516934afee | 25 | |
yihui | 0:99516934afee | 26 | minute++; |
yihui | 0:99516934afee | 27 | if (minute >= 60) { |
yihui | 0:99516934afee | 28 | minute = 0; |
yihui | 0:99516934afee | 29 | |
yihui | 0:99516934afee | 30 | hour++; |
yihui | 0:99516934afee | 31 | if (hour >= 24) { |
yihui | 0:99516934afee | 32 | hour = 0; |
yihui | 0:99516934afee | 33 | } |
yihui | 0:99516934afee | 34 | } |
yihui | 0:99516934afee | 35 | |
yihui | 0:99516934afee | 36 | |
yihui | 0:99516934afee | 37 | display_buffer[0] = hour / 10; |
yihui | 0:99516934afee | 38 | display_buffer[1] = hour % 10; |
yihui | 0:99516934afee | 39 | |
yihui | 0:99516934afee | 40 | display_buffer[2] = minute / 10; |
yihui | 0:99516934afee | 41 | display_buffer[3] = minute % 10; |
yihui | 0:99516934afee | 42 | |
yihui | 0:99516934afee | 43 | display.write(display_buffer); |
yihui | 0:99516934afee | 44 | } |
yihui | 0:99516934afee | 45 | } else { |
yihui | 0:99516934afee | 46 | colon = 1; |
yihui | 0:99516934afee | 47 | } |
yihui | 0:99516934afee | 48 | } |
yihui | 0:99516934afee | 49 | |
yihui | 0:99516934afee | 50 | int main() { |
yihui | 0:99516934afee | 51 | display.write(display_buffer); |
yihui | 0:99516934afee | 52 | tick.attach(handler, 0.5); |
yihui | 0:99516934afee | 53 | while(1) { |
yihui | 0:99516934afee | 54 | led = !led; |
yihui | 0:99516934afee | 55 | wait(0.5); |
yihui | 0:99516934afee | 56 | } |
yihui | 0:99516934afee | 57 | } |