Driving a 4-Digit Seven-Segment Display Module

The TM1637 four-digit seven-segment display module is a 4-digit common anode display module (0.36 inches) with a clock point. The driving chip is TM1637, and the driving method is IIC, so only 2 signal lines are needed to control the 4-digit 8-segment display (the brightness of the display can be adjusted in 8 levels).

Module features are as follows:

  • The display device is a 4-digit common anode seven-segment display.

  • The brightness of the seven-segment display can be adjusted in 8 levels.

  • The control interface level can be 5V or 3.3V.

  • IIC drive, only two IO pins are needed to achieve display function.

  • With a clock point, it is very convenient for electronic clock display.

TM1637 Pin Definition

Driving a 4-Digit Seven-Segment Display Module

Hardware Connection

The module has 4 pins (GND, VCC, DIO, CLK), GND is ground, VCC is the power supply, DIO is the data input/output pin, and CLK is the clock signal pin.

Connect the 4-digit seven-segment display module to the STM32 core board using 4 female-to-female Dupont wires, as shown in the figure and table below:

Driving a 4-Digit Seven-Segment Display Module
Four-Digit Display Connection Wire STM32 Core Board
CLK Yellow PA3
DIO Blue PA2
VCC Red 3.3V
GND Black GND

IIC Bus Timing Analysis

Since TM1637 is driven by the IIC bus, the bus timing for driving the TM1637 must meet the IIC bus specifications.

The following figure shows the command data transmission process (reading key data timing):

Driving a 4-Digit Seven-Segment Display Module

In the above timing diagram, the following points should be noted:

  1. The IIC bus requires the validity of the data: when inputting data, when CLK is high, the signal on DIO must remain unchanged; only when the clock signal on CLK is low can the signal on DIO change.

Therefore, the functions we encapsulated for writing bytes all modify the DIO data when the CLK pin is low;

Driving a 4-Digit Seven-Segment Display Module
  1. The start condition for data input is when CLK is high, and DIO changes from high to low;
Driving a 4-Digit Seven-Segment Display Module

The specific code implementation is as follows:

 //IIC start void TM1637_start(void){ CLK_1; DIO_1; delay_us(2); DIO_0;}
  1. The end condition is when CLK is high, and DIO changes from low to high.
Driving a 4-Digit Seven-Segment Display Module

The specific code implementation is as follows:

//IIC stop void TM1637_stop(void){ CLK_0; delay_us(2); DIO_0; delay_us(2); CLK_1; delay_us(2); DIO_1; delay_us(2);}
  1. The TM1637 data transmission includes an acknowledgment signal ACK. During the data transmission, an acknowledgment signal ACK will be generated inside the chip at the ninth clock of the clock line, pulling the DIO pin low.
Driving a 4-Digit Seven-Segment Display Module

Implementation of Writing Commands

Based on the above implementation of the IIC write byte function, implement the address auto-increment mode to write data to SRAM. In the following figure, the timing diagram and code part correspond to each other in color and shape.

Driving a 4-Digit Seven-Segment Display Module

After the start signal, DIO inputs the first byte of TM1637 as a command. After internal decoding, the B7 and B6 bits of this byte are taken to distinguish different commands.

B7 B6 Command
0 1 Data command setting
1 0 Display control command setting
1 1 Address command setting

Data Command Setting

This command is used to set data write and read. The B1 and B0 bits are not allowed to be set to 01 or 11; the above table can be consulted to find B7=0, B6=1.

Driving a 4-Digit Seven-Segment Display Module

For example:

Driving a 4-Digit Seven-Segment Display Module

The command 0x40 corresponds to the first row in the above table: write data to the display register.

Address Command Setting

Driving a 4-Digit Seven-Segment Display Module

This command is used to set the address of the display register. If the address is set to 06H or higher, the data will be ignored until a valid address is set; the default address is 00H at power-up.

For example:

Driving a 4-Digit Seven-Segment Display Module

The command 0xC0 is the address command, corresponding to the display address 00H, which is the address of the first seven-segment display. Subsequent data sent can set the display content for the first seven-segment display.

Display Control

Driving a 4-Digit Seven-Segment Display Module

For example:

Driving a 4-Digit Seven-Segment Display Module

The command 0x8F corresponds to binary 1000 1111, where B3=1 indicates the display is on, and B2~B0 of 111 indicates the pulse width, i.e., display brightness. From the table, we can see that 0x8F represents the highest brightness.

Result Display

This type of seven-segment display can show the following content: 0123456789AbcdEFHLnPU. The following video shows these contents in a loop.

Data Acquisition

Reply “Four-Digit Display” in the WeChat official account background to download the corresponding project source code for this article.

Driving a 4-Digit Seven-Segment Display Module

Driving a 4-Digit Seven-Segment Display Module

Leave a Comment