-
Notifications
You must be signed in to change notification settings - Fork 36
TM16xx base class reference
Use | Function |
---|---|
Constructor | TM16..(byte dataPin, byte clockPin, byte strobePin, byte nDigitsUsed) |
Initialisation | begin(bool activateDisplay=true, byte intensity=7) |
Change brightness | setupDisplay(bool active, byte intensity) |
Flip display | setDisplayFlipped(bool flipped) |
Clear display | clearDisplay() |
Set 8 segments | setSegments(byte segments, byte position) |
Set 16 segments | setSegments16(uint16_t segments, byte position) |
Set single digit | setDisplayDigit(byte digit, byte pos=0, bool dot=false, const byte numberFont[] = TM16XX_NUMBER_FONT) |
Show a number | setDisplayToDecNumber(int nNumber, byte bDots=0) |
Clear single digit | clearDisplayDigit(byte pos, bool dot=false) |
Set multiple digits | setDisplay(const byte values[], byte size=8) |
Show a string | setDisplayToString(const char* string, const word dots=0, const byte pos=0, const byte font[] = TM16XX_FONT_DEFAULT) |
Get button state | uint32_t getButtons() |
Complete examples can be found here.
All chip-specific classes are derived from the TM16xx base class. Depending on the interface they have one or more parameters to specify the connecting pins. For a full overview of all TM16xx chips and interfaces go to TM16xx chips features
Usage:
#include <TM1638.h>
#include <TM1637.h>
#include <TM1652.h>
TM1638 module1(8, 9, 7, 8); // dataPin: 8, clockPin: 9, strobePin: 7, 8 digits
TM1637 module2(3, 4, 4); // dataPin: 3, clockPin: 4, no strobePin, 4 digits
TM1652 module2(9, 4); // dataPin: 9, no clockPin, no strobePin, 4 digits
To initialize a module/chip begin()
can be called in setup()
. It typically calls calls setupDisplay() and clearDisplay() to as part of the initialization.
Usage:
setup() {
module.begin();
}
Note: Calling begin()
isn't mandatory. If it isn't called explicitly in setup()
, it will be called implicitly at the first displaying of data.
Set the display (segments and LEDs) on or off and set the intensity/brightness (range 0-7). Use this function for instance to change the brightness after measuring the surrounding light with a light dependent resistor (LDR).
Usage:
module.setupDisplay(true, 3); // on/off, intensity (range 0-7).
Note: when the display is switched off using setupDisplay()
, the chip/module will not be able to read buttons being pressed.
Sets flipped state of the display. When displaying numbers (or text) every digit will be rotated 180 degrees.
Note that this only affects subsequent displaying of data, not what is currently being shown. If affects all functions that display text or numbers but not setSegments()
and setSegments16()
.
Usage:
module.setDisplayFlipped(true); // flip all subsequent displaying of numbers/text 188 degrees.
Clear the display. All LEDs of the display will be switched of and no data is displayed.
Usage:
module.clearDisplay(); // clear all digits of the display.
Set up to 8 segments of display digit, one digit at a time.
- byte segments - Value representing the state of each segment. Order is bit 0 - 7 for segments A-G,DP (see below).
- byte position - The position of the digit on the display. Order is left to right. 0 is the leftmost digit.
Usage:
module.setSegments(123, 0); // Set the segments of digit 0 to 123, this is binary value
// 0111 1011 for segments ABDEFG, forming the lower case letter 'e'
Segment labels Segment bits
-- A -- -- 0 --
| | | |
F B 5 1
-- G -- -- 6 --
E C 4 2
| | | |
-- D -- .DP -- 3 -- .7
Common pinout 7-segments x 4 digits:
12 11 10 9 8 7
| | | | | |
+------------------------+ 12, 9, 8, 6: GRD1-GRD4
| -- -- -- -- |
| | | | | | | | | | 1: SEG_E 5: SEG_G
| -- -- -- -- | 2: SEG_D 7: SEG_B
| | | | | | | | | | 3: SEG_DP 10: SEG_F
| -- . -- . -- . -- . | 4: SEG_C 11: SEG_A
+------------------------+
| | | | | |
1 2 3 4 5 6
Set up to 16 segments of a display digit, one digit at a time. Some chips/modules can address more than 8 segments per position. Alpha-numeric displays can use this for more accurate character representation.
- uint16_t segments - Value representing the state of each segment. Order is bit 0 - 14 for segments A-G1,DP,G2-N.
- byte position - The position of the digit on the display. Order is left to right. 0 is the leftmost digit.
Usage:
module.setSegments16(0xFFFF, 0); // light up the all 16 segments of digit 0
14+1 segment labels and bit-assignment:
|---A---| Bit Segment Bit Segment
|\ | /| [0] A [ 8] G2
F H J K B [1] B [ 9] H
| \|/ | [2] C [10] J
|-G1-G2-| [3] D [11] K
| /|\ | [4] E [12] L
E N M L C [5] F [13] M
|/ | \| [6] G1 [14] N
|---D---| DP [7] DP
Pinout dual-digit LED-display marked 5241AS on module QYF0231 (bottom side up):
| | | | | | | | | Index Segment Pin Index Segment Pin
| | | | | | | | | [0] A 12 [ 8] G2 6
1 2 3 4 5 6 7 8 9 [1] B 10 [ 9] H 17
+-|-|-|-|-|-|-|-|-|-+ <-marked [2] C 9 [10] J 15
| o o x o o o o o o | side [3] D 7 [11] K 14
| . .| [4] E 1 [12] L 5
| 5241AS (Com.Cat.) | [5] F 18 [13] M 4
| 5241BS (Com.An.) | [6] G1 13 [14] N 2
| | [7] DP 8
| o o v o o o o v o |
+-|-|-|-|-|-|-|-|-|-+ (x) Pin 3 is not connected
1 1 1 1 1 1 1 1 1 (v) Anode/Cathode digit 1 is on pin 16
8 7 6 5 4 3 2 1 0 (v) Anode/Cathode digit 2 is on pin 11
| | | | | | | | |
Note: LED-display KYX-5241AS has the same pinout, but NC pin 3 is missing.
Note: For compatibility bits 0-7 are in the same order as A-G on a 7-segment display. Most characters in the TM16XX_FONT_15SEG font are based on their 7-seg counterparts to keep styling alike.
For the easy to use and familiar print()
method and other more advanced display methods you can use the TM16xxDisplay class.
setDisplayDigit(byte digit, byte pos=0, bool dot=false, const byte numberFont[] = TM16XX_NUMBER_FONT)
Set a single display at pos (starting at 0) to a digit (left to right).
- byte digit - the single digit number to be displayed
- byte pos - the position of the display
- bool dot - set decimal point/dot (DP) on or off
- numberFont[] - font used to display the numbers. Defaults to hexadecimal font
Usage:
module.setDisplayDigit(7,1,true); // show number 7 at position 1 with dot: 7.
Set the display to a decimal number.
- int nNumber - the multi digit number to be displayed. Will be right aligned with preceding zeros
- byte bDots - bitwise representation of the decimal point state between each digit
Usage:
module.setDisplayToDecNumber(1234, bit(2)); // show number 1234 with dot on position 2: 123.4
Clear a single display at pos (starting at 0, left to right)
Usage:
module.clearDisplayDigit(1, true); // clear position 1 but leave the dot
Set the segments of the display to specific values (left to right)
Usage:
module1.setDisplay((const byte[]) {0xFF, 0x3F, 0xFF}, 3); // set the segments of positions 0, 1 and 2 to 8.0.8.
setDisplayToString(const char* string, const word dots=0, const byte pos=0, const byte font[] = TM16XX_FONT_DEFAULT)
Set the display to the string (defaults to built in font)
- const char* string - the text to be displayed
- const word dots=0 - the state of the dots between each digit
- const byte pos=0 - the starting position of the text
- const byte font[] - the font to be used (defaults to 7-segment alphanumeric font)
Usage:
module.setDisplayToString("Tst", 0, 1); // show "Tst" at position 1
Use the TM16xxDisplay class for the easier to use familiar print()
method.
Key-scanning - return the state of up to 32 keys.
Note: not all TM16xx chips support key-scanning and sizes are different per chip. Up to 32 key states are supported, but specific chips may support less keys or less combinations. For a full overview of all TM16xx chips and the number of buttons that can be scanned, go to TM16xx chips features.
The chip specific derived class method will return a 32-bit value representing the state of each key. Returns 0 if no key is pressed.
Usage:
uint32_t dwButtons=module.getButtons();
if(uButtons!=0)
{
// do something
Serial.println(dwButtons, HEX);
}
Please note that while you don't need to write any code for debouncing, the button state may be reset when you display something. For advanced detection of button clicks, double clicks and long presses you can use the TM16xxButtons class.