CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to

Tm1638 Library

Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ömer Faruk

Joined: 15 Nov 2018
Posts: 42
Location: Çanakkale

View user's profile Send private message Send e-mail

Tm1638 Library
PostPosted: Wed Jan 02, 2019 10:18 am     Reply with quote

Hello everyone.
İ searched tm1638 Library on the internet but i couldn't find it. Could someone share it with me.

Joined: 01 Jul 2010
Posts: 8944
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jan 02, 2019 10:46 am     Reply with quote

I'm guessing ??

It'd really help if you posted a link to whatever a tm1638 actually is...
Quite often posters mean a 'module' and not a 'chip', so it's important to provide details !

There's LOTS of Arduino libraries for this module BUT you need to select the correct one ! Some modules have bicolor LEDs, some don't. At $4 a board it's a good deal for 8 7sd, 8 led, 8pb BUT takes a LOT of current if all LEDs are on.

Converting the Arduino library to a CCS driver is easy, maybe 1/2 night project. Quicker if you understand Arduino C !

Joined: 11 Mar 2010
Posts: 18973

View user's profile Send private message

PostPosted: Thu Jan 03, 2019 5:10 am     Reply with quote

Though this is not complete, I've just put together a basic set of routines
to write data to the chip and read the key values. Lots of comments inline.
Some settings will depend on whether the matrix is wired as
common cathode or common anode.
Driving the chip itself is very basic. Just SPI:

//Your setup fuses, clock etc..

#use spi(MODE=3, SPI1, BAUD=2MHz, STREAM=TM1638)
//adjust this to use different pins if you want software I2C
//chip supports 2.5MHz max SPI.

#define DISPLAYON 0x84
#define AUTOINC 0x40 //auto increment on write
#define READKEY 0x42 //read key values
#define STB PIN_xx //set to the STB pin
#define ADDR_START 0xC0 //starting address

const unsigned int8 digitToSegment[] = {
  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

void init_MS(void)
   output_high(STB_PIN); //ensure chip is deselected

void display_block(int8 * disp_data, int8 brightness)
    int8 icnt;
    int8 dummy;
    //write block of display data to chip
    //all sixteen bytes sent
    //now need to set to send command to autowrite data
    spi_xfer(TM1638, AUTOINC, 8);
    spi_xfer(TM1638, ADDRESS_START, 8);
    //set to start at address 0xC0, and auto increment
    for (icnt=0;icnt<16;icnt++)
        dummy=spi_xfer(TM1638,*disp_data, 8); //send byte
        //using the read ensure write will complete each time before
    //Since I wait for the read each time, the STB can be raised immediately.
    //16 bytes sent
    delay_us(1); //minimum 1uSec
    //now command for brightness
    dummy=spi_xfer(TM1638, DISPLAY_ON | (brightness & 7), 8);

void read_keys(int8 * key_data)
    //read the keyboard data from the chip
    //Now command to read data
    spi_xfer(TM1638, READKEY, 8);

    //Now read back keyboard
    for (cnt=0;cnt<4;cnt++)
        *key_data=spi_xfer(TM1638, 0, 8); //clock out dummy byte
    //Now need to switch back to write mode
    dummy=spi_xfer(TM1638, AUTOINC, 8);   

void main(void)
    int8 display_data[16]= {nn,nn.......nn,nn};
    //whatever you want to send to the display
    int8 key_data[4]; //key data returned

    delay_ms(100); //just allow things to settle

    while (TRUE)
        display_block(display_data, 5);
        //Now the display data will have been written to the display
        //Last value here is brightness 0 to 7.
        //Now 'key_data' will contain the key value read back. You
        //need to look at this, decide what you are going to do, and
        //update the data if required.

Now the data values you put into the 'display_data' array are the digits.
'digitToSegment' contains constant values for the standard digits.
So if you put digitToSegment[0] into display_data[0], it'd display '0'
for the first digit.
The key values returned are in reverse bit order.

Edited - just realised I had left 'key_data' being sent to the display routine
was originally going to combine the routines, but decided against this
had updated the routine itself, but not the call.

Last edited by Ttelmah on Thu Jan 03, 2019 10:04 am; edited 1 time in total

Joined: 01 Jul 2010
Posts: 8944
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jan 03, 2019 8:15 am     Reply with quote

Mr. T has given you a GREAT starting point ! Just be sure to read the datasheet for YOUR display module. There are several 'variations' of them so you'll need to setup the 'configuration' as YOUR datasheet says.


Joined: 23 Sep 2016
Posts: 13

View user's profile Send private message

PostPosted: Thu Jun 17, 2021 12:13 am     Reply with quote

I think this may help.

This is the most recent piece of my code, you can control common anode displays with this added feature.

I hope you like it.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group