Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Data_Clock_Pair Seeed_Chainable_LED Seeed_Four_Digit_Disp Seeed_IR_Temp_Sensor Seeed_Led_Bar
Fork of Seeed_Grove_4_Digit_Display_Clock by
SeeedFourDigitDisp.h@12:a16d86fac131, 2017-04-27 (annotated)
- Committer:
- tulanthoar
- Date:
- Thu Apr 27 16:08:02 2017 -0600
- Revision:
- 12:a16d86fac131
- Parent:
- 7:b16b9733d859
- Child:
- 15:abda719ba6e6
use a pin pair struct to avoid confusing data and clock pins
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tulanthoar | 7:b16b9733d859 | 1 | #ifndef SEEED_FOUR_DIGIT_DISP_H |
| tulanthoar | 7:b16b9733d859 | 2 | #define SEEED_FOUR_DIGIT_DISP_H |
| tulanthoar | 7:b16b9733d859 | 3 | #include "mbed.h" |
| tulanthoar | 12:a16d86fac131 | 4 | #include "DataClockPair.h" |
| tulanthoar | 7:b16b9733d859 | 5 | |
| tulanthoar | 7:b16b9733d859 | 6 | class SeeedFourDigitDisp { |
| tulanthoar | 7:b16b9733d859 | 7 | private: |
| tulanthoar | 7:b16b9733d859 | 8 | int digitTable_[17]; |
| tulanthoar | 7:b16b9733d859 | 9 | static const int onByte_ = 0x88; |
| tulanthoar | 7:b16b9733d859 | 10 | static const int fixedAddrByte_ = 0x44; |
| tulanthoar | 7:b16b9733d859 | 11 | static const int positionBit_ = 0xc0; |
| tulanthoar | 7:b16b9733d859 | 12 | static const int nullDigit_ = 16; |
| tulanthoar | 7:b16b9733d859 | 13 | static const int colonBit_ = 0x80; |
| tulanthoar | 7:b16b9733d859 | 14 | void start_cmd(); |
| tulanthoar | 7:b16b9733d859 | 15 | void stop_cmd(); |
| tulanthoar | 7:b16b9733d859 | 16 | DigitalOut datPin_; |
| tulanthoar | 7:b16b9733d859 | 17 | DigitalOut clkPin_; |
| tulanthoar | 7:b16b9733d859 | 18 | void pin_delay(int delay_us = 1); |
| tulanthoar | 7:b16b9733d859 | 19 | void send_byte(int byte); |
| tulanthoar | 7:b16b9733d859 | 20 | public: |
| tulanthoar | 7:b16b9733d859 | 21 | int brightness; |
| tulanthoar | 7:b16b9733d859 | 22 | bool colonFlag; |
| tulanthoar | 7:b16b9733d859 | 23 | void set_digit(int pos, int digit); |
| tulanthoar | 7:b16b9733d859 | 24 | void set_integer(int value); |
| tulanthoar | 7:b16b9733d859 | 25 | void clear_display(); |
| tulanthoar | 7:b16b9733d859 | 26 | void turn_on(); |
| tulanthoar | 12:a16d86fac131 | 27 | SeeedFourDigitDisp (DataClockPair pins); |
| tulanthoar | 7:b16b9733d859 | 28 | }; |
| tulanthoar | 7:b16b9733d859 | 29 | |
| tulanthoar | 12:a16d86fac131 | 30 | SeeedFourDigitDisp::SeeedFourDigitDisp(DataClockPair pins) : datPin_(pins.dataPin, 1), clkPin_(pins.clockPin, 1) { |
| tulanthoar | 7:b16b9733d859 | 31 | brightness = 7; |
| tulanthoar | 7:b16b9733d859 | 32 | colonFlag = false; |
| tulanthoar | 7:b16b9733d859 | 33 | const int digits[] = {0x3f, 0x06, 0x5b, 0x4f, |
| tulanthoar | 7:b16b9733d859 | 34 | 0x66, 0x6d, 0x7d, 0x07, |
| tulanthoar | 7:b16b9733d859 | 35 | 0x7f, 0x6f, 0x77, 0x7c, |
| tulanthoar | 7:b16b9733d859 | 36 | 0x39, 0x5e, 0x79, 0x71, |
| tulanthoar | 7:b16b9733d859 | 37 | 0x00 |
| tulanthoar | 7:b16b9733d859 | 38 | }; //0~9,A,b,C,d,E,F,null |
| tulanthoar | 12:a16d86fac131 | 39 | for (int i = 0; i < 17; ++i) { |
| tulanthoar | 7:b16b9733d859 | 40 | digitTable_[i] = digits[i]; |
| tulanthoar | 7:b16b9733d859 | 41 | } |
| tulanthoar | 7:b16b9733d859 | 42 | clear_display(); |
| tulanthoar | 7:b16b9733d859 | 43 | } |
| tulanthoar | 7:b16b9733d859 | 44 | |
| tulanthoar | 12:a16d86fac131 | 45 | void SeeedFourDigitDisp::pin_delay(int delay_us) { |
| tulanthoar | 12:a16d86fac131 | 46 | wait_us(delay_us); |
| tulanthoar | 12:a16d86fac131 | 47 | } |
| tulanthoar | 12:a16d86fac131 | 48 | |
| tulanthoar | 7:b16b9733d859 | 49 | void SeeedFourDigitDisp::start_cmd() { |
| tulanthoar | 12:a16d86fac131 | 50 | datPin_ = !datPin_; |
| tulanthoar | 7:b16b9733d859 | 51 | } |
| tulanthoar | 7:b16b9733d859 | 52 | |
| tulanthoar | 7:b16b9733d859 | 53 | void SeeedFourDigitDisp::stop_cmd() { |
| tulanthoar | 7:b16b9733d859 | 54 | datPin_ = 0; |
| tulanthoar | 12:a16d86fac131 | 55 | clkPin_ = !clkPin_; |
| tulanthoar | 7:b16b9733d859 | 56 | pin_delay(); |
| tulanthoar | 12:a16d86fac131 | 57 | clkPin_ = !clkPin_; |
| tulanthoar | 12:a16d86fac131 | 58 | datPin_ = !datPin_; |
| tulanthoar | 7:b16b9733d859 | 59 | } |
| tulanthoar | 7:b16b9733d859 | 60 | |
| tulanthoar | 12:a16d86fac131 | 61 | void SeeedFourDigitDisp::send_byte(int byte) { |
| tulanthoar | 12:a16d86fac131 | 62 | byte |= 0x100; // bring data high for ack after 8 bits |
| tulanthoar | 12:a16d86fac131 | 63 | for (int i = 0; i < 9; ++i) { |
| tulanthoar | 12:a16d86fac131 | 64 | pin_delay(); |
| tulanthoar | 12:a16d86fac131 | 65 | clkPin_ = !clkPin_; |
| tulanthoar | 12:a16d86fac131 | 66 | pin_delay(); |
| tulanthoar | 12:a16d86fac131 | 67 | datPin_ = byte & 1; |
| tulanthoar | 12:a16d86fac131 | 68 | byte >>= 1; |
| tulanthoar | 12:a16d86fac131 | 69 | pin_delay(); |
| tulanthoar | 12:a16d86fac131 | 70 | clkPin_ = !clkPin_; |
| tulanthoar | 12:a16d86fac131 | 71 | } |
| tulanthoar | 7:b16b9733d859 | 72 | } |
| tulanthoar | 7:b16b9733d859 | 73 | |
| tulanthoar | 7:b16b9733d859 | 74 | void SeeedFourDigitDisp::set_digit(int pos, int digit) { |
| tulanthoar | 7:b16b9733d859 | 75 | int flaggedDigit = digitTable_[digit] | (colonFlag ? colonBit_ : 0); |
| tulanthoar | 7:b16b9733d859 | 76 | start_cmd(); //start signal sent to TM1637 from MCU |
| tulanthoar | 7:b16b9733d859 | 77 | send_byte(fixedAddrByte_); |
| tulanthoar | 7:b16b9733d859 | 78 | stop_cmd(); |
| tulanthoar | 7:b16b9733d859 | 79 | start_cmd(); |
| tulanthoar | 7:b16b9733d859 | 80 | send_byte(pos|positionBit_); |
| tulanthoar | 7:b16b9733d859 | 81 | send_byte(flaggedDigit); |
| tulanthoar | 7:b16b9733d859 | 82 | stop_cmd(); |
| tulanthoar | 7:b16b9733d859 | 83 | start_cmd(); |
| tulanthoar | 7:b16b9733d859 | 84 | send_byte(onByte_ + brightness); |
| tulanthoar | 7:b16b9733d859 | 85 | stop_cmd(); |
| tulanthoar | 12:a16d86fac131 | 86 | } |
| tulanthoar | 7:b16b9733d859 | 87 | |
| tulanthoar | 12:a16d86fac131 | 88 | void SeeedFourDigitDisp::turn_on() { |
| tulanthoar | 12:a16d86fac131 | 89 | start_cmd(); |
| tulanthoar | 12:a16d86fac131 | 90 | send_byte(onByte_+brightness); |
| tulanthoar | 12:a16d86fac131 | 91 | stop_cmd(); |
| tulanthoar | 7:b16b9733d859 | 92 | } |
| tulanthoar | 7:b16b9733d859 | 93 | |
| tulanthoar | 12:a16d86fac131 | 94 | void SeeedFourDigitDisp::clear_display() { |
| tulanthoar | 12:a16d86fac131 | 95 | set_digit(0,nullDigit_); |
| tulanthoar | 12:a16d86fac131 | 96 | set_digit(1,nullDigit_); |
| tulanthoar | 12:a16d86fac131 | 97 | set_digit(2,nullDigit_); |
| tulanthoar | 12:a16d86fac131 | 98 | set_digit(3,nullDigit_); |
| tulanthoar | 12:a16d86fac131 | 99 | } |
| tulanthoar | 12:a16d86fac131 | 100 | |
| tulanthoar | 12:a16d86fac131 | 101 | |
| tulanthoar | 7:b16b9733d859 | 102 | void SeeedFourDigitDisp::set_integer(int value) { |
| tulanthoar | 7:b16b9733d859 | 103 | clear_display(); |
| tulanthoar | 7:b16b9733d859 | 104 | if( value < 0 ) { |
| tulanthoar | 7:b16b9733d859 | 105 | colonFlag = true; |
| tulanthoar | 7:b16b9733d859 | 106 | set_digit(0, 0); |
| tulanthoar | 7:b16b9733d859 | 107 | return; |
| tulanthoar | 7:b16b9733d859 | 108 | } |
| tulanthoar | 7:b16b9733d859 | 109 | if( value > 9999 ) { |
| tulanthoar | 7:b16b9733d859 | 110 | colonFlag = true; |
| tulanthoar | 7:b16b9733d859 | 111 | set_digit(0, 15); |
| tulanthoar | 7:b16b9733d859 | 112 | return; |
| tulanthoar | 7:b16b9733d859 | 113 | } |
| tulanthoar | 7:b16b9733d859 | 114 | for (int i = 3; i >= 0; --i) { |
| tulanthoar | 7:b16b9733d859 | 115 | int digit = value % 10; |
| tulanthoar | 7:b16b9733d859 | 116 | set_digit(i, digit); |
| tulanthoar | 7:b16b9733d859 | 117 | value -= digit; |
| tulanthoar | 7:b16b9733d859 | 118 | if(value < 10) return; |
| tulanthoar | 7:b16b9733d859 | 119 | value /= 10; |
| tulanthoar | 7:b16b9733d859 | 120 | } |
| tulanthoar | 7:b16b9733d859 | 121 | } |
| tulanthoar | 7:b16b9733d859 | 122 | |
| tulanthoar | 7:b16b9733d859 | 123 | |
| tulanthoar | 7:b16b9733d859 | 124 | #endif |
