This topic shows how to build a digital counter that counts from 0 to 9999 using PIC16F84A microcontroller and CCS PIC C compiler. The 4-digit 7-segment display uses multiplexing technique which means all the data lines from a to g are connected together for all the four digits. We will see circuit and code for both types of the 7-segment display: common anode and common cathode.
The following video shows a hardware circuit of the digital counter:
Hardware Required:
- PIC16F84A microcontroller —> datasheet
- 8MHz crystal oscillator
- 2 x 22pF ceramic capacitor
- Common anode/common cathode 4-digit seven-segment display module
- 4 x 2N3906 PNP transistor
- Push button
- 10k ohm resistor
- 4 x 4.7k ohm resistor
- 4 x 1k ohm resistor
- 7 x 100 ohm resistor
- 5V power source
- Bread board and jumper wires
- PIC microcontroller programmer (PICkit 2, PICkit 3…)
Interface PIC16F84A with common anode 7-segment display:
In common anode 7-segment display type all the 7 LED’s anodes are connected together.
Interfacing PIC16F84A with common anode 7-segment display (digital counter circuit) circuit schematic is below.
If the button which is connected to RB7 pressed the displayed number will be incremented by 1. The PIC16F84A microcontroller must be supplied with +5V between its power pins VDD (#14) and VSS (#5) (not shown in the circuit diagram).
4-Digit common anode 7-segment display counter with PIC16F84A CCS C code:
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 | // 4-Digit digital counter using PIC16F84A (common anode type) // https://simple-circuit.com/ #include <16F84A.h> #fuses HS,NOWDT,PUT,NOPROTECT #use delay(crystal=8000000) short s; // Used to know button position unsigned int digit, digit1, digit10, digit100,digit1000; unsigned long i; unsigned int seg(unsigned int num) { switch (num) { case 0 : return 0xC0; case 1 : return 0xF9; case 2 : return 0xA4; case 3 : return 0xB0; case 4 : return 0x99; case 5 : return 0x92; case 6 : return 0x82; case 7 : return 0xF8; case 8 : return 0x80; case 9 : return 0x90; } } void main() { while(TRUE) { if(input(PIN_B7) == 1) s = 1; if(s == 1) { if(input(PIN_B7) == 0) { s = 0; i++; if(i > 9999) i = 0; } } digit = i % 10; digit1 = seg(digit); output_a(0x0F); // Turn off all displays output_b(digit1); // Send ones digit output_a(0x07); // Turn on display for ones delay_ms(5); digit = (i / 10) % 10; digit10 = seg(digit); output_a(0x0F); // Turn off all displays output_b(digit10); // Send tens digit output_a(0x0B); // Turn on display for tens delay_ms(5); digit = (i / 100) % 10; digit100 = seg(digit); output_a(0x0F); // Turn off all displays output_b(digit100); // Send hundreds digit output_a(0x0D); // Turn on display for hundreds delay_ms(5); digit = (i / 1000) % 10; digit1000 = seg(digit); output_a(0x0F); // Turn off all displays output_b(digit1000); // Send thousands digit output_a(0x0E); // Turn on display for thousands delay_ms(5); } } |
Interface PIC16F84A with common cathode 7-segment display:
In common cathode 7-segment display type all the 7 LED’s cathodes are connected together.
The following circuit schematic is for 4-digit digital counter counts from 0 to 9999, the number is incremented when button is pressed.
The PIC16F84A microcontroller must be supplied with +5V between its power pins VDD (#14) and VSS (#5) (not shown in the circuit diagram).
4-Digit common cathode 7-segment display counter interfacing with PIC16F84A CCS C code:
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 | // 4-Digit digital counter using PIC16F84A (common cathode type) // Common cathode 7-segment display // https://simple-circuit.com/ #include <16F84A.h> #fuses HS,NOWDT,PUT,NOPROTECT #use delay(crystal=8000000) short s; // Used to know button position unsigned int digit, digit1, digit10, digit100,digit1000; unsigned long i; unsigned int seg(unsigned int 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; } } void main() { while(TRUE) { if(input(PIN_B7) == 1) s = 1; if(s == 1) { if(input(PIN_B7) == 0) { s = 0; i++; if(i > 9999) i = 0; } } digit = i % 10; digit1 = seg(digit); output_a(0); // Turn off all displays output_b(digit1); // Send ones digit output_a(8); // Turn on display for ones delay_ms(5); digit = (i / 10) % 10; digit10 = seg(digit); output_a(0); // Turn off all displays output_b(digit10); // Send tens digit output_a(4); // Turn on display for tens delay_ms(5); digit = (i / 100) % 10; digit100 = seg(digit); output_a(0); // Turn off all displays output_b(digit100); // Send hundreds digit output_a(2); // Turn on display for hundreds delay_ms(5); digit = (i / 1000) % 10; digit1000 = seg(digit); output_a(0); // Turn off all displays output_b(digit1000); // Send thousands digit output_a(1); // Turn on display for thousands delay_ms(5); } } |
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.