Vous êtes sur la page 1sur 13

UNIVERSITI MALAYSIA PERLIS

SCHOOL OF
ELECTRICAL SYSTEMS ENGINEERING

EET 203
MICROCONTROLLER SYSTEMS DESIGN

Module 3
PIC Input and Output Interfacing 7 Segment and Keypad
EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 3

PIC INPUT AND OUTPUT INTERFACING 7 SEGMENT AND KEYPAD

OBJECTIVES

At the end of this laboratory session, students should be able to:

i. write C language program to control Seven Segment using PIC16F877A.


ii. design C language program to control and manipulate Seven Segment and Keypad using
PIC16F877A.

EQUIPMENT / COMPONENTS

MPLAB IDE
HI TECH C Compiler
Computer Unit
PTK40A Training Kit

INTRODUCTIONS

7 SEGMENT DISPLAY:

7 Segment is a component that consists seven segments of lights which can be used to
display the number from 0 to 9. There is two types of 7 segment which are common anode (CA)
and common cathode (CC). The 7 Segment can display any number (0 - 9) by activating the correct
segment via providing 0V for CA type or 5V for CC type. As example, to display the number 3,
the segment a, b, c, d, and g should be switch on and segment f and e should be switch off. Table
3.1 shows the binary code for displaying different numeric and alphabetic characters for both the
common cathode and the common anode type displays.

However, connecting 7 segment directly to the microcontroller required more I/O pins
and complex to control if involving multiple units of 7 segment. In order to save the usage of the
I/O pins of MCU and to simplify the control commands, the 7 Segments are connected with a
decoder unit. In PTK40A, two CD4511 are used to decode 2 units of 7 Segment, as shown in Figure
3.1. With that, 2 units of 7 Segment can be controlled with 6 output pins from microcontroller.

Copyright School of Electrical Systems Engineering, UniMAP. 2017 (V1.1) 2


EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 3

Table 3.1: 7 Segment Display Code for Common Anode and Common Cathode type

Figure 3.1: Connection of 7 Segment with PIC Microcontroller

Copyright School of Electrical Systems Engineering, UniMAP. 2017 (V1.1) 3


EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 3

After connecting a 7 Segment to CD4511, the numbers can be displayed on it by providing


the input signal as shown in Table 3.2. For example, with set input signal (D3, D2, D1 and D0) to
low (0) will display 0 on 7 Segment.

Table 3.2: CD4511 function table

Copyright School of Electrical Systems Engineering, UniMAP. 2017 (V1.1) 4


EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 3

3.0 KEYPAD INTERFACING:

Keypad is an array of switch. The internal structure for standard 4 x 4 keypad is shown in
Figure 3.2. There will be 2 terminal pins connected each time a button is pressed. For example;
when button 3 is pressed, pin COL3 and ROW1 is connected. Initially, there is no connection
between rows and columns. The button connects it. The keypads pins need to be pulled up or
pulled down to avoid floating case. Pull up normally connect to 5 V and pull down is connect to
ground. 4 x 4 Keypad pin can directly connect to microcontroller or keypad decoder IC.

Figure 3.2. Internal Structure of 4x4 Matrix Keypad

The 4 x 4 keypad is a 16-way XY-Matrix hexadecimal keypad which has eight connections
where four pins for row (ROW1 - ROW4) and four pins for column (COL1 - COL4). The internal
connection for the keypad is shown in Figure 3.3. Most of microcontroller applications require a
keypad for users to enter numbers and commands. The usual way to interface a keypad to a
microcontroller is by connecting input/output (I/O) port bits to row and column connections on
the keypad. Figure 3.4 shows the 4 x 4 matrix keypad connection to ports. (Refer Lecture Notes
or PTK40A Schematic (PDF file) for detail connections.)

Copyright School of Electrical Systems Engineering, UniMAP. 2017 (V1.1) 5


EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 3

PORTD-High

(output)

PORTB-Low

(input)
Figure 3.3: Internal Connection of a 4x4 Keypad

Figure 3.4: Keypad Connections to Ports

With reference to schematic in Figure 3.4 and PTK40A Schematic file, the row pins are
connected directly to PIC microcontroller (RD4-RD7), and being configured as writeable pins
(Output from PIC). The column pins are being pulled high to 5 V and being connected to RB0-RB3,
and being configured as readable pins (Input to PIC).

Copyright School of Electrical Systems Engineering, UniMAP. 2017 (V1.1) 6


EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 3

In this lab, the scanning method is used to perform key press detection. In order to check
which button is pressed, users need to scan it column by column and row by row. In the program,
rows is set as an output and columns as an input. In 1st scan, row 1 is clear (logic low) and column
1 to column 4 (1, 2, 3 or A) is scan (read) one by one to detect for low logic, this will
determine which button is pressed in row 1. If one of those buttons is pressed, record it and jump
out from the scanning loop and continue with the action required. If none button is pressed in
row 1, it was set back to default (logic high). The process continue by clearing row 2. Then column
1 to column 4 are read again to scan button press of either 4, 5, 6, or B. These processes are
repeated until all four rows are being completely scanned.

Copyright School of Electrical Systems Engineering, UniMAP. 2017 (V1.1) 7


EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 3

LABORATORY EXERCISE

1. Figure 3.5 shows the block diagram of a basic 7 Segment display program.

Figure 3.5: Block Diagram for Keypad Display

Write the following sample program to count up (0 ~ 9) on 7 Segment display. The counter
will increment when SW1 is pressed.

Program Code

//define switch
#define SW1 RB0
//define 7-Segment
#define Seg1 RE0
#define Seg2 RE1

//Display the value of keypad on 7 Segment and hold it


