LABORATORY MANUAL Interfacing LCD 16x2, Keypad 4x4 and 7Segment Display to PIC18F458 1. OBJECTIVES: 1.1 To learn how to interface LCD 16x2, Keypad 4x4 and 7Segment Display to the microcontroller. 1.2 To program LCD using the assembly language. 1.3 To program Keypad 4x4 and LCD using the assembly language. 1.4 To program Keypad 4x4 and 7 Segment Display using the assembly language. 1.5 To program Keypad 4x4 and UART using the assembly language. 1.6 To draw the circuit and running the PROTEUS simulation. 1.7 To construct the hardware. 2. COMPONENTS AND EQUIPMENTS: 2.1 Mplab IDE 2.2 Sk4B/SK4C 2.3 Proteus 7.6 software 2.4 Resistor (4.7kΩ, 33Ω,1kΩ) 2.5 Led 2.6 Push Button Switch 2.7 Breadboard 2.8 AC-to-DC Adaptor (9V Selected Point) 2.9 Pickit2 programmer UICA 2.1 Pickit2 programmer software 2.11 Serial cable/usb serial port 2.12 HyperTerminal software 2.13 Virtual serial port software 2.14 7Segment Display 2.15 LCD 16x2 2.16 Keypad 4x4 2.17 Potential Meter 5kΩ 2.18 2N394 (NPN-Transistor) 3. INTRODUCTION: 3.1 LCD 16x2 3.1.1 LCD Pin Descriptions - 1 -
Pin Symbol I/O Description 1 GND -- Ground (V) 2 VDD -- +5V power supply 3 V -- Power Supply for LCD 4 RS I RS= to select COMMAND register (send command) RS=1 to select DATA register (send data) 5 R/W I R/W= for WRITE to LCD R/W=1 for READ from LCD 6 E I/O Enable 7 DB I/O The 8-bit data bus 8 DB1 I/O The 8-bit data bus 9 DB2 I/O The 8-bit data bus 1 DB3 I/O The 8-bit data bus 11 DB4 I/O The 8-bit data bus 12 DB5 I/O The 8-bit data bus 13 DB6 I/O The 8-bit data bus 14 DB7 I/O The 8-bit data bus 15 BL1 -- Backlight Power (+5V) 16 BL2 -- Backlight Power (V) 3.1.2 LCD Pin Configurations Table1: LCD pin descriptions 1 2 3 4 5 6 7 8 9 1 11 12 13 14 15 16 VSS VCC VEE RS R/W E DB DB1 DB2 DB3 DB4 DB5 DB6 DB7 LED+ LED- Table2: LCD pin configuration 3.1.3 LCD Command Codes The 8-bit data pins D-D7 are used to send information to the LCD or read the contents of the LCD s internal registers. We send ASCII codes to display letters or numbers while making RS=1. Code (HEX) Command to LCD Instruction Register. 1 Clear display screen 2 Return home 4 Decrement cursor (shift cursor to left) 6 Increment cursor (shift cursor to right) 5 Shift display right 7 Shift display left 8 Display off, cursor off A Display off, cursor on C Display on, cursor off E Display on, cursor blinking off - 2 -
F Display off, cursor blinking on 1 Shift cursor position to left 14 Shift cursor position to right 18 Shift the entire display to the left 1C Shift the entire display to the right 8 Force cursor to beginning of 1 st line C Force cursor to beginning of 2 nd line 38 2 lines and 5x7 matrix Table3: LCD command codes 3.1.4 LCD Connection PIC 18 LCD RD D VDD V RD7 D7 GND RS R/W E RC RC1 RC2 Figure1: LCD pin connections to microcontroller 3.1.5 Sending Data and Command to LCD There are two ways to send characters (command/data) to the LCD: Use a delay before sending the next one. Use the busy flag to see if the LCD is ready for the next one. - 3 -
Using a time Delay The delay between issuing each character to the LCD is about 5-1mS (we call it simply DELAY). In programming an LCD, we also need a long delay for the power-up process, (we call it long delay LDELAY). In order to enable LCD s input, the short delay is needed to make the En signal wide enough. Using Busy Flag \ 3.2 KEYPAD 4x4 The busy flag is D7, and can be read when R/W=1 and RS=. When D7 =1 (busy flag=1), the LCD is busy taking care of internal operations and will not accept any new information. When D7=, the LCD is ready to receive new information. Usually keyboards organized in a matrix of rows and columns. The CPU accesses both rows and columns through ports. When a key pressed, a row and column make a contact; otherwise there is no connection between rows and columns. To interface keypad, we must have two processes a) key press detection, and b) key identification. There are two ways, to perform key press detection: The interrupt method (PortB-Change interrupt can be used) Scanning method 3.2.1 Interrupt Method using PORTB Change Interrupt By using 4x4 matrix keypad connected to PORTB. The rows are connected to PORTB-Low (RB-RB3) and the columns are connected to PORTB-High (RB4-RB7), which is the PORTB-Change interrupt. Note: Refer your reference book at pages 449-452 for details information using PORTB change interrupt. 1 2 3 A 4 5 6 B 7 8 9 C * # D
Figure 2: Matrix Keypad Connections to Ports Figure3: Keypad 4x4 KEYPAD 4x4 RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB C1 C2 C3 C4 R4 R3 R2 R1 1 2 3 A 4 5 6 B 7 8 9 C * # D PIC18F458 C1 C2 C3 C4 R1 R2 R3 R4 Figure 4: Keypad pin connections to microcontroller - 5 -
Figure 5: Key Press Detection Flowchart. - 6 -
3.2.2 Scanning Method Figure 6: Flowchart of Scanning Method for Key Press Detection - 7 -
3.3 7SEGMENT DISPLAY A seven-segment display (SSD), or seven-segment indicator, is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot-matrix displays. Seven-segment displays are widely used in digital clocks, electronic meters, and other electronic devices for displaying numerical information. The SSD consists of 7 LED s described from figure 6 below. Usually SSD was made in two types which are common cathode and common anode but here we are focus in common anode only. 3.3.1 7Segment Display Pin Connections +5V MSB G 1 PORTD7 ANODE F 2 PORTD6 E D DP C B 4 PORTD5 5 PORTD4 6 PORTD3 7 PORTD2 9 PORTD1 A 3,8 1 PORTD 7-SEGMENT LSB PIC18F458 Figure7: common anode 7segment display pin connections to microcontroller. - 8 -
3.3.2 7Segment Display Label Figure8: 7Segment Visual Structure 3.3.3 7Segment Truth Table Common Anode MICRONTROLLER PORTD DATA POTRD7 POTRD6 POTRD5 POTRD4 POTRD3 POTRD2 POTRD1 POTRD DIPLAY G F E D DP C B A 1 1 1 1 1 1 1 2 1 1 3 1 1 4 1 1 1 5 1 1 6 1 7 1 1 1 1 8 9 1 1 A 1 B 1 1 C 1 1 1 D 1 1 E F 1 1 1 1 1 Table 4: 7segment display truth table common anode - 9 -
4. PROCEDURES. a. Copy the source code from the source code file and design the circuit by using the PROTEUS has shown in Figure 9 and Table 5 at appendices. Load the program onto the simulation and running the simulation for LCD source code. b. Construct the hardware and prove the results by compare with simulation. c. Copy the source code from the source code file and design the circuit by using the PROTEUS has shown in Figure 1 and Table 6 at appendices. Load the program onto the simulation and running the simulation for Keypad and LCD source code. d. Construct the hardware and prove the results by compare with simulation. e. Copy the source code from the source code file and design the circuit by using the PROTEUS has shown in Figure 11 and Table 7 at appendices. Load the program onto the simulation and running the simulation for Keypad and 7Segment Display source code. f. Construct the hardware and prove the results by compare with simulation. g. Copy the source code from the source code file and design the circuit by using the PROTEUS has shown in Figure 12 and Table 8 at appendices. Load the program onto the simulation and running the simulation for Keypad and HyperTerminal source code. h. Construct the hardware and prove the results by compare with simulation. 5. APPENDICES. - 1 -
Components CAP CYSTAL Figure 9: Interfacing with LCD connection Values/Configurations 22pF X1 (2Mhz) PIC18f458(U1) FREQ = 2MHZ RES SWITCH LCD POT R1=4.7k SW RESET LM16L RV1=5k Table 5: Components Figure 1: Interfacing with Keypad 4x4 and LCD connection Components CAP Values/Configurations 22pF - 11 -
CYSTAL X1 (2Mhz) PIC18f458(U1) FREQ = 2MHZ RES SWITCH LCD POT KEYPAD R1=4.7k SW RESET LM16L RV1=5k KEYPAD-SMALLCALC Table 6: Components Figure 11: Interfacing with Keypad 4x4 and 7segment display connection Components CAP Values/Configurations 22pF - 12 -
CYSTAL X1 (2Mhz) PIC18f458(U1) FREQ = 2MHZ RES SWITCH Q1 POT R1=4.7k, R2-R9=33, R1=1k SW RESET 2N394 (NPN) RV1=5k 7 Segment 7SEGMENT-COM-AN-GREEN Table 7: Components Figure 12: Interfacing with Keypad 4x4 and HyperTerminal connection Components CAP CYSTAL Values/Configurations 22pF X1 (2Mhz) - 13 -
PIC18f458(U1) FREQ = 2MHZ RES SWITCH COMPIM(P1) R1=4.7k SW RESET Port = com1, baud rate = 96, Data bits = 8, Parity = NONE. Table 8: Components Figure 13: Schematic and Pin Connection 2N394-14 -