Table of Contents
Overview
In this guide, we will learn how to use the TM1637 4-digit 7-segment LED display with the TMS320. These screens are cheaper and more suitable for displaying sensor data, time, stopwatch, random numbers, etc. They are similar to other 4-digit 7-segment displays, but have a built-in TM1637 LED driver IC. This eliminates additional wiring and with just 2 wires the display can be controlled.
In this guide, I have explained three examples of TM1637 TMS320. In the first example, we will look at the main functions of the TM1637 display library and display some random numbers. In the second example, we will display the ADC values on TMS1637. In the third example, we will use the lm35 sensor to display the real-time temperature on the TM1637 display.
Bill of Materials
For this tutorial, we will need the following components. You can buy all of these components on Amazon.
S.No | COMPONENTS | DESCRIPTION | QUANTITY | Link |
1 | LAUNCHXL-F28379D | LAUNCHXL-F28379D | 1 | LAUNCHXL-F28379D |
2 | TM1637 | 4-Digit 7-Segment Display | 1 | 4-Digit 7-Segment Display |
3 | Potentiometer | Potentiometer | 1 | 10K Potentiometer Module |
4 | Breadboard | Breadboard | 1 | Breadboard |
5 | connection wires | Jumper Wires | 40 | Jumper Wires |
TM1637 4-digit 7-segment LED display
TM1637 4-Digit 7-Segment Display Specifications
Operating voltage | 3.3 – 5 V |
Current draw | 80 mA |
Luminance levels | 8 |
Display dimensions | 30 x 14 mm (0.36″ digits) |
Overall dimensions | 42 x 24 x 12 mm |
Hole dimensions | 38 x 20, ⌀ 2.2 mm |
Operating temperature | -10 – 80 °C |
TM1637 Module Pinout
There is a 4-pin right-angle male header on the module for making connections.
CLK is a clock input pin. Connect to any digital pin on TMS320.
DIO is a Data I/O pin. Connect to any digital pin on TMS320.
VCC pin supplies power to the module. Connect it to the 3.3V to 5V power supply.
GND is a ground pin.
TM1637 4-digit 7-segment LED display
The TM1637 module includes four 0.36-inch 7-segment displays. The module has a “colon” in the centre, which is used to create clocks or time-based projects. On the back of the screen is an inexpensive serial LED driver from Titan MicroElectronics called the TM1637. The TM1637 supports many functions, including power on/off and LED brightness control, as well as access to each of the segments.
The module operates from 3.3V to 5V with a current consumption of 80mA. We can connect TM1637 to TMS320 or any other microcontroller using two data pins. There are several TM1637 libraries for TMS320 that take the complexity out and make it easy to interact with the display.
Connection with TMS320
The connection of the TM1637 4-digit 7-segment LED display as follows:
TM1637 | TMS320 LAUNCHXL |
Vcc | 5V |
GNG | GND |
DIO | GPIO32 |
CLK | GPIO67 |
The ADC connection is as follows:
ADC Pin | TMS320 LAUNCHXL |
GND | GND |
+Ve | 5V |
Signal | ADCINA0(30) |
Final code for displaying the number in TM1637
#include "F28x_Project.h"
#include "device.h"
#include "driverlib.h"
#define clk 32
#define dio 67
const char segmentMap[] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, // 0-7
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, // 8-9, A-F
0x00
};
void gpio_init();
void TM1637_ClkHigh(void);
void TM1637_ClkLow(void);
void TM1637_DataHigh(void);
void TM1637_DataLow(void);
void TM1637_Start(void);
void TM1637_Stop(void);
void TM1637_ReadResult(void);
void TM1637_WriteByte(unsigned char b);
void TM1637_DelayUsec(unsigned int i);
void TM1637_SetBrightness(char brightness);
void TM1637_DisplayDecimal(int v, int displaySeparator);
void TM1637_Init(void);
void TM1637_Demo(void);
volatile uint8_t displayColon = 0;
int i;
void main()
{
Device_init();
Device_initGPIO();
gpio_init();
TM1637_SetBrightness(7);
while(1)
{
int var;
for (var = 0; var < 9999; var++)
{
displayColon = !displayColon;
TM1637_DisplayDecimal(var, displayColon);
DEVICE_DELAY_US(1000000);
}
}
}
void gpio_init()
{
EALLOW;
GPIO_setPinConfig(GPIO_32_GPIO32); // for clock input of TM1637
GPIO_setDirectionMode(clk, GPIO_DIR_MODE_OUT);
GPIO_setPadConfig(clk, GPIO_PIN_TYPE_STD);
GPIO_setMasterCore(clk, GPIO_CORE_CPU1);
GPIO_setPinConfig(GPIO_67_GPIO67); // for DIO input of TM1637
GPIO_setDirectionMode(dio, GPIO_DIR_MODE_OUT);
GPIO_setPadConfig(dio, GPIO_PIN_TYPE_STD);
GPIO_setMasterCore(dio, GPIO_CORE_CPU1);
EDIS;
}
void TM1637_ClkHigh(void)
{
GPIO_writePin(clk, 1);
}
void TM1637_ClkLow(void)
{
GPIO_writePin(clk, 0);
}
void TM1637_DataHigh(void)
{
GPIO_writePin(dio, 1);
}
void TM1637_DataLow(void)
{
GPIO_writePin(dio, 0);
}
void TM1637_SetBrightness(char brightness)
{
TM1637_Start();
TM1637_WriteByte(0x87 + brightness);
TM1637_ReadResult();
TM1637_Stop();
}
void TM1637_DisplayDecimal(int v, int displaySeparator)
{
unsigned char digitArr[4];
int i;
for ( i = 0; i < 4; ++i) {
digitArr[i] = segmentMap[v % 10];
if (i == 2 && displaySeparator) {
digitArr[i] |= 1 << 7;
}
v /= 10;
}
TM1637_Start();
TM1637_WriteByte(0x40);
TM1637_ReadResult();
TM1637_Stop();
TM1637_Start();
TM1637_WriteByte(0xc0);
TM1637_ReadResult();
int cc;
for (cc = 0; cc < 4; ++cc) {
TM1637_WriteByte(digitArr[3 - cc]);
TM1637_ReadResult();
}
TM1637_Stop();
}
void TM1637_Demo(void)
{
uint8_t i = 0;
TM1637_Init();
TM1637_SetBrightness(8);
while(1){
TM1637_DisplayDecimal(i++, 0);
}
}
void TM1637_Init(void)
{
gpio_init();
TM1637_SetBrightness(8);
}
void TM1637_Start(void)
{
TM1637_ClkHigh();
TM1637_DataHigh();
TM1637_DelayUsec(2);
TM1637_DataLow();
}
void TM1637_Stop(void)
{
TM1637_ClkLow();
TM1637_DelayUsec(2);
TM1637_DataLow();
TM1637_DelayUsec(2);
TM1637_ClkHigh();
TM1637_DelayUsec(2);
TM1637_DataHigh();
}
void TM1637_ReadResult(void)
{
TM1637_ClkLow();
TM1637_DelayUsec(5);
TM1637_ClkHigh();
TM1637_DelayUsec(2);
TM1637_ClkLow();
}
void TM1637_WriteByte(unsigned char b)
{
int i;
for (i = 0; i < 8; ++i) {
TM1637_ClkLow();
if (b & 0x01) {
TM1637_DataHigh();
}
else {
TM1637_DataLow();
}
TM1637_DelayUsec(3);
b >>= 1;
TM1637_ClkHigh();
TM1637_DelayUsec(3);
}
}
void TM1637_DelayUsec(unsigned int i)
{
for (; i>0; i--) {
int j;
for (j = 0; j < 500; ++j) {
// __NOP();
}
}
}
Final code to display the ADC values in TM1637
#include "F28x_Project.h"
#include "device.h"
#include "driverlib.h"
#define EX_ADC_RESOLUTION 12
#define clk 32
#define dio 67
const char segmentMap[] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, // 0-7
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, // 8-9, A-F
0x00
};
void ConfigADC(uint32_t ADC_BASE);
void initADC_SOC(void);
void gpio_init();
void TM1637_ClkHigh(void);
void TM1637_ClkLow(void);
void TM1637_DataHigh(void);
void TM1637_DataLow(void);
void TM1637_Start(void);
void TM1637_Stop(void);
void TM1637_ReadResult(void);
void TM1637_WriteByte(unsigned char b);
void TM1637_DelayUsec(unsigned int i);
void TM1637_SetBrightness(char brightness);
void TM1637_DisplayDecimal(uint16_t v, int displaySeparator);
void TM1637_Init(void);
void TM1637_Demo(void);
volatile uint8_t displayColon = 0;
int i;
uint16_t Adc_Result_1,Adc_Result_2,Adc_Result_3;
void main()
{
Device_init();
Device_initGPIO();
Interrupt_initModule();
Interrupt_initVectorTable();
gpio_init();
TM1637_SetBrightness(7);
ConfigADC(ADCA_BASE);
initADC_SOC();
while(1)
{
// Convert, wait for completion, and store results
ADC_forceSOC(ADCA_BASE, ADC_SOC_NUMBER0);
// Wait for ADCA to complete, then acknowledge flag
while(ADC_getInterruptStatus(ADCA_BASE, ADC_INT_NUMBER1) == false)
{
}
ADC_clearInterruptStatus(ADCA_BASE, ADC_INT_NUMBER1);
//
// Store results
//
Adc_Result_1 = ADC_readResult(ADCARESULT_BASE, ADC_SOC_NUMBER0);
// int var;
// for (var = 0; var < 9999; var++)
// {
displayColon = !displayColon;
TM1637_DisplayDecimal(Adc_Result_1, 0);
DEVICE_DELAY_US(10000);
// }
}
}
void gpio_init()
{
EALLOW;
GPIO_setPinConfig(GPIO_32_GPIO32); // for clock input of TM1637
GPIO_setDirectionMode(clk, GPIO_DIR_MODE_OUT);
GPIO_setPadConfig(clk, GPIO_PIN_TYPE_STD);
GPIO_setMasterCore(clk, GPIO_CORE_CPU1);
GPIO_setPinConfig(GPIO_67_GPIO67); // for DIO input of TM1637
GPIO_setDirectionMode(dio, GPIO_DIR_MODE_OUT);
GPIO_setPadConfig(dio, GPIO_PIN_TYPE_STD);
GPIO_setMasterCore(dio, GPIO_CORE_CPU1);
EDIS;
}
void TM1637_ClkHigh(void)
{
GPIO_writePin(clk, 1);
}
void TM1637_ClkLow(void)
{
GPIO_writePin(clk, 0);
}
void TM1637_DataHigh(void)
{
GPIO_writePin(dio, 1);
}
void TM1637_DataLow(void)
{
GPIO_writePin(dio, 0);
}
void TM1637_SetBrightness(char brightness)
{
TM1637_Start();
TM1637_WriteByte(0x87 + brightness);
TM1637_ReadResult();
TM1637_Stop();
}
void TM1637_DisplayDecimal(uint16_t v, int displaySeparator)
{
unsigned char digitArr[4];
int i;
for ( i = 0; i < 4; ++i) {
digitArr[i] = segmentMap[v % 10];
if (i == 2 && displaySeparator) {
digitArr[i] |= 1 << 7;
}
v /= 10;
}
TM1637_Start();
TM1637_WriteByte(0x40);
TM1637_ReadResult();
TM1637_Stop();
TM1637_Start();
TM1637_WriteByte(0xc0);
TM1637_ReadResult();
int cc;
for (cc = 0; cc < 4; ++cc) {
TM1637_WriteByte(digitArr[3 - cc]);
TM1637_ReadResult();
}
TM1637_Stop();
}
void TM1637_Demo(void)
{
uint8_t i = 0;
TM1637_Init();
TM1637_SetBrightness(8);
while(1){
TM1637_DisplayDecimal(i++, 0);
}
}
void TM1637_Init(void)
{
gpio_init();
TM1637_SetBrightness(8);
}
void TM1637_Start(void)
{
TM1637_ClkHigh();
TM1637_DataHigh();
TM1637_DelayUsec(2);
TM1637_DataLow();
}
void TM1637_Stop(void)
{
TM1637_ClkLow();
TM1637_DelayUsec(2);
TM1637_DataLow();
TM1637_DelayUsec(2);
TM1637_ClkHigh();
TM1637_DelayUsec(2);
TM1637_DataHigh();
}
void TM1637_ReadResult(void)
{
TM1637_ClkLow();
TM1637_DelayUsec(5);
TM1637_ClkHigh();
TM1637_DelayUsec(2);
TM1637_ClkLow();
}
void TM1637_WriteByte(unsigned char b)
{
int i;
for (i = 0; i < 8; ++i) {
TM1637_ClkLow();
if (b & 0x01) {
TM1637_DataHigh();
}
else {
TM1637_DataLow();
}
TM1637_DelayUsec(3);
b >>= 1;
TM1637_ClkHigh();
TM1637_DelayUsec(3);
}
}
void TM1637_DelayUsec(unsigned int i)
{
for (; i>0; i--) {
int j;
for (j = 0; j < 500; ++j) {
// __NOP();
}
}
}
void ConfigADC(uint32_t ADC_BASE)
{
EALLOW;
ADC_setPrescaler(ADCA_BASE, ADC_CLK_DIV_4_0);
#if(EX_ADC_RESOLUTION == 12)
{
ADC_setMode(ADC_BASE, ADC_RESOLUTION_12BIT, ADC_MODE_SINGLE_ENDED);
}
#elif(EX_ADC_RESOLUTION == 16)
{
ADC_setMode(ADCA_BASE, ADC_RESOLUTION_16BIT, ADC_MODE_DIFFERENTIAL);
}
#endif
ADC_setInterruptPulseMode(ADC_BASE, ADC_PULSE_END_OF_CONV);
ADC_enableConverter(ADC_BASE);
DEVICE_DELAY_US(1000);
EDIS;
}
void initADC_SOC(void)
{
#if(EX_ADC_RESOLUTION == 12)
{
ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER0, ADC_TRIGGER_SW_ONLY, ADC_CH_ADCIN0, 15);
ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER0, ADC_TRIGGER_EPWM1_SOCA, ADC_CH_ADCIN0, 15);
}
#elif(EX_ADC_RESOLUTION == 16)
{
ADC_setupSOC(ADCA_BASE, ADC_SOC_NUMBER0, ADC_TRIGGER_SW_ONLY, ADC_CH_ADCIN0,64);
}
#endif
ADC_setInterruptSource(ADCA_BASE, ADC_INT_NUMBER1, ADC_SOC_NUMBER0);
ADC_enableInterrupt(ADCA_BASE, ADC_INT_NUMBER1);
ADC_clearInterruptStatus(ADCA_BASE, ADC_INT_NUMBER1);
}