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 www.futurlec.com).We 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.



Software:
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
     buttons.
 * 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
      Delay_ms(300);
      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:




Comments

  1. i am very interest in this project pls send circuit diagram and source code in my mail id kavi.paradise@gmail.com

    ReplyDelete
  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 = samitbsk6@googlemail.com

    ReplyDelete
  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.
    http://pic16f628a.blogspot.com/2009/09/pic16f628a-development-board-part-1.html

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

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

    ReplyDelete
  6. Do you know how to code for pic18f4520?

    ReplyDelete
  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

    ReplyDelete
  8. hi
    merci deja pour votre très grand travail
    votre programme est très intéressant
    mais comment donner un nombre au depart comme 473 avant de commencer a compter ou decompter
    merci cordialement

    ReplyDelete
  9. I cant find code of Button function. can you please show me

    ReplyDelete

Post a Comment

Popular posts from this blog

Contact less tachometer using PIC16F628A

Introduction 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 ...

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 pins 4 Push Buttons for Input 4 LEDs for Output An LCD Interface Port A 4-digit Seven-Segment Display Interface LCD Backlight Switch and Contrast Adjustment ICSP Programming (Very Important)