Initial Revision
This commit is contained in:
219
TM1638.cpp
Normal file
219
TM1638.cpp
Normal file
@@ -0,0 +1,219 @@
|
||||
/*
|
||||
TM1638.h - Library for TM1638.
|
||||
|
||||
Copyright (C) 2011 Ricardo Batista (rjbatista <at> gmail <dot> com)
|
||||
|
||||
Based on a sketch by: Martin Hubacek (http://www.martinhubacek.cz)
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the version 3 GNU General Public License as
|
||||
published by the Free Software Foundation.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "WProgram.h"
|
||||
#include "TM1638.h"
|
||||
|
||||
/*
|
||||
The bits are displayed by mapping bellow
|
||||
-- 0 --
|
||||
| |
|
||||
5 1
|
||||
-- 6 --
|
||||
4 2
|
||||
| |
|
||||
-- 3 -- .7
|
||||
*/
|
||||
const byte NUMBER_DATA[] = {
|
||||
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
|
||||
};
|
||||
|
||||
const byte ERROR[] = {
|
||||
0b01111001, // E
|
||||
0b01010000, // r
|
||||
0b01010000, // r
|
||||
0b01011100, // o
|
||||
0b01010000, // r
|
||||
0,
|
||||
0,
|
||||
0
|
||||
};
|
||||
|
||||
TM1638::TM1638(byte dataPin, byte clockPin, byte strobePin)
|
||||
{
|
||||
this->dataPin = dataPin;
|
||||
this->clockPin = clockPin;
|
||||
this->strobePin = strobePin;
|
||||
|
||||
pinMode(dataPin, OUTPUT);
|
||||
pinMode(clockPin, OUTPUT);
|
||||
pinMode(strobePin, OUTPUT);
|
||||
|
||||
digitalWrite(strobePin, HIGH);
|
||||
digitalWrite(clockPin, HIGH);
|
||||
|
||||
sendCommand(0x8B);
|
||||
sendCommand(0x40);
|
||||
|
||||
digitalWrite(strobePin, LOW);
|
||||
send(0xC0);
|
||||
for (int i = 0; i < 16; i++) {
|
||||
send(0x00);
|
||||
}
|
||||
digitalWrite(strobePin, HIGH);
|
||||
}
|
||||
|
||||
void TM1638::setDisplayToHexNumber(unsigned long number, byte dots)
|
||||
{
|
||||
for (int i = 0; i < 8; i++) {
|
||||
setDisplayDigit(number & 0xF, 7 - i, (dots & (1 << i)) != 0);
|
||||
number >>= 4;
|
||||
}
|
||||
}
|
||||
|
||||
void TM1638::setDisplayToDecNumber(unsigned long number, byte dots)
|
||||
{
|
||||
if (number > 99999999L) {
|
||||
setDisplay(ERROR);
|
||||
} else {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
if (number != 0) {
|
||||
setDisplayDigit(number % 10, 7 - i, (dots & (1 << i)) != 0);
|
||||
number /= 10;
|
||||
} else {
|
||||
setDisplayDigit(0, 7 - i, (dots & (1 << i)) != 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TM1638::setDisplayToBinNumber(byte number, byte dots)
|
||||
{
|
||||
for (int i = 0; i < 8; i++) {
|
||||
setDisplayDigit((number & (1 << i)) == 0 ? 0 : 1, 7 - i, (dots & (1 << i)) != 0);
|
||||
}
|
||||
}
|
||||
|
||||
void TM1638::setDisplayDigit(byte digit, byte pos, boolean dot)
|
||||
{
|
||||
sendData(pos << 1, NUMBER_DATA[digit & 0xF] | (dot ? 0b10000000 : 0));
|
||||
}
|
||||
|
||||
void TM1638::setDisplay(const byte values[])
|
||||
{
|
||||
for (int i = 0; i < 8; i++) {
|
||||
sendData(i << 1, values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void TM1638::setLED(byte color, byte pos)
|
||||
{
|
||||
sendData((pos << 1) + 1, color);
|
||||
}
|
||||
|
||||
void TM1638::setLEDs(word leds)
|
||||
{
|
||||
for (int i = 0; i < 8; i++) {
|
||||
byte val = 0;
|
||||
|
||||
if (leds & (1 << i)) {
|
||||
val |= 1;
|
||||
}
|
||||
|
||||
if (leds & (1 << (i + 8))) {
|
||||
val |= 2;
|
||||
}
|
||||
|
||||
sendData((i << 1) + 1, val);
|
||||
}
|
||||
}
|
||||
|
||||
byte TM1638::getButtons(void)
|
||||
{
|
||||
byte keys = 0;
|
||||
|
||||
digitalWrite(strobePin, LOW);
|
||||
send(0x42);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
keys |= receive() << i;
|
||||
}
|
||||
digitalWrite(strobePin, HIGH);
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
void TM1638::sendCommand(byte cmd)
|
||||
{
|
||||
digitalWrite(strobePin, LOW);
|
||||
send(cmd);
|
||||
digitalWrite(strobePin, HIGH);
|
||||
}
|
||||
|
||||
void TM1638::sendData(byte add, byte data)
|
||||
{
|
||||
sendCommand(0x44);
|
||||
digitalWrite(strobePin, LOW);
|
||||
send(0xc0 | add);
|
||||
send(data);
|
||||
digitalWrite(strobePin, HIGH);
|
||||
}
|
||||
|
||||
void TM1638::send(byte data)
|
||||
{
|
||||
for (int i = 0; i < 8; i++) {
|
||||
digitalWrite(clockPin, LOW);
|
||||
digitalWrite(dataPin, data & 1 ? HIGH : LOW);
|
||||
data >>= 1;
|
||||
digitalWrite(clockPin, HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
byte TM1638::receive()
|
||||
{
|
||||
byte temp = 0;
|
||||
|
||||
// Pull-up on
|
||||
pinMode(dataPin, INPUT);
|
||||
digitalWrite(dataPin, HIGH);
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
temp >>= 1;
|
||||
|
||||
digitalWrite(clockPin, LOW);
|
||||
|
||||
if (digitalRead(dataPin)) {
|
||||
temp |= 0x80;
|
||||
}
|
||||
|
||||
digitalWrite(clockPin, HIGH);
|
||||
}
|
||||
|
||||
// Pull-up off
|
||||
pinMode(dataPin, OUTPUT);
|
||||
digitalWrite(dataPin, LOW);
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
66
TM1638.h
Normal file
66
TM1638.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
TM1638.h - Library for TM1638.
|
||||
|
||||
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>
|
||||
|
||||
Based on a sketch by: Martin Hubacek (http://www.martinhubacek.cz)
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the version 3 GNU General Public License as
|
||||
published by the Free Software Foundation.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef TM1638_h
|
||||
#define TM1638_h
|
||||
|
||||
#include <WProgram.h>
|
||||
|
||||
#define TM1638_COLOR_RED 1
|
||||
#define TM1638_COLOR_GREEN 2
|
||||
|
||||
class TM1638
|
||||
{
|
||||
public:
|
||||
TM1638(byte dataPin, byte clockPin, byte strobePin);
|
||||
|
||||
/** Set the display to a unsigned hexadecimal number */
|
||||
void setDisplayToHexNumber(unsigned long number, byte dots);
|
||||
/** Set the display to a unsigned decimal number */
|
||||
void setDisplayToDecNumber(unsigned long number, byte dots);
|
||||
/** Set the display to a unsigned binary number */
|
||||
void setDisplayToBinNumber(byte number, byte dots);
|
||||
/** Set a single display at pos (starting at 0) to a digit (left to right) */
|
||||
void setDisplayDigit(byte digit, byte pos, boolean dot);
|
||||
/** Set the display to the 8 values (left to right) */
|
||||
void setDisplay(const byte values[]);
|
||||
|
||||
/** Set the LED at pos to color (TM1638_COLOR_RED, TM1638_COLOR_GREEN or both) */
|
||||
void setLED(byte color, byte pos);
|
||||
/** Set the LEDs. MSB byte for the green LEDs, LSB for the red LEDs */
|
||||
void setLEDs(word led);
|
||||
|
||||
/** Returns the pressed buttons as a bit set (left to right). */
|
||||
byte getButtons();
|
||||
|
||||
protected:
|
||||
void sendCommand(byte led);
|
||||
void sendData(byte add, byte data);
|
||||
void send(byte data);
|
||||
byte receive();
|
||||
|
||||
private:
|
||||
byte dataPin;
|
||||
byte clockPin;
|
||||
byte strobePin;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
25
keywords.txt
Normal file
25
keywords.txt
Normal file
@@ -0,0 +1,25 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For TM1638 Lib
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
TM1638 KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
setSegments KEYWORD2
|
||||
setLEDs KEYWORD2
|
||||
getButtons KEYWORD2
|
||||
sendCommand KEYWORD2
|
||||
sendData KEYWORD2
|
||||
send KEYWORD2
|
||||
receive KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
57
tm1638_lib.pde
Normal file
57
tm1638_lib.pde
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "TM1638.h"
|
||||
|
||||
unsigned char hello[] = {
|
||||
0b00000000, 0b01110110, 0b01111001, 0b00111000, 0b00111000, 0b00111111, 0b00000000, 0b00000000
|
||||
};
|
||||
|
||||
TM1638 module1(2, 3, 4);
|
||||
TM1638 module2(2, 3, 5);
|
||||
|
||||
unsigned long value = 87654321L;
|
||||
|
||||
void setup()
|
||||
{
|
||||
module1.setDisplay(helloLCD);
|
||||
module2.setDisplay(helloLCD);
|
||||
|
||||
module1.setLEDs(0b00011111 | 0b11111000 << 8);
|
||||
|
||||
module2.setLED(TM1638_COLOR_RED, 3);
|
||||
module2.setLED(TM1638_COLOR_GREEN, 5);
|
||||
module2.setLED(TM1638_COLOR_RED | TM1638_COLOR_GREEN, 7);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
byte key1, key2;
|
||||
|
||||
key1 = module1.getButtons();
|
||||
key2 = module2.getButtons();
|
||||
|
||||
// both pressed
|
||||
if (key1 != 0 && key2 != 0) {
|
||||
value = 0;
|
||||
|
||||
module1.setDisplayToHexNumber(value, 0b10101010);
|
||||
module2.setDisplayToDecNumber(value, 0b01010101);
|
||||
} else {
|
||||
if (key1 != 0) {
|
||||
value += key1;
|
||||
|
||||
module2.setDisplayToBinNumber(key1, 0);
|
||||
|
||||
module1.setLEDs(key1);
|
||||
}
|
||||
|
||||
if (key2 != 0) {
|
||||
value += key2;
|
||||
|
||||
module1.setDisplayToHexNumber(value, 0b10101010);
|
||||
module2.setDisplayToDecNumber(value, 0b01010101);
|
||||
|
||||
module2.setLEDs(key2 << 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user