Driving a 4-Digit Seven-Segment Display Module

The TM1637 four-digit seven-segment display module is a 4-digit common anode display (0.36 inches) with a clock point. The driving chip is TM1637, and it uses IIC for control, allowing a microcontroller to control the 4-digit 8-segment display with just 2 signal lines (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 seven-segment display has 8 levels of brightness adjustment

  • The control interface can be 5V or 3.3V

  • IIC drive, requiring only two IO pins to implement the display function

  • Equipped with a clock point, making it 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), where 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 and 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 Seven-Segment 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 timing for driving TM1637 must comply with the IIC bus specifications.

The following diagram shows the instruction data transmission process (timing for reading key data):

Driving a 4-Digit Seven-Segment Display Module

In the above timing diagram, the following points need to be noted:

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

Thus, the functions we encapsulate for writing bytes modify the DIO data only 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. TM1637 data transmission includes an acknowledgment signal (ACK). During the data transmission process, an acknowledgment signal (ACK) is generated inside the chip on the ninth clock pulse of the clock line, pulling the DIO pin low.
Driving a 4-Digit Seven-Segment Display Module

Implementation of Write Command

Based on the above implementation of the IIC write byte function, implement the address auto-increment mode to write data to SRAM. The timing diagram and code sections in the figure below correspond with the same color and shape.

Driving a 4-Digit Seven-Segment Display Module

After the start signal, the first byte is input from DIO to TM1637 as an instruction. After internal decoding, the B7 and B6 bits of this byte are used to distinguish different instructions.

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

Data Command Setting

This instruction is used to set data writing and reading. The B1 and B0 bits cannot be set to 01 or 11, as can be seen from the table above, 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 table above: writing data to the display register.

Address Command Setting

Driving a 4-Digit Seven-Segment Display Module

This instruction 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; at power-up, the address defaults to 00H.

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. Sending data afterwards 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 the binary 1000 1111, where B3=1 indicates the display is on, and B2~B0 of 111 indicates the pulse width, which is the display brightness. From the table, we can conclude that 0x8F represents the highest brightness.

Result Display

This seven-segment display can show the following content: 0123456789AbcdEFHLnPU. The video below cycles through these contents.

Leave a Comment