PIC and TM1638 LED&KEY module

These are interesting modules that you can buy from various online sources. They comprise of 2 4×7 7 Segment displays, 8 push buttons and 8 LEDs. The chip that performs all the magic is a TM1638.

The module I bought had 5 connections.

VCC – 5v
Gnd – GND
STB – strobe pin
CLK – clock pin
DIO –  data pin

 

Schematic

The board I used had a PIC 18F4550 on it

If you are interested the board is available from (UK) – RKPK40 Prototype PCB for 40 pin PIC

In the code example later you can see the pins used, they are pins 5, 6 and 7 on PORTB

#define DI PORTB.F5
#define CLK PORTB.F6
#define STB PORTB.F7

Code

This was a MIKROC example – https://libstock.mikroe.com/projects/view/2120/tm1638-led-amp-key

[codesyntax lang=”cpp”]

///////////////////////////////////////////////////////
// //
// TM1638 LED&KEY module //
// MikroC for PIC //
// //
///////////////////////////////////////////////////////

//PIC 12
//#define DI GPIO.F2
//#define DO TRISIO.F2
//#define CLK GPIO.F1
//#define STB GPIO.F0

// PIC 16
//#define DI PORTB.F5
////#define DO TRISB.F5
//#define CLK PORTB.F6
//#define STB PORTB.F7

//PIC 18
#define DI PORTB.F5
#define DO LATB.F5
#define CLK PORTB.F6
#define STB PORTB.F7

///////////////////////////////////////////////////////
// //
// 7 Segment common Cathode HGFEDCBA codes //
// code, // number description //
// //
///////////////////////////////////////////////////////
const char FontNumber[] =
{
0x3F, // 0 0
0x06, // 1 1
0x5B, // 2 2
0x4F, // 3 3
0x66, // 4 4
0x6D, // 5 5
0x7D, // 6 6
0x07, // 7 7
0x7F, // 8 8
0x6F, // 9 9
0x77, // 10 A
0x7C, // 11 B
0x39, // 12 C
0x5E, // 13 D
0x79, // 14 E
0x71, // 15 F
0x3D, // 16 G
0x76, // 17 H
0x00, // 18
0x40, // 19 -
0x80, // 20 .
0x63, // 21 *
0x78, // 22 t
0x1E, // 23 J
0x38, // 24 L
};

const char FontChar[] =
{
0b00000000, // (32) <space>
0b10000110, // (33) !
0b00100010, // (34) "
0b01111110, // (35) #
0b01101101, // (36) $
0b00000000, // (37) %
0b00000000, // (38) &
0b00000010, // (39) '
0b00110000, // (40) (
0b00000110, // (41) )
0b01100011, // (42) *
0b00000000, // (43) +
0b00000100, // (44) ,
0b01000000, // (45) -
0b10000000, // (46) .
0b01010010, // (47) /
0b00111111, // (48) 0
0b00000110, // (49) 1
0b01011011, // (50) 2
0b01001111, // (51) 3
0b01100110, // (52) 4
0b01101101, // (53) 5
0b01111101, // (54) 6
0b00100111, // (55) 7
0b01111111, // (56) 8
0b01101111, // (57) 9
0b00000000, // (58) :
0b00000000, // (59) ;
0b00000000, // (60) <
0b01001000, // (61) =
0b00000000, // (62) >
0b01010011, // (63) ?
0b01011111, // (64) @
0b01110111, // (65) A
0b01111111, // (66) B
0b00111001, // (67) C
0b00111111, // (68) D
0b01111001, // (69) E
0b01110001, // (70) F
0b00111101, // (71) G
0b01110110, // (72) H
0b00000110, // (73) I
0b00011111, // (74) J
0b01101001, // (75) K
0b00111000, // (76) L
0b00010101, // (77) M
0b00110111, // (78) N
0b00111111, // (79) O
0b01110011, // (80) P
0b01100111, // (81) Q
0b00110001, // (82) R
0b01101101, // (83) S
0b01111000, // (84) T
0b00111110, // (85) U
0b00101010, // (86) V
0b00011101, // (87) W
0b01110110, // (88) X
0b01101110, // (89) Y
0b01011011, // (90) Z
0b00111001, // (91) [
0b01100100, // (92) \ (this can't be the last char on a line, even in comment or it'll concat)
0b00001111, // (93) ]
0b00000000, // (94) ^
0b00001000, // (95) _
0b00100000, // (96) `
0b01011111, // (97) a
0b01111100, // (98) b
0b01011000, // (99) c
0b01011110, // (100) d
0b01111011, // (101) e
0b00110001, // (102) f
0b01101111, // (103) g
0b01110100, // (104) h
0b00000100, // (105) i
0b00001110, // (106) j
0b01110101, // (107) k
0b00110000, // (108) l
0b01010101, // (109) m
0b01010100, // (110) n
0b01011100, // (111) o
0b01110011, // (112) p
0b01100111, // (113) q
0b01010000, // (114) r
0b01101101, // (115) s
0b01111000, // (116) t
0b00011100, // (117) u
0b00101010, // (118) v
0b00011101, // (119) w
0b01110110, // (120) x
0b01101110, // (121) y
0b01000111, // (122) z
0b01000110, // (123) {
0b00000110, // (124) |
0b01110000, // (125) }
0b00000001, // (126) ~
};

