Jump to content
43oh

TM1638 LEDs interface _version 1.7.0 available!


Recommended Posts

Hello!

My library for interacting an MSP430g2553 with a TM1638 (SPI 3-wire mode)

post-33401-135135554317_thumb.png

Description

MSP430G2xx3 - USCI_A0 SPI Master Interface to TM1638, Read/Write command, key scanning, show digit and switch LEDs on board.
TM1638 - Driver for Led's indicator, 10 segment output lines, 8 grid output lines, 8 segment/key - 3 lines scanning.

Features
Support for the TM1638;
Support for dimming the display and LEDs;
Reading simultaneous button presses
Connection diagramm:
post-33401-135135554595_thumb.gif

Code:

#include "msp430g2553.h"

// MSP430 Ports Define
#define LED_RED BIT0 //RED Led
#define LED_GRE BIT6 //Green Led
#define STROBE_TM1638 BIT5
#define DIO BIT2
#define CLK BIT4

// TM1638 Commands Define
#define RED_TM1638 0x01 //RED Led
#define GRE_TM1638 0x02 //Green Led
#define DATA_WRITE_INCR_ADDR 0x40 //Command to switch TM1638 for automatic increment address mode
#define DATA_WRITE_FIX_ADDR 0x44 //Command to switch TM1638 for fix address mode
#define DATA_READ_KEY_SCAN_MODE 0x42 //Command for read key code from TM1638
#define ADDRSET 0xC0 //Command to set address 0x00
#define DISP_ON 0x8F //Command to Display ON and set (max) brightness
#define DISP_OFF 0x80 //Command to Display OFF
//#define MIN_VOLT 2700 //Critycal voltage of power OFF


const unsigned int Num[]= //Code table of symbols
{
0x3F, //0
0x06, //1
0x5B, //2
0x4F, //3
0x66, //4
0x6D, //5
0x7D, //6
0x07, //7
0x7F, //8
0x6F, //9
0x40, //Minus
0x63, //Degree
0x39, //"C"
0x00, //Blank
0x3E //"U"
};

const unsigned int ERROR_DATA[] = {
0x79, // E
0x50, // r
0x50, // r
0x5C, // o
0x50, // r
0,
0,
0
};

//Function definitions

void init_Ports()
{
P1OUT = 0x00; // P1 setup for LED & STROBE output
P2OUT = 0x00; // P2 setup for output
P1DIR = LED_RED + LED_GRE + STROBE_TM1638;
P1SEL = BIT1 + BIT2 + BIT4; // Set secondary functions for PORT1
P1SEL2 = BIT1 + BIT2 + BIT4; // P1.1 - TXD, P1.2 - RXD
P1OUT |= STROBE_TM1638; // Chip Select, Set STROBE = "1"
}

void init_WDT()
{
WDTCTL = WDTPW + WDTHOLD; //Stop watchdog
}

