Four digit 7 segment LED driver
FourDigitLED.cpp@4:5ed17505c88e, 2015-11-22 (annotated)
- Committer:
- takashikojo
- Date:
- Sun Nov 22 08:46:13 2015 +0000
- Revision:
- 4:5ed17505c88e
- Parent:
- 3:73f31aea935a
Cleaned comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takashikojo | 0:7a8925d2a8e7 | 1 | #include <mbed.h> |
takashikojo | 3:73f31aea935a | 2 | #include <rtos.h> |
takashikojo | 1:c94aa39af4c7 | 3 | #include "FourDigitLED.h" |
takashikojo | 0:7a8925d2a8e7 | 4 | |
takashikojo | 0:7a8925d2a8e7 | 5 | static unsigned char SegPattern[] = { |
takashikojo | 3:73f31aea935a | 6 | 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x27, 0x7f, 0x6f, |
takashikojo | 3:73f31aea935a | 7 | 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71 |
takashikojo | 0:7a8925d2a8e7 | 8 | }; |
takashikojo | 0:7a8925d2a8e7 | 9 | |
takashikojo | 3:73f31aea935a | 10 | FourDigitLED::FourDigitLED(PinName seg0, PinName seg1, PinName seg2, PinName seg3, |
takashikojo | 3:73f31aea935a | 11 | PinName seg4, PinName seg5, PinName seg6, PinName dot, |
takashikojo | 3:73f31aea935a | 12 | PinName digit0, PinName digit1, PinName digit2, PinName digit3 |
takashikojo | 3:73f31aea935a | 13 | ) |
takashikojo | 0:7a8925d2a8e7 | 14 | { |
takashikojo | 0:7a8925d2a8e7 | 15 | LEDs = new BusOut( seg0, seg1, seg2, seg3, seg4, seg5, seg6, dot, digit0, digit1, digit2, digit3) ; |
takashikojo | 0:7a8925d2a8e7 | 16 | scan_digit = 0 ; |
takashikojo | 0:7a8925d2a8e7 | 17 | /* test LEDs */ |
takashikojo | 3:73f31aea935a | 18 | |
takashikojo | 0:7a8925d2a8e7 | 19 | LEDs->write(0xfe00 | MASK_DOT | 0x0) ; |
takashikojo | 0:7a8925d2a8e7 | 20 | wait(1.0) ; |
takashikojo | 3:73f31aea935a | 21 | setNum(0, 8); setNum(1, 8); setNum(2, 8); setNum(3, 8); |
takashikojo | 3:73f31aea935a | 22 | setDot(0, 1); setDot(1, 1); setDot(2, 1); setDot(3, 1); |
takashikojo | 0:7a8925d2a8e7 | 23 | /* end test */ |
takashikojo | 4:5ed17505c88e | 24 | |
takashikojo | 0:7a8925d2a8e7 | 25 | wait(1.0) ; |
takashikojo | 0:7a8925d2a8e7 | 26 | val[0] = 0 ; val[1] = 0 ; val[2] = 0 ; val[3] = 0 ; |
takashikojo | 0:7a8925d2a8e7 | 27 | } |
takashikojo | 0:7a8925d2a8e7 | 28 | |
takashikojo | 0:7a8925d2a8e7 | 29 | FourDigitLED::~FourDigitLED() |
takashikojo | 0:7a8925d2a8e7 | 30 | { |
takashikojo | 0:7a8925d2a8e7 | 31 | } |
takashikojo | 0:7a8925d2a8e7 | 32 | |
takashikojo | 3:73f31aea935a | 33 | |
takashikojo | 3:73f31aea935a | 34 | void FourDigitLED::scanDigit(void) |
takashikojo | 0:7a8925d2a8e7 | 35 | { |
takashikojo | 0:7a8925d2a8e7 | 36 | int d ; |
takashikojo | 0:7a8925d2a8e7 | 37 | d= ++scan_digit ; |
takashikojo | 0:7a8925d2a8e7 | 38 | d= d % DIGITS ; |
takashikojo | 0:7a8925d2a8e7 | 39 | LEDs->write(~(0x100 << d) & 0xff00 | (val[d] & 0xff) ) ; |
takashikojo | 0:7a8925d2a8e7 | 40 | } |
takashikojo | 0:7a8925d2a8e7 | 41 | |
takashikojo | 2:c4e8b2fd8f7e | 42 | unsigned int FourDigitLED::getPtn(int d) |
takashikojo | 1:c94aa39af4c7 | 43 | { |
takashikojo | 2:c4e8b2fd8f7e | 44 | if((d>=0) && (d<=sizeof(SegPattern))) |
takashikojo | 2:c4e8b2fd8f7e | 45 | return SegPattern[d] ; |
takashikojo | 2:c4e8b2fd8f7e | 46 | else |
takashikojo | 2:c4e8b2fd8f7e | 47 | return 0 ; |
takashikojo | 2:c4e8b2fd8f7e | 48 | } |
takashikojo | 2:c4e8b2fd8f7e | 49 | |
takashikojo | 2:c4e8b2fd8f7e | 50 | unsigned int FourDigitLED::setPtn(int d, unsigned int ptn, unsigned int mask) |
takashikojo | 2:c4e8b2fd8f7e | 51 | { |
takashikojo | 2:c4e8b2fd8f7e | 52 | val[d] = (val[d] & 0xff80) | (ptn & mask); |
takashikojo | 1:c94aa39af4c7 | 53 | return ptn ; |
takashikojo | 1:c94aa39af4c7 | 54 | } |
takashikojo | 1:c94aa39af4c7 | 55 | |
takashikojo | 0:7a8925d2a8e7 | 56 | int FourDigitLED::setNum(int d, int n) |
takashikojo | 0:7a8925d2a8e7 | 57 | { |
takashikojo | 1:c94aa39af4c7 | 58 | if((n <0) || (n >= 16)) |
takashikojo | 1:c94aa39af4c7 | 59 | val[d] = val[d] & 0xff80 ; |
takashikojo | 1:c94aa39af4c7 | 60 | else |
takashikojo | 1:c94aa39af4c7 | 61 | val[d] = (val[d] & 0xff80) | SegPattern[n] ; |
takashikojo | 0:7a8925d2a8e7 | 62 | return n ; |
takashikojo | 0:7a8925d2a8e7 | 63 | } |
takashikojo | 0:7a8925d2a8e7 | 64 | |
takashikojo | 0:7a8925d2a8e7 | 65 | int FourDigitLED::setDot(int d, bool n) |
takashikojo | 0:7a8925d2a8e7 | 66 | { |
takashikojo | 0:7a8925d2a8e7 | 67 | val[d] = (val[d] & 0xff7f) | ((n == false) ? 0x0 : 0x80) ; |
takashikojo | 0:7a8925d2a8e7 | 68 | return n ; |
takashikojo | 0:7a8925d2a8e7 | 69 | } |
takashikojo | 0:7a8925d2a8e7 | 70 | |
takashikojo | 3:73f31aea935a | 71 | void FourDigitLED::dump(void) |
takashikojo | 3:73f31aea935a | 72 | { |
takashikojo | 3:73f31aea935a | 73 | printf("val = %02x, %02x, %02x, %02x\n", val[3], val[2], val[1], val[0]) ; |
takashikojo | 0:7a8925d2a8e7 | 74 | } |