void Write1638(unsigned char Data)
{
unsigned char i;
for(i=0 ; i<8 ; i++)
{
DI = (Data & 0x01);
CLK = 0;
Data >>= 1;
CLK = 1;
}
}

void WriteCmd(unsigned char cmd)
{
STB = 0;
Write1638(cmd);
STB = 1;
}

///////////////////////////////////////////////////////
// //
// Read buttons //
// 1F 17 0F 07 1B 13 0B 03 //
// //
///////////////////////////////////////////////////////
unsigned char ReadKey(void)
{
unsigned char i;
unsigned char temp=32;
STB = 0;
Write1638(0x42); // Switch to read mode
DO = 1; // Set DIO = input
for(i=0; i<32; i++) // Read 4 bytes
{
temp--;
CLK = 0;
delay_us(1); // Fast MCU (>4MHz)
if(DI)
temp<<=1;
CLK = 1;
}
DO = 0; // Set DIO = output
STB = 1;
return temp; // 0x00 ~ 0xFF
}

void WriteData(unsigned char Adress,unsigned char Data)
{
WriteCmd(0x44);
STB = 0;
Write1638(0xC0 | Adress);
Write1638(Data);
STB = 1;
}

///////////////////////////////////////////////////////
// //
// Clear display, turn leds off //
// //
///////////////////////////////////////////////////////
void LedReset (void)
{
unsigned char i;
CLK = 1;
WriteCmd(0x40);
STB = 0;
Write1638(0xC0);
for(i=0 ; i<32 ; i++)
{
Write1638(0x00);
}
STB = 1;
CLK = 1;
}

///////////////////////////////////////////////////////
// //
// Led - positions 1,3,5,7,9,11,13,15 //
// 0 - turn led ON, 1 - turn OFF //
// 7Seg - positions 0,2,4,6,8,10,12,14 //
// //
///////////////////////////////////////////////////////
void WriteDigit(char number,char pos)
{
WriteCmd(0x44);
STB = 0;
Write1638(0xC0 | pos);
Write1638(FontNumber[number]);
STB = 1;
}