void S71_Disp_BCD(unsigned char bcd_num)
{
Seg1 = 0; //enable the decoder to received new number
PORTD = bcd_num; //send bcd value to decoder
__delay_us(30); //delay for a while
Seg1 = 1; //holding the state before this
}

Copyright School of Electrical Systems Engineering, UniMAP. 2017 (V1.1) 8


EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 3

//main program
void main(void)
{
// set the port direction
TRISD = 0x00; //PORT D as o/p
TRISE0 = 0; // Pin E0 as output to control LE decoder 1 (Seg1)
TRISE1 = 0; // Pin E1 as output to control LE decoder 2 (Seg2)
TRISB0 = 1; //Pin B0 as input (SW1)

Seg1 = 1; //send HIGH signal to disable Seg1


Seg2 = 1; //send HIGH signal to disable Seg2

unsigned char up; //declare variable for counting


up=0; //set counting value starting from 0

while(1)
{
if (SW1==0) //if switch is being pressed
{
S71_Disp_BCD(up); //sending bcd number to display on 7 segment
up++; //increment counter
if (up==10) //if counter >10,
{
up=0; //reset it back
}
}
}
}

Copyright School of Electrical Systems Engineering, UniMAP. 2017 (V1.1) 9


EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 3

2. Figure 3.6 shows the block diagram of a basic keypad display program. Write the following
sample program to read the key pressed and display its number on the 7 Segment display. For
example, if key number 5 is pressed, the digit number 5 will be displayed on the Seven
Segment. The assumptions to be made:

When A, B, C, D, *, or # is pressed, the 7 segment will display blank.

Figure 4.6: Block Diagram for Keypad Display

Figure 3.6. The block diagram of a basic microcontroller interface with keypad and seven segment

Program Code

//define Keypad (refer to PTK40A Schematic)


#define KP_R1 RD4
#define KP_R2 RD5
#define KP_R3 RD6
#define KP_R4 RD7

//function for Keypad


unsigned char Read_Keypad()
{

unsigned char uc_number = 0;


//Scanning process
//Row1
KP_R1=0;
KP_R2=1;
KP_R3=1;
KP_R4=1;
__delay_us(30);
//checking every column
if(KP_C1==0) uc_number=1;//return location number
if(KP_C2==0) uc_number=2;// return location number
if(KP_C3==0) uc_number=3;// return location number
if(KP_C4==0) uc_number=4;// return location number

Copyright School of Electrical Systems Engineering, UniMAP. 2017 (V1.1) 10


EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 3

//Row2
KP_R1=1;
KP_R2=0;
KP_R3=1;
KP_R4=1;
__delay_us(30);
//checking every column
if(KP_C1==0) uc_number=5;// return location number
if(KP_C2==0) uc_number=6;// return location number
if(KP_C3==0) uc_number=7;// return location number
if(KP_C4==0) uc_number=8;// return location number

//Row3
KP_R1=1;
KP_R2=1;
KP_R3=0;
KP_R4=1;
__delay_us(30);
//checking every column
if(KP_C1==0) uc_number=9;// return location number
if(KP_C2==0) uc_number=10;// return location number
if(KP_C3==0) uc_number=11;// return location number
if(KP_C4==0) uc_number=12;// return location number

//Row4
KP_R1=1;
KP_R2=1;
KP_R3=1;
KP_R4=0;
__delay_us(30);
//checking every column
if(KP_C1==0) uc_number=13;// return location number
if(KP_C2==0) uc_number=14;// return location number
if(KP_C3==0) uc_number=15;// return location number
if(KP_C4==0) uc_number=16;// return location number

return (uc_number); //return value


}

Copyright School of Electrical Systems Engineering, UniMAP. 2017 (V1.1) 11


EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 3

//define 7-Segment (refer to PTK40A Schematic)


#define Seg1 RE0
#define Seg2 RE1

//Display the value of keypad on 7 Segment and hold it


void S72_Disp_BCD(unsigned char bcd_num)
{
Seg2 = 0; //enable the decoder to received new number
PORTD = bcd_num; //send bcd value to decoder
__delay_us(30); //delay for a while
Seg1 = 1; //holding the state before this
}

//create array with keypad value (0x0A is used to display blank on 7 segment)
const unsigned char kp_pattern[]={0x0A,0x01,0x02,0x03,0x0A,0x04,0x05,0x06,0x0A,0x07,
0x08,0x09,0x0A,0x0A,0x00,0x0A,0x0A};

void main(void)
{
//set direction
TRISB = 0x0F; //Pin B0-B3 as i/p (for keypad)
TRISD = 0x00; //PortD as o/p (for keypad(PortD[High]) and 7-segment(PortD[Low]))
TRISE0 = 0; // Pin E1 as output (Seg 1)
TRISE1 = 0; // Pin E1 as output (Seg 2)

Seg1 = 1; //send HIGH signal to disable Seg1


Seg2 = 1; //send HIGH signal to disable Seg2

unsigned char kp_num;


while(1)
{
kp_num = Read_Keypad();
S72_Disp_BCD(kp_pattern [kp_num]);
}
}

Copyright School of Electrical Systems Engineering, UniMAP. 2017 (V1.1) 12


EET 203 [MICROCONTROLLER SYSTEMS DESIGN] MODULE 3

NAME : MATRIC NO :
NAME : MATRIC NO :
LAB TITLE : PIC INPUT AND OUTPUT INTERFACING 7 SEGMENT AND KEYPAD PC NO:

PRE-LAB QUESTION

1. State how to define the direction of the PIC Port for Keypad and 7 segment.



.
(4 marks)

2. Explain the scanning method process for keypad













(6 marks)

Copyright School of Electrical Systems Engineering, UniMAP. 2017 (V1.1) 13

Vous aimerez peut-être aussi