dolphinlabs
...
Doplhin labs
News

LPC2148-4x4 Keyboard Interfacing


1.      Keypad

Objective:

Interfacing of 4X4 matrix Keypad with LPC2148 microcontroller and display the output on LCD

Hardware Connections for LPC2148 Board:-

1.      Plug in the USB cable to your computer and to the Board. Once the USB cable is plugged in, Board is powered up.

2.      Connect +5V pin of LCD interface to +5V supply pin and GND pin of LCD interface to GND pin of supply.

3.      Connect RS pin of LCD to P0.15 pin of microcontroller.

4.      Connect RW pin of LCD to P0.16 pin of microcontroller.

5.      Connect EN pin of LCD to P0.17 pin of microcontroller.

6.      Connect data pins of LCD D4-D7 to P0.18-P0.21 pins of microcontroller.

7.      Connect COL1 COL2 COL3 COL4 of keypad to P1.20, P1.21, and P1.22, P1.23 pins of microcontroller respectively and ROW1 ROW2 ROW 3 ROW4 of keypad to P1.16, P1.17, P1.18 and P1.19 pins of microcontroller.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Diagram:

+5V

 

 

 

 

 

 

 

 

 

 

 

 

   

 

Source code:

//key.h

extern void init_key(void);

extern void get_key(void);

extern void delay(int);

extern void key_pressed(unsigned char *key);

extern void goto_key(unsigned char);

 

//lcd.h

  #ifndef _LCD_H

#define _LCD_H

#define MAX_CHAR_IN_ONE_LINE 16

enum ROW_NUMBERS

{

  LINE1,

  LINE2          

};

void init_lcd(void);

void test_cmd(void);

void lcd_putstring(unsigned char line, char *string);

void lcd_clear(void);

void lcd_backlight_on(void);

int lcd_gotoxy(unsigned int x, unsigned int y);

void lcd_putchar(int c);

 

#endif

 

//lcd.c

#include <LPC21xx.H>                       /* LPC214x definitions */

#include "lcd.h"

 

#define LCD_BACK_LIGHT_TIMEOUT 1000

#define LCD_DATA_DIR       IO0DIR

#define LCD_DATA_SET       IO0SET

#define LCD_DATA_CLR      IO0CLR

 

#define LCD_CTRL_DIR        IO0DIR

#define LCD_CTRL_SET       IO0SET

#define LCD_CTRL_CLR       IO0CLR

 

#define LCDRS                     (1 << 15)

#define LCDRW                   (1 << 16)

#define LCDEN                    (1 << 17)

 

#define LCD_D4 (1 << 18)

#define LCD_D5 (1 << 19)

#define LCD_D6 (1 << 20)

#define LCD_D7 (1 << 21)

 

#define LCD_DATA_MASK           (LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7)

#define LCD_BUSY_FLAG           LCD_D7

 

#define LCD_IO0PIN                                               IO0PIN

 

void delay(int count)

{

  int j=0,i=0;

  for(j=0;j<count;j++)

  {

    /* At 60Mhz, the below loop introduces

    delay of 10 us */

    for(i=0;i<35;i++);

  }

}

void wait_lcd( void )

{

  LCD_DATA_DIR &= ~LCD_BUSY_FLAG;

  LCD_CTRL_CLR |=  LCDRS;                               /*Clear RS*/

  LCD_CTRL_SET |=  LCDRW |LCDEN;                /*Set RW, EN*/

  while(LCD_IO0PIN & LCD_BUSY_FLAG);       /* wait for busy flag to become low */

  LCD_CTRL_CLR |= LCDEN | LCDRW;               /*Clear RW, EN*/

  LCD_DATA_DIR |= LCD_DATA_MASK;           /* Clear all bits*/

    delay(100); 

}

 

void lcd_command_write( unsigned char command )

{

  unsigned char temp=0;

  unsigned int temp1=0;

 

  temp=command;

  temp=(temp>>4)&0x0F;

  temp1=(temp<<18)&LCD_DATA_MASK;

 

  LCD_CTRL_CLR = LCDRS;

  LCD_CTRL_SET = LCDEN;

  LCD_DATA_CLR = LCD_DATA_MASK;

  LCD_DATA_SET = temp1;

  delay(10000);

  LCD_CTRL_CLR = LCDEN;

  temp=command;

  temp&=0x0F;

  temp1=(temp<<18)&LCD_DATA_MASK;

  delay(100*2);

 

  LCD_CTRL_CLR |= LCDRS;

  LCD_CTRL_SET |= LCDEN;

  LCD_DATA_CLR = LCD_DATA_MASK;

  LCD_DATA_SET = temp1;

  delay(10000);           

  LCD_CTRL_CLR |= LCDEN;

  wait_lcd();

}

void set_lcd_port_output( void )

{

  LCD_CTRL_DIR |= ( LCDEN | LCDRS | LCDRW );

  LCD_CTRL_CLR |= ( LCDEN | LCDRS | LCDRW );     

  LCD_DATA_DIR |= LCD_DATA_MASK;

}

void lcd_clear( void)

{

  lcd_command_write( 0x01 );

}

int lcd_gotoxy( unsigned int x, unsigned int y)

{

  int retval = 0;

    if( (x > 1) && (y > 15) )

  {

    retval = -1;

  } else {

  if( x == 0 )

  {

    lcd_command_write( 0x80 + y );   /* command - position cursor at 0x00 (0x80 + 0x00 ) */

  } else if( x==1 ){

    lcd_command_write( 0xC0 + y );   /* command - position cursor at 0x40 (0x80 + 0x00 ) */

    }

    }

   return retval;

}

