This post shows how to interface PIC16F887 microcontroller with 7-segment display in order to build a simple 4-digit counter which counts from 0 to 9999. A push button connected to the PIC16F887 MCU is used to increment the displayed number.
The compiler used in this project is the MikroElektronika mikroC PRO for PIC.
There are two types of the seven-segment displays: common anode and common cathode.
In the common anode type all the 7 LED anode terminals are connected together whereas in the common cathode all cathode terminals are connected together.
The common terminal is connected to +VCC (+5V, +3.3V …) or GND (0V) depending on the type of the 7-segment display (common anode or common cathode respectively).
Basically for each 7-segment digit there are 8 pins: one for the common terminal (anode or cathode) and 7 pins for the 7 segments (A, B, C, D, E, F and G). Another pin may be used for the decimal point (DP).
In multi-digit 7-segment display (for example 4-digit) all pins of the same segment are connected together (segment A of digit 1 with segment A of digit 2 …), and each digit has its common pin alone. This is called multiplexing technique. This technique minimizes number of pins used.
So, for a 4-digit display we’ll have 7 pins of the 7 segments, 4 pins of the 4 digits (common terminals) and 1 pin for the decimal point (DP) which means a total of 12 pins.
Hardware Required:
- PIC16F887 microcontroller —-> datasheet
- 4-digit common anode 7-segment display
- 4 x PNP transistor (2SA1015, 2S9015, 2N3906 …)
- 7 x 100 ohm resistor
- 4 x 4.7k ohm resistor
- Push button
- 5V source
- Breadboard
- Jumper wires
- PIC MCU Programmer (PICkit 3, PICkit 4…)
Interfacing PIC16F887 MCU with 7-segment display circuit:
Project circuit schematic diagram is shown below.
All the grounded terminal are connected together.
In the circuit there are 4 transistors of the type PNP, the collector of each transistor is connected to common anode pin of 1 digit. That means each transistor supplies one digit segments.
The 4 transistors are used to supply the display LEDs with sufficient current because the PIC16F887 microcontroller may not be able to do that (maximum output current is 25 mA).
Each transistor emitter pin is connected to +5V and each transistor base is connected to the PIC16F887 through 4.7k resistor as follows:
digit 1 (most left) transistor base is connected to PIC16F887 pin RB3 (#36)
digit 2 transistor base is connected to PIC16F887 pin RB2 (#35)
digit 3 transistor base is connected to PIC16F887 pin RB1 (#34)
digit 4 (most right) transistor base is connected to PIC16F887 pin RB0 (#33)
Each 100 ohm resistor is used for limiting the current that passes through the segment LED.
The push button which is connected to PIC16F887 pin RB4 (#37) is used to increment the displayed number.
A common anode 7-segment display is used in this example.
In this project the PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.
Interfacing PIC16F887 MCU with 7-segment display C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.2.0.
Since the 4 digits are multiplexed we need to refresh the display very quickly (display one digit at a time, others are off), for that I used Timer0 module (8-bit timer) interrupt with 1:16 prescaler, this means Timer0 overflows every 2048 microseconds { 256/[8/(4 x 16)] = 256 x 8 = 2048 microseconds }.
Full mikroC code:
Configuration words:
CONFIG1 = 0x2CD4
CONFIG2 = 0x0700
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | /************************************************************************************** Interfacing PIC16F887 with common anode 7-segment display. 4-Digit counter example. C Code for mikroC PRO for PIC compiler. Internal oscillator used @ 8MHz Configuration words: CONFIG1 = 0x2CD4 CONFIG2 = 0x0700 This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ unsigned short current_digit; int count = 0; void disp(unsigned short number) { switch (number) { case 0: // print 0 PORTD = 0x02; break; case 1: // print 1 PORTD = 0x9E; break; case 2: // print 2 PORTD = 0x24; break; case 3: // print 3 PORTD = 0x0C; break; case 4: // print 4 PORTD = 0x98; break; case 5: // print 5 PORTD = 0x48; break; case 6: // print 6 PORTD = 0x40; break; case 7: // print 7 PORTD = 0x1E; break; case 8: // print 8 PORTD = 0x00; break; case 9: // print 9 PORTD = 0x08; } } void interrupt() { PORTD = 0xFE; // turn off all segments if(current_digit == 1){ PORTB = 0x07; // turn on digit 1 (most left) disp(count / 1000); } if(current_digit == 2){ PORTB = 0x0B; // turn on digit 2 disp((count / 100) % 10); } if(current_digit == 3){ PORTB = 0x0D; // turn on digit 3 disp((count / 10) % 10); } if(current_digit == 4){ PORTB = 0x0E; // turn on digit 4 (most right) disp(count % 10); } current_digit = (current_digit % 4) + 1; T0IF_bit = 0; // clear Timer0 interrupt flag bit } void main() { OSCCON = 0x70; // set internal oscillator to 8MHz ANSELH = 0; // configure all PORTB pins as digital PORTB = 0; TRISB = 0xF0; // configure RB0, RB1, RB2 & RB3 as outputs PORTD = 0; TRISD = 0; TMR0 = 0; // reset Timer0 OPTION_REG = 0x03; // set Timer0 prescaler to 1:16 INTCON = 0xA0; // enable global interrupt & Timer0 overflow interrupt WPUB4_bit = 1; // enable RB4 internal weak pull-up while(1) { if(PORTB.F4 ==0) { // if button is pressed count++; // increment 'count' by 1 if(count > 9999) count = 0; delay_ms(200); // wait 200 milliseconds } } } // end of code. |
The following picture shows my breadboard circuit:
Other PIC16F887 projects where 7-segment display is used:
Print ADC Values on 7-Segment Display with PIC MCU | mikroC Projects
PIC16F887 with 7-Segment Display and Rotary Encoder | mikroC Projects
7-Segment Display with 74HC595 Shift Register | mikroC Projects
PIC18F46K22 with 7-segment display and LM335 sensor | mikroC Projects
PIC18F46K22 with LM35 sensor and 7-segment display | mikroC Projects
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
Hello sir..
dear sir can you give me the software of pic hold indicator with wika pressor transmitter interfacing.