Module 3
Module 3
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
OBJECTIVES
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.
Table 3.1: 7 Segment Display Code for Common Anode and Common Cathode type
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.
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.)
PORTD-High
(output)
PORTB-Low
(input)
Figure 3.3: Internal Connection of a 4x4 Keypad
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).
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.
LABORATORY EXERCISE
1. Figure 3.5 shows the block diagram of a basic 7 Segment display program.
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
//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)
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
}
}
}
}
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:
Figure 3.6. The block diagram of a basic microcontroller interface with keypad and seven segment
Program Code
//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
//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)
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)
(6 marks)