void DisplayLongNumber(signed long number)
{
unsigned char und,dec,cnt,mil,decmil,cntmil,mlh,decmlh;
signed long tmp;
und = number%10;
tmp = number/10;
dec = tmp%10;
tmp = tmp/10;
cnt = tmp%10;
tmp = tmp/10;
mil = tmp%10;
tmp = tmp/10;
decmil = tmp%10;
tmp = tmp/10;
cntmil = tmp%10;
tmp = tmp/10;
mlh = tmp%10;
tmp = tmp/10;
decmlh = tmp%10;
CLK = 1;
WriteCmd(0x40);
STB = 0;
Write1638(0xC0);
Write1638(FontNumber[decmlh]);
Write1638(0x00);
Write1638(FontNumber[mlh]);
Write1638(0x00);
Write1638(FontNumber[cntmil]);
Write1638(0x00);
Write1638(FontNumber[decmil]);
Write1638(0x00);
Write1638(FontNumber[mil]);
Write1638(0x00);
Write1638(FontNumber[cnt]);
Write1638(0x00);
Write1638(FontNumber[dec]);
Write1638(0x00);
Write1638(FontNumber[und]);
Write1638(0x00);
STB = 1;
CLK = 1;
}

void DisplayString(const char *msg)
{
CLK = 1;
WriteCmd(0x40);
STB = 0;
Write1638(0xC0);
Write1638(FontChar[msg[0]-32]);
Write1638(0x00);
Write1638(FontChar[msg[1]-32]);
Write1638(0x00);
Write1638(FontChar[msg[2]-32]);
Write1638(0x00);
Write1638(FontChar[msg[3]-32]);
Write1638(0x00);
Write1638(FontChar[msg[4]-32]);
Write1638(0x00);
Write1638(FontChar[msg[5]-32]);
Write1638(0x00);
Write1638(FontChar[msg[6]-32]);
Write1638(0x00);
Write1638(FontChar[msg[7]-32]);
Write1638(0x00);
STB = 1;
CLK = 1;
}

void main(void)
{
//TRISIO = 0x00;
//GPIO = 0x00;
//ANSEL = 0x00; // AD Disabled
TRISA = 0x00;
TRISB = 0x00;
TRISC = 0x00;
//TRISD = 0x00;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
//PORTD = 0x00;
ADCON0 = 0x00; // AD Disabled
ADCON1 = 0x06; // All inputs are digital
//T0CON = 0x00;
//INTCON = 0x00;
//INTCON2 = 0x00;
//CMCON = 0x07; // Compare disabled
delay_ms(1000); // Power-Up recovery delay
WriteCmd(0x8F); // Light level (8,9,A,B,C,D,E,F)
LedReset();

///////////////////////////////////////////////////////
// //
// DEMO TEST //
// //
///////////////////////////////////////////////////////
while(1)
{
DisplayLongNumber(76543210);
delay_ms(2000);
DisplayString("--OFF---");
delay_ms(1000);
WriteCmd(0x80); // Display off
delay_ms(1000);
WriteCmd(0x88); // Light min
DisplayString("-LLL-001");
delay_ms(1000);
WriteCmd(0x8B);
DisplayString("-LLL-004");
delay_ms(1000);
WriteCmd(0x8F); // Light max
DisplayString("-LLL-008");
delay_ms(1000);
LedReset();
delay_ms(1000);
WriteDigit (0,1); // Leds...
WriteDigit (10,0);
delay_ms(200);
WriteDigit (0,3);
WriteDigit (11,2);
delay_ms(200);
WriteDigit (0,5);
WriteDigit (12,4);
delay_ms(200);
WriteDigit (0,7);
WriteDigit (13,6);
delay_ms(200);
WriteDigit (0,9);
WriteDigit (14,8);
delay_ms(200);
WriteDigit (0,11);
WriteDigit (15,10);
delay_ms(200);
WriteDigit (0,13);
WriteDigit (16,12);
delay_ms(200);
WriteDigit (0,15);
WriteDigit (17,14);
delay_ms(2000);
{ // Keys test
unsigned char i;
for(i=0 ; i<100 ; i++)
{
DisplayLongNumber(ReadKey());
delay_ms(200);
}
}
DisplayString("SOFt OFF");
delay_ms(1000);
LedReset();
delay_ms(1000);
}
}

[/codesyntax]

 

Links

 

1pc Key Display For AVR Arduino New 8-Bit Digital LED Tube 8-Bit TM1638 Module

Related posts

DsPIC30F4011 Development Board OLED example

DsPIC30F4011 Development Board push button example

DsPIC30F4011 Development Board flashing LED example

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More