Using with C

In order to use the library in your program, you will need to add the following include line:

#include <tm1640.h>

When you compile your program, you will need to tell the linker to use libtm1640.so. You can typically do this with the -l option to gcc:

$ gcc -o myprogram myprogram.c -ltm1640

Initialise the display

In order to connect to the display, always need to pass the GPIO pins you wish to use. This is normally GPIO 0 for data, and GPIO 1 for clock:

tm1640_display* display = tm1640_init(1, 0);

You should then verify that there was no error while initialising the display. tm1640_init() returns NULL on error:

if (display == NULL) {
        fprintf(stderr, "Error initialising display!\n");
        return EXIT_FAILURE;
}

You must then switch on the display. You must specify a brightness level between 1 and 7. To set the display to maximum brightness:

tm1640_displayOn(display, 7);

Clear the display

If you turn on the display, it may have the contents of whatever was in the TM1640 IC’s memory last time it was used. You can clear the display with:

tm1640_displayClear(display);

Turn off the display

In order to turn off the display, and keep the contents of the display in memory, you can issue the off command:

tm1640_displayOff(display);

Write to the display

You can write up to 16 characters to the display at once. If you write less than 16 characters, it will leave the cells that you have not specified in the state that they currently are in.

For example:

char* s = "1234567890123456";
int result = tm1640_displayWrite(display, 0, s, strlen(s), INVERT_MODE_NONE);

You should verify that the display was written to correctly:

if (result != 0) {
        fprintf(stderr, "write error %d\n", result);
        return EXIT_FAILURE;
}

Writing to part of the display at an offset

You can update parts of the screen instead of the entire display at once. This allows you to push updates partial updates to the screen from different parts of your program, or improve the speed of updates.

For example, you could write the last 4 characters of the display (offset 12) with:

char* s = "test";
int result = tm1640_displayWrite(display, 12, s, strlen(s), INVERT_MODE_NONE);

Vertically-inverted mode

If you are viewing the display in a mirror, you can invert the display vertically, using the INVERT_MODE_VERTICAL parameter:

char* s = "1234567890123456";
int result = tm1640_displayWrite(display, 0, s, strlen(s), INVERT_MODE_VERTICAL);

Library Reference

Defines

DIN_PIN

Default data GPIO pin to use.

Used by the standalone tm1640 application in order to set which display to use.

SCLK_PIN

Default clock GPIO pin to use.

Used by the standalone tm1640 application in order to set which display to use.

INVERT_MODE_NONE

Used by tm1640_displayWrite

Sets an inversion mode of “none”, that the segments will be output in the way they were input.

INVERT_MODE_VERTICAL

Used by tm1640_displayWrite

Sets an inversion mode of “vertical”, that the segments will be flipped vertically when output on the display.

Functions

tm1640_display *tm1640_init(int clockPin, int dataPin)

Initialises the display.

Return

NULL if wiringPiSetup() fails (permission error)

pointer to tm1640_display on successful initialisation.

Parameters
  • clockPin -

    WiringPi pin identifier to use for clock (SCLK)

  • dataPin -

    WiringPi pin identifier to use for data (DIN)

void tm1640_destroy(tm1640_display *display)

Destroys (frees) the structure associated with the connection to the TM1640.

Parameters
  • display -

    TM1640 display connection to dispose of.

char tm1640_invertVertical(char input)

Flips 7-segment characters vertically, for display in a mirror.

Return
Bitmask of segments flipped vertically.
Parameters
  • input -

    Bitmask of segments to flip.

int tm1640_displayWrite(tm1640_display *display, int offset, const char *string, char length, int invertMode)

displayWrite

Return

-EINVAL if invertMode is invalid

-EINVAL if offset + length > 16

0 on success.

Parameters
  • display -

    TM1640 display to write to

  • offset -

    offset on the display to start writing from

  • string -

    string to write to the display

  • length -

    length of the string to write to the display

  • invertMode -

    invert mode to apply to text written to the display

char tm1640_ascii_to_7segment(char ascii)

Converts an ASCII character into 7 segment binary form for display.

Return

0 if there is no translation available.

bitmask of segments that represents the input character.

Parameters
  • ascii -

    Input ASCII byte to translate.

void tm1640_displayClear(tm1640_display *display)

Clears the display

Parameters
  • display -

    TM1640 display to clear

void tm1640_displayOn(tm1640_display *display, char brightness)

Turns on the display and sets the brightness level

Parameters
  • display -

    TM1640 display to set brightness of

  • brightness -

    Brightness to set (1 is lowest, 7 is highest)

void tm1640_displayOff(tm1640_display *display)

Turns off the display preserving display data.

Parameters
  • display -

    TM1640 display to turn off

void tm1640_send(tm1640_display *display, char cmd, char *data, int len)

Sends a cmd followed by len amount of data. Includes delay from wiringPi.

Bitbanging the output pins too fast creates unpredictable results.

Parameters
  • display -

    TM1640 display structure to use for this operation.

  • cmd -

    The command

  • data -

    Pointer to data that should be appended, or NULL if no data is to be passed.

  • len -

    Length of data.

void tm1640_sendRaw(tm1640_display *display, char out)

Shifts out the byte on the port.

Implementing this with WiringPi directly is too fast for the IC.

Parameters
  • display -

    TM1640 display structure to use this for this operation.

  • out -

    Byte to send

void tm1640_sendCmd(tm1640_display *display, char cmd)

Send a single byte command

Parameters
  • display -

    TM1640 display structure to use for this operation.

  • cmd -

    Command code to send

struct tm1640_display
#include <tm1640.h>

Structure that defines a connection to a TM1640 IC.

You should not manipulate this structure directly, and always create new instances of this with tm1640_init

Public Members

int clockPin

WiringPi GPIO pin for display clock (SCLK).

int dataPin

WiringPi GPIO pin for display data (DIN).