You are on page 1of 6

Microprocessor

Laboratory 2 – Decimal Counter Using PIC16F877A with 7-Segment Display


Objectives:
- To integrate the external components to PIC16F877A microcontroller.
- To manipulate the display using 7 Segments.
Requirements:
1. The students have understanding on the concept of microcontroller.
2. The students have understanding on how to use programming to manipulate the output
using 7-segment display to count numbers whether up or down.
3. The students have knowledge on Looping statement, and binary to decimal decoding for
easier and faster approach to achieved the desired output.
Materials and Equipment:
1. Laboratory Manual
Discussion:
What is microcontroller?
A microcontroller is a compact integrated circuit designed to govern a specific operation in an
embedded system. A typical microcontroller includes a processor, memory and input/output (I/O)
peripherals on a single chip.
Sometimes referred to as an embedded controller or microcontroller unit (MCU),
microcontrollers are found in vehicles, robots, office machines, medical devices, mobile radio
transceivers, vending machines and home appliances, among other devices. They are essentially
simple miniature personal computers (PCs) designed to control small features of a larger
component, without a complex front-end operating system (OS).
Microcontrollers usually operate at lower speeds, around the 1MHz to 200 MHz range, and need
to be designed to consume less power because they are embedded inside other devices that can
have greater power consumptions in other areas.
A microcontroller can be seen as a small computer, and this is because of the essential
components inside of it; the Central Processing Unit (CPU), the Random-Access Memory
(RAM), the Flash Memory, the Serial Bus Interface, the Input/Output Ports (I/O Ports), and in
many cases, the Electrical Erasable Programmable Read-Only Memory (EEPROM).
In figure 1 shows the components inside the microcontroller

Fig. 1 – Microcontroller Components


Interfacing Seven Segment with PIC16F877A
To display numeric values, we can use seven segment displays. We will interface a seven
segment to PIC16f877a and display a single digit decimal counter (0-9). Well, the name 7
segments imply there are 7 LED segments arranged. Since these are basically LEDs arranged as
a group, they can either have the anode in common or cathode thus they are named as Common-
Anode/Common-Cathode displays.
- Common Cathode: In this type of segments all the cathode terminals are made common
and tied to GND. Thus, the segments a to g needs a logic High signal(5v) to glow.
- Common Anode: In this type of segments all the anodes’ terminals are made common
and tied to VCC(5v). Thus the segments a to g needs a logic LOW signal (GND) in order
to glow.
Below table shows the binary/hex values for displaying the digits on Common Anode seven
segment display.
Laboratory Exercise 1:
Using the PIC16F877A microcontroller create a circuit that will count from 0-9 and the numbers
will be displayed in the 7-Segment.
Circuit Diagram:

Source Code: (Mikro C)


void main() {
char seg_code[10] = {0xcC0,0xF9, 0xA4,0xB0,0x99,0x92,0x82,0xf8,0x80,0x90};
int i;
TRISD = 0x00;
while(1){
for(i=0;i<=9;i++){
PORTD = seg_code[i];
delay_ms(1000);
}
}
}
Laboratory Exercise 2:
Create a circuit that will count from 0-9999 using PIC16F877a and 7-Segment Display.
Circuit Diagram:

Source Code:
#define SegOne 0x01
#define SegTwo 0x02
#define SegThree 0x04
#define SegFour 0x08

void main() {
char
seg_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,
0x8e};
int cnt, num, temp,i;

/* Configure the ports as output */


TRISB = 0x00; // Data lines
TRISD = 0x00; // Control signal PORTD0-PORTD3

while (1)
{
for (cnt = 0x00; cnt <= 9999; cnt++) // loop to display 0-9999
{
for (i = 0; i < 100; i++)
{
num = cnt;
temp = num / 1000;
num = num % 1000;
PORTD = SegOne;
PORTB = seg_code[temp];
DELAY_ms(1);

temp = num / 100;


num = num % 100;
PORTD = SegTwo;
PORTB = seg_code[temp];
DELAY_ms(1);

temp = num / 10;


PORTD = SegThree;
PORTB = seg_code[temp];
DELAY_ms(1);

temp = num % 10;


PORTD = SegFour;
PORTB = seg_code[temp];
DELAY_ms(1);
}
}
}
}

Laboratory Activity:
Using the 7-Segment Display and the PIC16F877A microcontroller create a circuit that will
count the number of visitors entering the building and will count down if the person leaves the
building. Limit the counting to 9 only.
Do the following:
1. Schematic Diagram
2. Source Code
Questions:
1. Write below the specific code you use to count-up the numbers from 0-9?
Answer:
if (!PORTB.F1 && i<9)
{
i++;
PORTD = segdisplay[i];
delay_ms(50)
}
2. Write below the specific code you use to count-down the numbers from 9-0?
Answer:
if (!PORTB.F2 && i>0)
{
i--;
PORTD = segdisplay[i];
delay_ms(50);

}
3. Write the code in setting your ports as INPUT?
Answer:
TRISB.F1=1;
TRISB.F2=1;

4. Write the code in setting your ports as INPUT?


Answer:
TRISB.F1=1;
TRISB.F2=1;
5. What ports did you use as INPUT and OUTPUT?
Answer:
Input Ports:
PORTB.F1, PORTB.F2.
Output Ports:
PORTD F0 to F6.
Observations:
The input ports are always on as it is directly connected to the power, then the push button turns
it off as the push button is connected to the ground. It can be all manipulated through the use of
programming. The push button serve as the switch to count the number of visitor. The upper
button purpose is for adding the number of visitor while the lower button subtracts.

You might also like