Sunday, September 13, 2009

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


Microcontroller-based Embedded Systems Design