void lcd_data_write( unsigned char data )

{

  unsigned char temp=0;

  unsigned int temp1=0;

 

  temp=data;

  temp=(temp>>4)&0x0F;

  temp1=(temp<<18)&LCD_DATA_MASK;

 

  LCD_CTRL_SET |= LCDEN|LCDRS;

  LCD_DATA_CLR = LCD_DATA_MASK;

  LCD_DATA_SET = temp1;

  LCD_CTRL_CLR |= LCDEN;

 

  temp=data;

  temp&=0x0F;

  temp1=(temp<<18)&LCD_DATA_MASK;

 

  LCD_CTRL_SET |= LCDEN|LCDRS;

  LCD_DATA_CLR = LCD_DATA_MASK;

  LCD_DATA_SET = temp1;

  LCD_CTRL_CLR |= LCDEN;

  wait_lcd();

}

void lcd_putchar( int c )

{

  lcd_data_write( c );

}

 

void lcd_putstring( unsigned char line, char *string )

{

  unsigned char len = MAX_CHAR_IN_ONE_LINE;

 

  lcd_gotoxy( line, 0 );

  while(*string != '\0' && len--)

  {

    lcd_putchar( *string );

    string++;

  }

}

 

void init_lcd( void )

{

  set_lcd_port_output();

  delay(100*2);

  lcd_command_write(0x28);     /*   4-bit interface, two line, 5X7 dots.        */

  lcd_command_write(0x02);     /*   cursor home                                 */

  lcd_command_write(0x06);     /*   Entry mode                       */

  lcd_command_write(0x0F) ;    /*   display on cursor blinking    */ 

}

 

void test_cmd(void)

{

            //lcd_command_write(0x38);   // Interface length 8-bit 2 line

            lcd_command_write(0x08);   //Display off,cursor off

            lcd_command_write(0x0C);   //Display on cursor off

            lcd_command_write(0x10);   //Move cursor left by 1 char

            lcd_command_write(0x14);   //Move cursor right by 1 char

            lcd_command_write(0x01);   //Clear display

}

                       

//key.c

#include <LPC21xx.H>                       /* LPC214x definitions */

#include "lcd.h"

#include "key.h"

unsigned char keyboard[] = {'1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'};

#define LCD_KEY_DIR          IO1DIR

#define LCD_KEY_SET          IO1SET

#define LCD_KEY_CLR         IO1CLR

 

 

#define ROW1 (1 << 16)

#define ROW2 (1 << 17)

#define ROW3 (1 << 18)

#define ROW4 (1 << 19)

 

#define COL1 (1 << 20)

#define COL2 (1 << 21)

#define COL3 (1 << 22)

#define COL4 (1 << 23)

 

#define KEY_OUTPUT         (COL1 |COL2 |COL3 | COL4);

#define KEY_INPUT                   (ROW1 |ROW2 |ROW3 | ROW4);                    

 

void init_key()

{

            LCD_KEY_DIR = (ROW1 |ROW2 |ROW3 | ROW4);                               //rows as input

            LCD_KEY_DIR = ~(COL1 |COL2 |COL3 | COL4);                                       //columns as output

            LCD_KEY_SET = (ROW1 |ROW2 |ROW3 | ROW4);

}         

 

void get_key()

{         

            LCD_KEY_CLR       = ROW1;

            delay(20);

            goto_key(0);

            LCD_KEY_SET= ROW1;

            delay(20);

            LCD_KEY_CLR = ROW2;

            delay(20);

            goto_key(4);

            LCD_KEY_SET = ROW2;

            delay(20);

 

            LCD_KEY_CLR       = ROW3;

            delay(20);

            goto_key(8);

            LCD_KEY_SET        = ROW3;

            delay(20);

            LCD_KEY_CLR = ROW4;

            delay(20);

            goto_key(12);

            LCD_KEY_SET = ROW4;

            delay(20);         

}         

 

void goto_key(unsigned char i)

{

           

 if(!(IOPIN1 & COL1))

            {

            while(!(IOPIN1 & COL1))

            {

             continue;

            }

             lcd_putstring(0,"pressed key  ");

             lcd_putchar(keyboard[0+i]);

            }

            else if(!(IOPIN1 & COL2))

            {

             while(!(IOPIN1 & COL2))

            {

             continue;

            }

             

             lcd_putstring(0,"pressed key  ");

             lcd_putchar(keyboard[1+i]);

            }

            else if(!(IOPIN1 & COL3))

            {

             while(!(IOPIN1 & COL3))

            {

             continue;

            }

             lcd_putstring(0,"pressed key  ");

             lcd_putchar(keyboard[2+i]);

            }

            else if(!(IOPIN1 & COL4))

            {

             while(!(IOPIN1 & COL4))

            {

             continue;

            }

             lcd_putstring(0,"pressed key  ");

             lcd_putchar(keyboard[3+i]);

            }

}

 

#include <LPC21xx.H>                       /* LPC214x definitions */

#include "lcd.h"

#include "key.h"

 

//main.c

int main (void)

  init_lcd();                                                                    //Initialize the LCD

  test_cmd();

  init_key();                                                                    //Test all the LCD commands

  lcd_putstring(0,"press any key");

  while(1)

  {

   get_key();

  }

}         

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Get SOCIAL







SAFE & SECURE SHOPPING

visa card master card maestro card
Maverick IT Industries Pvt. Ltd.