Vous êtes sur la page 1sur 5

Lab Notes 4

PIC16F877: KEYPAD LCD

Mechatronics Lecture Notes by Dr. Can U. Dogruer

L4. PIC16F877:KEYPAD LCD INTERFACING


Objectives 1. Interface LCD 2. Learn how to LCD library of mikroC PART LIST NO 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 3. PART NAME PIC16F877 / PIC16F877A Push Button 10K Resistor Keypad (4x4) LCD (16x2) Potentiometer MAX232 1-micro Farad Polarized Capacitor RS-232 Connector RS-232 Cable 20 MHz Crystal 22-picoFarad Capacitor Copper Wire USB PIC Programmer [Hardware] PIC Downloader Program [Bootloader; Software] QUANTITY 1 1 9 1 1 1 1 5 1 1 1 2 2 meter 1 1

L10.1 EXPERIMENT V: KEYPAD LCD INTERFACING In this experiment you will interface a keypad to a PIC microcontroller. This experiment is an extension of the previous week LCD interfacing application. You have to preserve the LCD interfacing circuit and only connect the keypad to PORTB. Note that you need to attach pull-down resistor of 10K to each keypad pins. NOTE: STUDY LCD AND KEYPAD LIBRARY HELP FILES

Hacettepe University / Mechanical Engineering Department Automotive Engineering Program

L4-1

Lab Notes 4
PIC16F877: KEYPAD LCD

Mechatronics Lecture Notes by Dr. Can U. Dogruer

Hacettepe University / Mechanical Engineering Department Automotive Engineering Program

L4-2

Mechatronics Lecture Notes PIC16F877: KEYPAD LCD by Dr. Can U. Dogruer L10.2KEYPAD LCD INTERFACING: Reading Keypad 1. 2. 3. 4. Construct the circuit shown in Figure 1. Note that pins of keypad is attached to pull-down resistors of 10K In this experiment keypad is interfaced to PORTB so Write a C code that display text on LCD display

Lab Notes 4

SAMPLE mikroC Source Code


unsigned short kp, cnt, oldstate = 0; char txt[6];

// Keypad module connections char keypadPort at PORTB; // End Keypad module connections // LCD module connections sbit LCD_RS at RC4_bit; sbit LCD_EN at RC5_bit; sbit LCD_D4 at RC0_bit; sbit LCD_D5 at RC1_bit; sbit LCD_D6 at RC2_bit; sbit LCD_D7 at RC3_bit;

sbit LCD_RS_Direction at TRISC4_bit; sbit LCD_EN_Direction at TRISC5_bit; sbit LCD_D4_Direction at TRISC0_bit; sbit LCD_D5_Direction at TRISC1_bit; sbit LCD_D6_Direction at TRISC2_bit; sbit LCD_D7_Direction at TRISC3_bit; // End LCD module connections

void main() { cnt = 0; // Reset counter

Hacettepe University / Mechanical Engineering Department Automotive Engineering Program

L4-3

Lab Notes 4
PIC16F877: KEYPAD LCD
Keypad_Init(); // Initialize Keypad

Mechatronics Lecture Notes by Dr. Can U. Dogruer

Lcd_Init(); Lcd_Cmd(_LCD_CLEAR);

// Initialize LCD // Clear display // Cursor off

Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1, 1, "1"); Lcd_Out(1, 1, "Key :"); Lcd_Out(2, 1, "Times:");

// Write message text on LCD

do { kp = 0; // Reset key code variable

// Wait for key to be pressed and released do // kp = Keypad_Key_Press(); kp = Keypad_Key_Click(); while (!kp); // Prepare value for output, transform key to it's ASCII value switch (kp) { //case 10: kp = 42; break; // '*' // Uncomment this block for keypad4x3 //case 11: kp = 48; break; // '0' //case 12: kp = 35; break; // '#' //default: kp += 48; // Store key code in kp variable // Store key code in kp variable

case 1: kp = 49; break; // 1 case 2: kp = 50; break; // 2 case 3: kp = 51; break; // 3 case 4: kp = 65; break; // A case 5: kp = 52; break; // 4 case 6: kp = 53; break; // 5 case 7: kp = 54; break; // 6 case 8: kp = 66; break; // B

// Uncomment this block for keypad4x4

Hacettepe University / Mechanical Engineering Department Automotive Engineering Program

L4-4

Lab Notes 4
PIC16F877: KEYPAD LCD
case 9: kp = 55; break; // 7 case 10: kp = 56; break; // 8 case 11: kp = 57; break; // 9 case 12: kp = 67; break; // C case 13: kp = 42; break; // * case 14: kp = 48; break; // 0 case 15: kp = 35; break; // # case 16: kp = 68; break; // D }

Mechatronics Lecture Notes by Dr. Can U. Dogruer

if (kp != oldstate) { cnt = 1; oldstate = kp; } else { cnt++; } Lcd_Chr(1, 10, kp);

// Pressed key differs from previous

// Pressed key is same as previous

// Print key ASCII value on LCD

if (cnt == 255) { cnt = 0; Lcd_Out(2, 10, " "); } WordToStr(cnt, txt); Lcd_Out(2, 10, txt); } while (1); }

// If counter varialble overflow

// Transform counter value to string // Display counter value on LCD

void interrupt() { }

Hacettepe University / Mechanical Engineering Department Automotive Engineering Program

L4-5