void init_SPI()
{
UCA0CTL0 |= UCCKPL + UCMST + UCSYNC; // 3-pin, 8-bit SPI master
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 |= 0x02; // /2
UCA0BR1 = 0; //
UCA0MCTL = 0; // No modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}


void SendCommand(unsigned char Command) { //Transmit Command
P1OUT &= ~STROBE_TM1638; //Set STROBE = "0"
_NOP();
_NOP();
P1OUT |= STROBE_TM1638; //Set STROBE = "1"
_NOP();
_NOP();
P1OUT &= ~STROBE_TM1638; //Set STROBE = "0"
_NOP();
UCA0TXBUF = Command;
while (!(IFG2 & UCA0TXIFG));
P1OUT |= STROBE_TM1638; //Set STROBE = "1"
}

void SendData(unsigned int address, unsigned int data) { //Transmit Data
SendCommand(DATA_WRITE_FIX_ADDR);
P1OUT &= ~STROBE_TM1638; //Set STROBE = "0"
UCA0TXBUF = (0xC0 | address);
while (!(IFG2 & UCA0TXIFG));
UCA0TXBUF = data;
while (!(IFG2 & UCA0TXIFG));
P1OUT |= STROBE_TM1638; //Set STROBE = "1"
}

void ShowDig(int position, int Data, int Dot) //show single digit
{
SendData(position << 1, Data | (Dot ? 0x80 : 0) );
}

void ClearDig(unsigned int pos, unsigned int dot)
{
ShowDig(pos, 0x00, dot);
}

void ShowLed(int Number, int Color)
{
SendData((Number << 1)-1, Color);
}

void ShowLeds(int color) {
unsigned int i;
for (i=1; i<9; i++) {
ShowLed (i, color);
}
}

void DisplayClean() { //Clean RAM of TM1638
int i;
for (i=0; i<17; i++) {
SendData(i, 0x00);
}
}

void SetupDisplay(char active, char intensity) {
SendCommand (0x80 | (active ? 8 : 0) | intensity);
P1OUT |= STROBE_TM1638; // Set STROBE = "1"
}

void init_Display() {
__delay_cycles(100000); //Time to initial TM1638
DisplayClean(); //Clean display
SendCommand(DISP_OFF); //Display off
SendCommand(DATA_WRITE_FIX_ADDR); //Set address mode
SendCommand(ADDRSET); //Set first adress
}

void ShowError() {
unsigned int j;
for (j=0; j<8; j++) {
ShowDig(j, ERROR_DATA[j], 0);
}
}

int GetKey() {
unsigned int KeyData = 0;
unsigned int i;

P1OUT &= ~STROBE_TM1638; // Set STROBE = "0"
UCA0TXBUF = DATA_READ_KEY_SCAN_MODE;
while (!(IFG2 & UCA0TXIFG));
__delay_cycles(20); //wait to ready (see datasheet)

UCA0TXBUF = 0xff; //Send dummy byte
while (!(IFG2 & UCA0TXIFG)); //wait for buffer ready
// 1'st reseiving byte = bad

for (i=0; i<4; i++) {
UCA0TXBUF = 0xff;
while (!(IFG2 & UCA0RXIFG));
KeyData |= UCA0RXBUF << i;
}
P1OUT |= STROBE_TM1638; //Set STROBE = "1"
return KeyData;
}


void main() {
unsigned int j;
unsigned int Keys = 0;
init_WDT();
init_Ports();
init_SPI();
init_Display();

// Example program:

for (j=0; j<8; j++) { //example set "01234567" on display
ShowDig(j, Num[j], 0);
}
SetupDisplay(1, 7); // Display ON, max brightness

while(1) {
ShowLeds(0); //switch all LEDs off
switch(GetKey()) {
case 1: Keys = 1; break;
case 2: Keys = 2; break;
case 4: Keys = 3; break;
case 8: Keys = 4; break;
case 16: Keys = 5; break;
case 32: Keys = 6; break;
case 64: Keys = 7; break;
case 128: Keys = 8; break;
default: Keys = 0; break;
}

if (Keys) { ShowLed(Keys, GRE_TM1638);}
ShowDig(7, Num[Keys], 1);
}

//end of example program
}

tm1638.rar

Link to post
Share on other sites
  • 9 months later...

Why don't you add hexadecimal entries to your character table?

const unsigned int Num[]=					//Code table of symbols
{
		0x3F,								//0
		0x06,								//1
		0x5B,								//2
		0x4F,								//3
		0x66,								//4
		0x6D,								//5
		0x7D,								//6
		0x07,								//7
		0x7F,								//8
		0x6F,								//9
		0x77,								//A
		0x7C,								//b
		0x39,								//C
		0x5E,								//d
		0x79,								//E
		0x71,								//F
		0x40,								//Minus
		0x63,								//Degree
		0x00,								//Blank
		0x3E								//"U"
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...