Using analog serial port to drive74HC164, there is always a slight glow in segments that should not be lit, commonly referred to as ghosting, which is particularly noticeable at night. However, using serial port driving yields much better results; when external light is dim, ghosting is slightly visible but does not significantly affect viewing. This time, we use serial mode 0 to drive74HC164, sending data via timer interrupts to achieve dynamic display of an 8-digit 7-segment display, with the scan code output from the lower 3 bits of P2, driven by74HC138 for common cathode 7-segment display.
//ch164_138.c // Download successful The segment that should not light up is slightly visible in dim light, while the other segment does not exhibit this phenomenon, // that is, ghosting occurs, but the effect is far better than analog serial port driving // Scan code is in phase Field code is in phase //-----------------------------------------------------------------// Name: Serial Control Serial-to-Parallel Converter 74HC164 Driving Two 4-Digit Common Cathode Integrated 7-Segment Displays//-----------------------------------------------------------------// Description: In this example, two 4-digit integrated 7-segment displays are driven by one serial-to-parallel converter 74HC164 + 74HC138 // to display two groups of 8-digit numbers, with the segment code driving only occupying 3 pins of the microcontroller.////-----------------------------------------------------------------#include <reg51.h> // Include 51 microcontroller header file#include <intrins.h> #define INT8U unsigned char // Macro definition, declare variable#define INT16U unsigned int // Macro definition, declare variable#define SCAN P2 // Input port P2 of 74HC138 // Common cathode 7-segment display digit code table for 0~9, off code INT8U SEG_CODE[] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92, 0x82,0xF8,0x80,0x90,0xFF };// Common cathode 7-segment display digit code table for 0~9, off in reverse order, hardware forward order A~H corresponds to D7~D0 code INT8U Nixu_SEG_CODE[] = { 0x03,0x9f,0x25,0x0d,0x99,0x49, 0x41,0x1f,0x01,0x09,0xFF }; // Define the two groups of data to be displayedcode INT8U tab[2][8]={{1,2,3,4,5,6,7,8}, // First screen display content {1,7,10,0,4,10,2,0}}; // Second screen display content//74HC164 pin definitionssbit RST = P3^2; // Reset 164_Pin_9//P30/RXD--164_Pin1_2DAT //P31/TXD--164_Pin8CLK//-----------------------------------------------------------------// Main program//-----------------------------------------------------------------void main(void){ SCON= 0x00; // Serial port mode 0 TI = 1; // Allow sending TMOD = 0x00; // T0 mode 0 (13-bit), timer TH0 = (8192 - 2000) >> 5; // Timer 2ms TL0 = (8192 - 2000) & 0x1f; //TH0 = (8192 - 2000) / 32; //TL0 = (8192 - 2000) % 32; TR0 = 1; // Start T0 IE = 0x82; // Allow T0 interrupt while(1); // Infinite loop} //-----------------------------------------------------------------// T0 interrupt function//-----------------------------------------------------------------void LED_Flash(void) interrupt 1{ static INT8U j=0,k=0,cnt = 0; TH0 = (8192 - 2000) >> 5; // Timer 2ms TL0 = (8192 - 2000) & 0x1f; RST = 0; // Reset both 74HC164, output is 0 RST = 1; // Pull high reset pin, allow 74HC164 output SCAN = k; // First send scan code //SBUF = ~SEG_CODE[tab[j][k]]; // Then send segment code SBUF = ~Nixu_SEG_CODE[tab[j][k]]; // Serial port sends low bit first, so send reverse code while(TI == 0); TI = 0; if(++k == 8) // Display next digit { k = 0; if(++cnt == 200) // Refresh display 200 times on one screen { cnt = 0; if(++j == 2) // Display next screen j=0; } }}
It is important to note that serial port transmission sends the low bit first, so for the same hardware, the data must be changed, meaning the data code must be reversed.
The images were taken at night, showing that the effect of driving the 74HC164 via serial communication is much better.