Seven segment display 4 digit For CSS Compiler

Summary of Seven segment display 4 digit For CSS Compiler


This article details a C program for the PIC 16F877 microcontroller designed to create a 4-digit BCD counter with up/down functionality. The system utilizes two switches to increment or decrement a count from 0 to 9999, displaying the result on a common cathode 7-segment display via multiplexing logic.

Parts used in PIC 16F877 Counter Project:

  • PIC 16F877 Microcontroller
  • Common Cathode 7-Segment Display
  • Switch UP (connected to Pin A0)
  • Switch DOWN (connected to Pin A1)
  • Port C Pins (C0, C1, C2, C3) for digit selection
  • Port D for segment data output
  • 4MHz Clock Source
#include <16F877.h>
#device adc=8
#fuses NOWDT,HS, NOPUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=4000000)

#define sw_up    pin_a0  // SWITCH UP
#define sw_down  pin_a1  // SWITCH DOWN

INT16    COUNT;
int8     A,B,C,D;
//int8    segment[10] ={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//common anode
  int8  num[10] ={ 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x27,0x7F,0x6F};//common cathode                         0    1    2    3    4    5    6    7    8      9         
                    //   0   1    2    3    4    5   6     7    8     9   
VOID HEX_BCD(INT16 VALUE);

void main(){
  setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);

    OUTPUT_LOW(PIN_C0);
    OUTPUT_LOW(PIN_C1);
    OUTPUT_LOW(PIN_C2);
    OUTPUT_LOW(PIN_C3);        
    COUNT=0; 
WHILE(true){

  IF(!INPUT(SW_up)){IF(COUNT++>9999)COUNT=0;delay_ms(150);}
  IF(!INPUT(SW_down)){IF(COUNT --<1)COUNT=9999;delay_ms(150);} 

        HEX_BCD(COUNT);

        OUTPUT_D(NUM[D]);   // nuoy
        OUTPUT_HIGH(PIN_C0);
        DELAY_MS(5);
        OUTPUT_LOW(PIN_C0);

        OUTPUT_D(NUM[C]);   // sib        
        OUTPUT_HIGH(PIN_C1);
        DELAY_MS(5);
        OUTPUT_LOW(PIN_C1);

        OUTPUT_D(NUM[B]);   // Roy        
        OUTPUT_HIGH(PIN_C2);
        DELAY_MS(5);
        OUTPUT_LOW(PIN_C2);

        OUTPUT_D(NUM[A]);   // PHUN        
        OUTPUT_HIGH(PIN_C3);
        DELAY_MS(5);
        OUTPUT_LOW(PIN_C3);                
        DELAY_MS(100);
     }        
 }

void HEX_BCD(INT16 VALUE){
   A=0;B=0;C=0;D=0;
  while(value>=1000){A++;value-=1000;}
  while(value>=100){B++;value-=100;}
  while(value>=10){C++;value-=10;}
  D=value;
 }

 

Quick Solutions to Questions related to PIC 16F877 Counter Project:

  • What is the primary function of this code?
    The code implements a 4-digit BCD counter that counts from 0 to 9999 using a PIC 16F877 microcontroller.
  • How does the user increase the count value?
    The count increases when the switch connected to pin_a0 (sw_up) is pressed, provided the current value is less than 9999.
  • What happens if the count reaches 9999 and the up switch is pressed again?
    The count resets to 0 immediately after reaching 9999.
  • How is the down count handled when the value is 0?
    If the count is at 1 and the down switch is pressed, it wraps around to 9999.
  • Which type of 7-segment display is utilized in this project?
    A common cathode 7-segment display is used, as indicated by the specific hex values in the num array.
  • What is the purpose of the HEX_BCD function?
    This function converts a 16-bit integer value into four separate digits representing thousands, hundreds, tens, and units.
  • How are the display digits refreshed continuously?
    The main loop cycles through each digit rapidly by setting the corresponding Port C pin high, sending data to Port D, and then turning the pin low.
  • What delay is applied to prevent switch bounce?
    A delay of 150 milliseconds is implemented after detecting a switch press to debounce the input.

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