Skip to main content

Experiment No. 2 : Push Button and Seven Segment Display Interface

In this experiment, we will program the PIC16F628A as an UP/DOWN Decade Counter. The count value will be displayed on a Seven-Segment Display and will be incremented/decremented by two push buttons on the board.

Experimental Setup:
The board has built in interface for a multiplexed 4-digit seven segment display (HS-5461AS2 from will select only one digit by connecting a Digit Select pin to Vcc, as shown in figure below. A black jumper wire is used for this purpose. The seven segments will be driven through PORTB (already wired on the board). Connect Push Buttons (PB3 and PB4) to RA1 and RA0 female headers using jumper wires.

We will use in-built 'Button Library' to detect push button press.

Here is the complete C program written for mikroC for PIC 2009.
 * Project name:
     UP/DOWN Decimal Counter with Push Button and 7-Segment Interface
 * Copyright:
     (c) Rajendra Bhatt, 2009.
 * Description:
     This code is an example of Seven Segment Display and Push Button interface.
     A decimal counter value will be displayed on the seven segment display.
     The value of the counter can be incremented or decremented through push
 * Test configuration:
     MCU:             PIC16F628A
     The two push buttons are connected to RA0(Increment) and RA1(Decrement)
     and the seven segment display connected to PORTB (Segment a to PB.0,
     Segment b to PB.1 and so on)
//-------------- Function to Return mask for common cathode 7-seg. display
unsigned short mask(unsigned short num) {
  switch (num) {
    case 0 : return 0x3F;
    case 1 : return 0x06;
    case 2 : return 0x5B;
    case 3 : return 0x4F;
    case 4 : return 0x66;
    case 5 : return 0x6D;
    case 6 : return 0x7D;
    case 7 : return 0x07;
    case 8 : return 0x7F;
    case 9 : return 0x6F;
  } //case end

unsigned int   digit;      // To Hold Decimal Value
unsigned short number;     // To Hold Equivalent Seven Segment Value

void main() {
  CMCON  |= 7;         // Disable Comparators
  TRISB = 0x00;        // Set PORTB direction to be output
  PORTB = 0x00;        // Turn OFF LEDs on PORTB
  TRISA0_bit = 1;      // PA.0 Input for Increment
  TRISA1_bit = 1;      // PA.1 Input for Decrement

  digit   =    0;  // Initial Value of Counter
  number  = mask(digit)   ;
  PORTB   = number;
  do {
    if (Button(&PORTA, 0, 1, 0)) {  // Detect logical one to zero
      digit ++;                 // Increase Counter
      number  = mask(digit)   ;
      PORTB   = number;

    if (Button(&PORTA, 1, 1, 0)) {  // Detect logical one to zero
      Delay_ms(300) ;
      digit = digit-1;                 // Decrease Counter
      number  = mask(digit)   ;
      PORTB   = number;                // Update flag
  } while(1);                             // endless loop

Experiment Output Video:


  1. i am very interest in this project pls send circuit diagram and source code in my mail id

  2. Hello Mr.Raj.I'm quite interested in your project.But I'm using MPLAB.Can I do it in there?If I can then please send me the circuit diagram and the source code.Thanks...
    My mail id =

  3. I am using mikroC for writing the software for PIC. It is an high level (C) compiler. You can use MPLAB but then you have to write in assembly language. I don't have specific circuit diagrams, all the projects I am doing are on my board. The circuit diagrams of my development board is here.

  4. interesting to your..i want two decimal digit..but my problem is what codes can i use and condition..can you help me..TIA

  5. hi.. im interesting to your project..i want two decimel digit to is possible?..i have codes..but the problem is nothing display or output in the seven you have any idea..TIA

  6. Do you know how to code for pic18f4520?

  7. Hi, I am new in learning C program for PIC. Do you mind to explain the code:
    " unsigned short mask( unsigned short num)"
    thank you


Post a Comment

Popular posts from this blog

Contact less tachometer using PIC16F628A

Tachometer is a device that gives you the information about the rotational speed of any shaft or disc. It usually measures the speed in revolutions per minute (RPM). Today we are going to make a simple tachometer that could measure the rotation speed of a disk without making any physical contact (that's why it is contact less) with the rotating object. The range of this tachometer is 0 - 9999 RPM and displays the RPM on a multiplexed 4-digit seven-segment display. Of course, we are going to do this project on our usual PIC16F628A development board.

Infrared sensor
Contact-less measurement of RPM will be achieved through an IR sensor. An IR diode will send a beam of infrared towards the rotating disc, and any reflected pulse will be received by a photo diode. The resistance of a photo diode drops drastically when exposed to infrared. An infrared is reflected by a white surface and absorbed by the dark ones. The test disc for this project is shown below. You can see the …

PIC16F628A Development Board

The development board we are going to make for our experimental microcontroller PIC16F628A will look like this. Here are the features it is going to have:
Access to all I/O pins through female header pins4 Push Buttons for Input4 LEDs for OutputAn LCD Interface PortA 4-digit Seven-Segment Display InterfaceLCD Backlight Switch and Contrast AdjustmentICSP Programming (Very Important)