Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TM1637
======
Arduino library for TM1637 (LED Driver)
Arduino library for TM1637 (LED Driver).


Description
Expand All @@ -9,7 +9,7 @@ An Arduino library for 7-segment display modules based on the TM1637 chip, such

Hardware Connection
-------------------
The display modules has two signal connection (and two power connections) which are CLK and DIO. These pins can be connected to any pair of digital pins on the Arduino. When an object is created, the pins should be configured. There is no limitation on the number of instances used concurrently (as long as each instance has a pin pair of its own)
The display modules has two signal connection, CLK and DIO, as well as two power connections, VCC and GND. The signal pins can be connected to any pair of digital pins on the Arduino. When an object is created, the pins should be configured. There is no limitation on the number of instances used concurrently (as long as each instance has a pin pair of its own).

Installation
------------
Expand All @@ -24,4 +24,4 @@ The library provides a single class named TM1637Display. An instance of this cla
* `showNumberDecEx` - Display a decimal number with decimal points or colon
* `setBrightness` - Sets the brightness of the display

The information given above is only a summary. Please refer to TM1637Display.h for more information. An example is included, demonstrating the operation of most of the functions.
The information given above is only a summary. Please refer to `TM1637Display.h` for more information. An example is included, demonstrating the operation of most of the functions.
201 changes: 100 additions & 101 deletions TM1637Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

extern "C" {
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
}

#include <TM1637Display.h>
Expand All @@ -37,24 +37,24 @@ extern "C" {
// ---
// D
const uint8_t digitToSegment[] = {
// XGFEDCBA
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9
0b01110111, // A
0b01111100, // b
0b00111001, // C
0b01011110, // d
0b01111001, // E
0b01110001 // F
};
// XGFEDCBA
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9
0b01110111, // A
0b01111100, // b
0b00111001, // C
0b01011110, // d
0b01111001, // E
0b01110001 // F
};

static const uint8_t minusSegments = 0b01000000;

Expand All @@ -67,8 +67,8 @@ TM1637Display::TM1637Display(uint8_t pinClk, uint8_t pinDIO, unsigned int bitDel

// Set the pin direction and default value.
// Both pins are set as inputs, allowing the pull-up resistors to pull them up
pinMode(m_pinClk, INPUT);
pinMode(m_pinDIO,INPUT);
pinMode(m_pinClk, INPUT);
pinMode(m_pinDIO, INPUT);
digitalWrite(m_pinClk, LOW);
digitalWrite(m_pinDIO, LOW);
}
Expand All @@ -80,7 +80,7 @@ void TM1637Display::setBrightness(uint8_t brightness, bool on)

void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_t pos)
{
// Write COMM1
// Write COMM1
start();
writeByte(TM1637_I2C_COMM1);
stop();
Expand All @@ -91,7 +91,7 @@ void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_

// Write the data bytes
for (uint8_t k=0; k < length; k++)
writeByte(segments[k]);
writeByte(segments[k]);

stop();

Expand All @@ -103,78 +103,78 @@ void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_

void TM1637Display::clear()
{
uint8_t data[] = { 0, 0, 0, 0 };
uint8_t data[] = { 0, 0, 0, 0 };
setSegments(data);
}

void TM1637Display::showNumberDec(int num, bool leading_zero, uint8_t length, uint8_t pos)
{
showNumberDecEx(num, 0, leading_zero, length, pos);
showNumberDecEx(num, 0, leading_zero, length, pos);
}

void TM1637Display::showNumberDecEx(int num, uint8_t dots, bool leading_zero,
uint8_t length, uint8_t pos)
{
showNumberBaseEx(num < 0? -10 : 10, num < 0? -num : num, dots, leading_zero, length, pos);
showNumberBaseEx(num < 0? -10 : 10, num < 0? -num : num, dots, leading_zero, length, pos);
}

void TM1637Display::showNumberHexEx(uint16_t num, uint8_t dots, bool leading_zero,
uint8_t length, uint8_t pos)
{
showNumberBaseEx(16, num, dots, leading_zero, length, pos);
showNumberBaseEx(16, num, dots, leading_zero, length, pos);
}

void TM1637Display::showNumberBaseEx(int8_t base, uint16_t num, uint8_t dots, bool leading_zero,
uint8_t length, uint8_t pos)
{
bool negative = false;
bool negative = false;

if (base < 0) {
base = -base;
base = -base;
negative = true;
}


uint8_t digits[4];
uint8_t digits[4];

if (num == 0 && !leading_zero) {
// Singular case - take care separately
for(uint8_t i = 0; i < (length-1); i++)
for (uint8_t i = 0; i < (length-1); i++)
digits[i] = 0;
digits[length-1] = encodeDigit(0);
}
else {
//uint8_t i = length-1;
//if (negative) {
// // Negative number, show the minus sign
// digits[i] = minusSegments;
// i--;
//}
for(int i = length-1; i >= 0; --i)
// uint8_t i = length-1;
// if (negative) {
// // Negative number, show the minus sign
// digits[i] = minusSegments;
// i--;
// }

for (int i = length-1; i >= 0; --i)
{
uint8_t digit = num % base;
uint8_t digit = num % base;

if (digit == 0 && num == 0 && leading_zero == false)
// Leading zero is blank
// Leading zero is blank
digits[i] = 0;
else
digits[i] = encodeDigit(digit);
digits[i] = encodeDigit(digit);

if (digit == 0 && num == 0 && negative) {
digits[i] = minusSegments;
digits[i] = minusSegments;
negative = false;
}

num /= base;
}
}
if(dots != 0)
}

if (dots != 0)
{
showDots(dots, digits);
}
setSegments(digits, length, pos);

setSegments(digits, length, pos);
}

