Microcontroller › 8051 › Microcontroller Lcd display with keypad
- This topic has 0 replies, 1 voice, and was last updated 13 years, 2 months ago by
lgy.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
January 5, 2012 at 6:13 pm #1761
lgy
ParticipantHi i need help on my Lcd and keypad . I have a 4X4 keypad and a lcd .
Please help as Im struggling on the codes .Im able to display the keys to the lcd screen but i wanna let the user key in 4 digit and an alphabet ( the alphabet will act as a Enter ) and it will store the “passcode”.
this is my code for now .
#include <p18F4550.h>#include <delays.h>#include “lcd.h” // Include file is located in the project directory// Include this when using Bootloader Program ================================#pragma udataextern void _startup (void); // See c018i.c in your C18 compiler dir#pragma code _RESET_INTERRUPT_VECTOR = 0x000800void _reset (void){_asm goto _startup _endasm}#pragma code#pragma code _HIGH_INTERRUPT_VECTOR = 0x000808void _high_ISR (void){;}#pragma code _LOW_INTERRUPT_VECTOR = 0x000818void _low_ISR (void){;}#pragma code#pragma code// additional codes ends here ===============================================================// Your program declarations start here:====#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#define KEY_DA PORTBbits.RB5 // 74922 DA output#define KEY_PORT PORTB // RB3 to RB0 has keypad dataunsigned char key,msgindex,outchar,lcdindex;char Message1 [ ] = “Enter PIN number : “; // Defining a 20 char string//— Function for writing a command byte to the LCD in 4 bit mode
void lcd_write_cmd(signed char cmd){unsigned char temp2;LCD_RS = 0; // Select LCD for command modeDelay10TCYx(4); // 40us delay for LCD to settle downtemp2 = cmd;temp2 = temp2 >> 4; // Output upper 4 bits, by shifting out lower 4 bitsPORTD = temp2 & 0x0F; // Output to PORTD which is connected to LCDDelay1KTCYx(10); // 10ms -Delay at least 1 ms before strobinglcd_strobe();Delay1KTCYx(10); // 10ms – Delay at least 1 ms after strobingtemp2 = cmd; // Re-initialise temp2PORTD = temp2 & 0x0F; // Mask out upper 4 bitsDelay1KTCYx(10); // 10ms – Delay at least 1 ms before strobinglcd_strobe();Delay1KTCYx(10); // 10ms – Delay at least 1 ms before strobing}//—- Function to write a character data to the LCD
void lcd_write_data(char data){char temp1;LCD_RS = 1; // Select LCD for data modeDelay10TCYx(4); // 40us delay for LCD to settle downtemp1 = data;temp1 = temp1 >> 4;PORTD = temp1 & 0x0F;Delay1KTCYx(10);LCD_RS = 1;Delay1KTCYx(10); //_-_ strobe data inlcd_strobe();Delay1KTCYx(10);temp1 = data;PORTD = temp1 & 0x0F;Delay1KTCYx(10);LCD_RS = 1;Delay1KTCYx(10); //_-_ strobe data inlcd_strobe();Delay1KTCYx(10);}//– Function to generate the strobe signal for command and character
void lcd_strobe(void) // Generate the E pulse{LCD_EN = 1; // E = 0Delay1KTCYx(1); // 1ms delay for LCD_EN to settleLCD_EN = 0; // E = 1Delay1KTCYx(1); // 1ms delay for LCD_EN to settle}//—- Function to initialise LCD module
void lcd_init(void){TRISD = 0x00;PORTD = 0x00; // PORTD is connected to LCD data pinLCD_EN = 0;LCD_RS = 0; // Select LCD for command modeLCD_WR = 0; // Select LCD for write modeDelay10KTCYx(250); // Delay a total of 1 s for LCD module toDelay10KTCYx(250); //Delay10KTCYx(250); //Delay10KTCYx(250); // finish its own internal initialisation/* The data sheets warn that the LCD module may fail to initialise properly whenpower is first applied. This is particularly likely if the Vddsupply does not rise to its correct operating voltage quickly enough.It is recommended that after power is applied, a command sequence of3 bytes of 3xh be sent to the module. This will ensure that the module is in8-bit mode and is properly initialised. Following this, the LCD module can beswitched to 4-bit mode.*/lcd_write_cmd(0x33);lcd_write_cmd(0x32);lcd_write_cmd(0x28); // 001010xx ?Function Set instruction// DL=0 :4-bit interface,N=1 :2 lines,F=0 :5×7 dotslcd_write_cmd(0x0E); // 00001110 ?Display On/Off Control instruction// D=1 :Display on,C=1 :Cursor on,B=0 :Cursor Blink onlcd_write_cmd(0x06); // 00000110 ?Entry Mode Set instruction// I/D=1 :Increment Cursor position// S=0 : No display shiftlcd_write_cmd(0x01); // 00000001 Clear Display instructionDelay10KTCYx(20); // 20 ms delay}//
Function to obtained wait for key press and returns its ASCII valuechar getkey(void){ char keycode;const unsigned char lookup[] = “123F456E789DA0BC “;while (KEY_DA==0); //wait for key to be pressedkeycode=KEY_PORT &0x0F; //read from encoder at portB,mask upper 4 bitswhile (KEY_DA==1); //wait for key to be releasedreturn(lookup[keycode]); //convert keycode to its ascii value for LCD}// —- Main Program
void main(void){// Do not remove these as well=============ADCON1 = 0x0F; // Configure PORTA to be digital I/OCMCON = 0x07;// ========================================// Your MAIN program Starts here: =========lcd_init(); // Initialise LCD moduleLCD_RS = 1; // Select LCD for character data modeDelay1KTCYx(1); // 1 ms delaywhile(1){lcd_write_cmd(0x80); // Move cursor to line 1 position 1for (msgindex = 0; msgindex < 20; msgindex++) //for 20 char LCD module{outchar = Message1[msgindex];lcd_write_data(outchar); // write character data to LCD}lcd_write_cmd(0xC0); // Move cursor to line 2 position 1for (lcdindex = 0; lcdindex < 20; lcdindex++) //for 20 number{key=getkey(); // waits and get an ascii key number when pressedlcd_write_data(key); //display on LCD}lcd_write_cmd(0x01); // 00000001 Clear Display instructionDelay1KTCYx(250); // Delay for 1 s before proceeding to repeatDelay1KTCYx(250);Delay1KTCYx(250);Delay1KTCYx(250);}}Im sorry if its too long and messy as this is my first post in this forum . Your help will be much appreciated. -
AuthorPosts
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.