Lab 4 - Interfacing To Keypad and LCD Objectives
Lab 4 - Interfacing To Keypad and LCD Objectives
Lab 4 - Interfacing To Keypad and LCD Objectives
____________________________________________________________________
Objectives
To learn to read an input from a 4x4 keypad (using a 74922 keypad encoder).
Introduction / Briefing
LCD at Port D
Examine the connection below. Other than the power supply pins, you should
be able to locate the pins VEE, RS, R/W, E, DB7-0.
To make it easier for you to use the LCD, 4 functions have been written,
based on the table above and the “commands for LCD module” on the next
page. You don’t really have to understand the “fine prints” below or the
table on the next page.
1 0
1 1 0
0 1 0
There is no need to start from scratch when you need to use LCD. You can
modify an existing “main” function to suit your new application.
while(1)
{
lcd_write_cmd(0x80); // Move cursor to line 1 position 1
lcd_write_data(0x41); // write "A" to LCD
…..
Q1: Fill in the blanks below to show how you can display “HELLO” on the first line
of the LCD, and “WORLD” on the second line.
// Hello World.c
void main(void)
{
_______________ // Initialise LCD module
while(1)
{
_______________ // Move cursor to line 1 position 1
lcd_write_data(0x_____); // write "H" to LCD
lcd_write_data(0x_____); // write "E" to LCD
lcd_write_data(0x_____); // write "L" to LCD
lcd_write_data(0x_____); // write "L" to LCD
lcd_write_data(0x_____); // write "O" to LCD
void main(void)
{
lcd_init(); // Initialise LCD module
while(1)
{
lcd_write_cmd(0x80); // Move cursor to line 1 position 1
for (K = 0; K < 20; K++) // for 20 char LCD module
{
outchar = Message[ K ];
lcd_write_data(outchar); // write character data to LCD
}
…
Keypad at Port B
In the second part of this experiment, you will be reading from a 4x4 keypad
(with encoder) connected to Port B (pins 0, 1, 2, 3 and 5).
Examine the connection below. See how the 16 keys are labelled. The columns
are numbered X1, X2, X3, X4 (from left to right) while the rows are
numbered Y1, Y2, Y3, Y4 (from top to bottom). So, the key 2 is X2, Y1 while
the key B is X3, Y4.
These 8 signals (X’s and Y’s) from the keypad are connected to a keypad
encoder 74C922 which has the truth table below. As a result of “encoding”, 8
bits become 5 bits.
D 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
A
D (msb), C, B, A identify the key pressed. For instance, if key ‘2’ is pressed,
X2, Y1 cause DCBA = 0001. [Note: This does not tell the key pressed is 2, as
binary 0001 is not exactly decimal 2. Further interpretation is needed – see C
code below.] The DA (data available) signal will be set to logic ‘1’, whenever a
key is pressed.
Your answer: key ‘6’ is X__ & Y__. So, DCBA = ______, DA = ______
The function waits for DA to become 1 i.e. a key pressed. Then it reads from
Port B and mask off the top 4 bits, i.e. only RB3 to RB0 (connected to the
signals D, C, B and A) are retained. After that, it waits for the key to be
released. Finally, it returns the key pressed by looking up the look-up-table.
0000 1111
hardware C code
hardware C code
Q7: Assuming the hardware connections are unchanged, but the 4x4 keypad has
been labelled differently, as follows:
Q8. You will come across the following code in the last part of the experiment.
Activites:
Before you begin, ensure that the Micro-controller Board is connected to the LCD /
Keypad Board.
1. Launch the MPLABX IDE and create a new project called Lab4.
2. Add the file LCD2Lines.c to the Lab4 project Source File folder.
Make sure Copy is ticked before you click Select. If you have forgotten the
steps, you will need to refer to the previous lab sheet.
Note that the program uses the functions lcd_init (), lcd_write_cmd (),
lcd_write_data () from lcd_utilities.c and contains #include “lcd.h”. The files
lcd.h and lcd_utilities.c need to be added to the Project.
Display
msg on
LCD 3. Study the code (the main function) and describe what this program will do:
__________________________________________________
4. Build, download and execute the program. Observe the result and see if it is
as expected.
Changing
the msg
on LCD 5. Modify the code to show the following on the LCD. Build, download and
execute the program to verify your coding.
JOHN
9123456
7. Study the code and describe what this program will do:
__________________________________________________
8. Build, download and execute the program. Observe the result and see if it is
as expected.
4-key
PIN no.
on LCD 9. Modify the code to accept a 4-key PIN number (-- see Hint below). Build,
download and execute the program to verify your coding.
Hint:
unsigned char P1, P2, P3, P4; // variables to store a copy of the PIN number
// entered. Put this BEFORE any executable code in main
// put the following after the code to move cursor to line 2 position 1,
// replacing the 2nd for loop
key = getkey(); // get the first key
P1 = key; // save first key in P1
lcd_write_data(key); // display on LCD
while(1); // add this to prevent program from restarting after 4th key
Hiding 10. Further modify the code so that it will not display the actual PIN number
the PIN
number entered. Instead, * will be shown after each key.
Optional:
After 4 keys have been entered, the program can show the message:
“Processing……”
Hint: You need to have a char array: Message2 [] = “ Processing…..” and a loop
to display this message.
11. Build, download and execute the program to verify your coding. Debug until
the program can work.
13. This program will accept a 4-key password (or “PIN number”). If the correct
password is entered, the LCD will display “OPEN”. Otherwise, the LCD will
display “WRONG”
15. Build, download and execute the program. Observe the result and see if it is
as expected.
// LCD2Lines.c
// Program to test LCD.
// The LCD display with two lines, 24 characters each.
// There are three control lines (RD4:RD6) and four data lines(RD3:RD0).
// RD6 - RS=0 Data represents Command, RS=1 Data represents Character
// RD5 - RW=0 Writing into the LCD module
// RD4 - E =1 Data is latched into LCD module during low to hight
transition
#include <xc.h>
#include "lcd.h" // Include file is located in the project directory
void main(void)
{
lcd_init(); // Initialise LCD module
while(1)
{
lcd_write_cmd(0x80); // Move cursor to line 1 position 1
lcd_write_data(0x41); // write "A" to LCD
lcd_write_data(0x42); // write "B" to LCD
lcd_write_data(0x43); // write "C" to LCD
}
}
// LCDKeypad.c
// Program to test LCD and keypad.
// For project using USB interface with Bootloader
#include "lcd.h"
#include <xc.h>
#include "keypad.h"
#include "delays.h"
unsigned char key,outchar;
char Message1 [ ] = "Enter PIN number : "; // Defining a 20 char string
while(1)
{
lcd_write_cmd(0x80); // Move cursor to line 1 position 1
for (i = 0; i < 20; i++) //for 20 char LCD module
{
outchar = Message1[i];
lcd_write_data(outchar); // write character data to LCD
}
// LCDKeypad.c
// Program to test LCD and keypad.
// For project using USB interface with Bootloader
#include <xc.h>
#include "lcd.h"
#include "delays.h"
#include "keypad.h"
void main(void) {
int i;
lcd_init(); // Initialise LCD module
key = getkey(); // waits and get an ascii key number when pressed
p1 = key;
lcd_write_data(key); //display on LCD
key = getkey(); // waits and get an ascii key number when pressed
p2 = key;
lcd_write_data(key); //display on LCD
key = getkey(); // waits and get an ascii key number when pressed
p3 = key;
lcd_write_data(key); //display on LCD
key = getkey(); // waits and get an ascii key number when pressed
p4 = key;
lcd_write_data(key); //display on LCD
}
}
/*
* File: lcd utilities.c
*
* Created on 13 January, 2016, 10:28 AM
*/
#include <xc.h>
#define _XTAL_FREQ 48000000
#define LCD_RS PORTDbits.RD6 // Register Select on LCD
#define LCD_EN PORTDbits.RD4 // Enable on LCD controller
#define LCD_WR PORTDbits.RD5 // Write on LCD controller
void lcd_strobe(void);
//--- Function for writing a command byte to the LCD in 4 bit mode -------------
temp2 = cmd;
temp2 = temp2 >> 4; // Output upper 4 bits, by shifting out lower 4 bits
PORTD = temp2 & 0x0F; // Output to PORTD which is connected to LCD
temp1 = data;
temp1 = temp1 >> 4;
PORTD = temp1 & 0x0F;
temp1 = data;
PORTD = temp1 & 0x0F;
//-- Function to generate the strobe signal for command and character----------
for(i=0;i<100;i++)
__delay_ms(10); // Delay a total of 1 s for LCD module to
// finish its own internal initialisation
/* The datasheets warn that LCD module may fail to initialise properly when
power is first applied. This is particularly likely if the Vdd
supply does not rise to its correct operating voltage quickly enough.
lcd_write_cmd(0x33);
lcd_write_cmd(0x32);
__delay_ms(10); // 10 ms delay
extern char getkey(void); // waits for a keypress and returns the ascii code
/*
* File: keypad utilities.c
*
* Created on 13 January, 2016, 10:46 AM
*/
#include <xc.h>
//----- Function to obtained wait for key press and returns its ASCII value
char getkey(void)
{ char keycode;
const unsigned char lookup[] = "123F456E789DA0BC ";