void TM1637Display::bitDelay()
Expand All @@ -184,8 +184,8 @@ void TM1637Display::bitDelay()

void TM1637Display::start()
{
pinMode(m_pinDIO, OUTPUT);
bitDelay();
pinMode(m_pinDIO, OUTPUT);
bitDelay();
}

void TM1637Display::stop()
Expand All @@ -200,56 +200,55 @@ void TM1637Display::stop()

bool TM1637Display::writeByte(uint8_t b)
{
uint8_t data = b;

// 8 Data Bits
for(uint8_t i = 0; i < 8; i++) {
// CLK low
pinMode(m_pinClk, OUTPUT);
bitDelay();

// Set data bit
if (data & 0x01)
pinMode(m_pinDIO, INPUT);
else
pinMode(m_pinDIO, OUTPUT);

bitDelay();

// CLK high
pinMode(m_pinClk, INPUT);
bitDelay();
data = data >> 1;
}

// Wait for acknowledge
// CLK to zero
pinMode(m_pinClk, OUTPUT);
pinMode(m_pinDIO, INPUT);
bitDelay();

// CLK to high
pinMode(m_pinClk, INPUT);
bitDelay();
uint8_t ack = digitalRead(m_pinDIO);
if (ack == 0)
pinMode(m_pinDIO, OUTPUT);


bitDelay();
pinMode(m_pinClk, OUTPUT);
bitDelay();

return ack;
uint8_t data = b;

// 8 Data Bits
for(uint8_t i = 0; i < 8; i++) {
// CLK low
pinMode(m_pinClk, OUTPUT);
bitDelay();

// Set data bit
if (data & 0x01)
pinMode(m_pinDIO, INPUT);
else
pinMode(m_pinDIO, OUTPUT);

bitDelay();

// CLK high
pinMode(m_pinClk, INPUT);
bitDelay();
data = data >> 1;
}

// Wait for acknowledge
// CLK to zero
pinMode(m_pinClk, OUTPUT);
pinMode(m_pinDIO, INPUT);
bitDelay();

// CLK to high
pinMode(m_pinClk, INPUT);
bitDelay();
uint8_t ack = digitalRead(m_pinDIO);
if (ack == 0)
pinMode(m_pinDIO, OUTPUT);

bitDelay();
pinMode(m_pinClk, OUTPUT);
bitDelay();

return ack;
}

void TM1637Display::showDots(uint8_t dots, uint8_t* digits)
{
for(int i = 0; i < 4; ++i)
{
digits[i] |= (dots & 0x80);
dots <<= 1;
}
for (int i = 0; i < 4; ++i)
{
digits[i] |= (dots & 0x80);
dots <<= 1;
}
}

uint8_t TM1637Display::encodeDigit(uint8_t digit)
Expand Down
21 changes: 10 additions & 11 deletions TM1637Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,23 @@ class TM1637Display {
static uint8_t encodeDigit(uint8_t digit);

protected:
void bitDelay();
void bitDelay();

void start();
void start();

void stop();
void stop();

bool writeByte(uint8_t b);
bool writeByte(uint8_t b);

void showDots(uint8_t dots, uint8_t* digits);

void showNumberBaseEx(int8_t base, uint16_t num, uint8_t dots = 0, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);
void showDots(uint8_t dots, uint8_t* digits);

void showNumberBaseEx(int8_t base, uint16_t num, uint8_t dots = 0, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);

private:
uint8_t m_pinClk;
uint8_t m_pinDIO;
uint8_t m_brightness;
unsigned int m_bitDelay;
uint8_t m_pinClk;
uint8_t m_pinDIO;
uint8_t m_brightness;
unsigned int m_bitDelay;
};

#endif // __TM1637DISPLAY__
Loading