4-Digit 7-Segment display with 74HC595 shift register

Summary of 4-Digit 7-Segment display with 74HC595 shift register


This article details the construction of a digital up/down counter using a PIC16F877A microcontroller, a 4-digit multiplexed common anode 7-segment display, and a 74HC595 shift register. The system utilizes two push buttons to increment or decrement the count from 0 to 9999. The provided CCS C code handles data shifting, latch control, and segment mapping to drive the display dynamically.

Parts used in the Digital Up/Down Counter:

  • PIC16F877A microcontroller
  • 74HC595 shift register
  • 4-digit multiplexed 7-segment display (common anode)
  • Two push buttons
  • CCS C compiler

There are many topics in this blog talking about the 7-segment display and how to interface it with different types of PIC microcontrollers. One of these topics shows how to interface PIC16F877A with a multiplexed 4-digit 7-segment display with the shift register 74HC164N.4-Digit 7-Segment display with 74HC595 shift register

There are many topics in this blog talking about the 7-segment display and how to interface it with different types of PIC microcontrollers. One of these topics shows how to interface PIC16F877A with a multiplexed 4-digit 7-segment display with the shift register 74HC164N.
In this topic we are going to see how to make a digital up/down counter using multiplexed 7-segment display with 74HC595 shift register and PIC16F877A microcontroller.
From the 74HC595 datasheet this shift register is a high speed, 8-stage serial shift register with a storage register and 3-state outputs. The registers have separate clocks.
Data is shifted on the positive-going transitions of the shift register clock input (SHCP). The data in each register is transferred to the storage register on a positive-going transition of the storage register clock input (STCP). If both clocks are connected together, the shift register will always be one clock pulse ahead of the storage register.
The following table shows the 74HC595 shift register pin-outs:74hc595_shift_register_pin_outs

7-Segment display with 74HC595 shift register:
The following circuit schematic shows a multiplexed 4 digits connected to the 74HC595 shift register. The type of the 7-segment display used in this example is common anode.
In the circuit there are two push buttons, these buttons are used to increment and decrement the displayed number.

7-Segment display with 74HC595 shift register interfacing with PIC16F877A CCS C code:
Here is the example code I think it is a small and clear code.

// 4-Digit 7-Segment display with 74HC595 interfacing with PIC16F877A CCS C code
// http://ccspicc.blogspot.com/
// [email protected]

#define data_pin PIN_B0
#define clock_pin PIN_B1
#define latch_pin PIN_B2
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8000000)
#use fast_io(B)
#use fast_io(D)

short s;                                   // Used to know buttons position
unsigned int j, digit ;
unsigned long i = 0;
unsigned int seg(unsigned int num) {
  switch (num) {
    case 0 : return 0x80;
    case 1 : return 0xF2;
    case 2 : return 0x48;
    case 3 : return 0x60;
    case 4 : return 0x32;
    case 5 : return 0x24;
    case 6 : return 0x04;
    case 7 : return 0xF0;
    case 8 : return 0;
    case 9 : return 0x20;
    }
}
void write_data(unsigned int number){
  for(j = 0x80; j > 0; j = j >> 1) {
     if(number & j)
       output_high(data_pin);
     else
       output_low(data_pin);
     output_high(clock_pin);
     output_low(clock_pin);
   }
     output_high(latch_pin);
     output_low(latch_pin);
}
void main(){
  port_b_pullups(TRUE);                  // Enable PORTB pull-ups
  output_b(0);                           // PORTB initial state
  set_tris_b(0x18);                      // Configure RB3 & RB4 pins as inputs
  output_d(0);                           // PORTD initial state
  set_tris_d(0);                         // Configure PORTD pins as inputs
  while(TRUE){
    if(input(PIN_B3) && input(PIN_B4))
      s = 1;
    if(s == 1) {
      if(input(PIN_B3) == 0) {
       s = 0;
       i++;
       if(i > 9999)
         i = 0;
      }
      if(input(PIN_B4) == 0) {
       s = 0;
       if(i < 1)
         i = 1;
       i--;
      }
    }
    digit = seg(i % 10);                 // Prepare to display ones
    output_d(0x0F);                      // Turn off all displays
    write_data(digit);
    output_d(0x07);                      // Turn on display for ones
    delay_ms(1);
    digit = seg((i / 10) % 10);          // Prepare to display tens
    output_d(0x0F);                      // Turn off all displays
    write_data(digit);
    output_d(0x0B);                      // Turn on display for tens
    delay_ms(1);
    digit = seg((i / 100) % 10);         // Prepare to display hundreds
    output_d(0x0F);                      // Turn off all displays
    write_data(digit);
    output_d(0x0D);                      // Turn on display for hundreds
    delay_ms(1);
    digit = seg((i / 1000) % 10);        // Prepare to display thousands
    output_d(0x0F);                      // Turn off all displays
    write_data(digit);
    output_d(0x0E);                      // Turn on display for thousands
    delay_ms(1);
  }
}

7-Segment display with 74HC595 shift register interfacing with PIC16F877A video:
The following video from a real hardware circuit for the digital counter.

Source : 4-Digit 7-Segment display with 74HC595 shift register

Quick Solutions to Questions related to Digital Up/Down Counter:

  • What is the purpose of the two push buttons?
    The buttons are used to increment and decrement the displayed number.
  • How does the 74HC595 shift register handle data transfer?
    Data is shifted on positive-going transitions of the shift register clock input, while storage occurs on the storage register clock input transition.
  • What type of 7-segment display is used in this project?
    A common anode 7-segment display is used.
  • Can the counter count down below zero?
    No, if the value is less than 1 during decrementing, it resets to 1.
  • What happens when the count exceeds 9999?
    The counter resets to 0.
  • Which pins on PORTB are configured as inputs for the buttons?
    PIN_B3 and PIN_B4 are configured as inputs.
  • Does the article mention using a 74HC164N for this specific project?
    No, this project uses a 74HC595 shift register, though the blog mentions the 74HC164N in other topics.
  • What programming language is used for the example code?
    The code is written in CCS C.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter