Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

code for 2x2 keypad interface with mega32

Status
Not open for further replies.

shahfenil08

Newbie level 1
Joined
Feb 19, 2012
Messages
0
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,280
Activity points
1,259
#include <mega32.h>

// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x12 ;PORTD
#endasm
#include <lcd.h>
#include<delay.h>
//#include<stdio.h>

#define KB_PORT_OUT PORTB
#define KB_PORT_IN PINB
void main(void)
{
unsigned char upperNibble, keyCode, keyPressed, i;
lcd_init(16);
PORTD=0x00;
DDRD=0xFF;
PORTB=0xff;
DDRB=0xF3;

while (1)
{
//lcd_int(); // display ON

lcd_gotoxy(0,0);
lcd_putsf("welcome");

lcd_gotoxy(0,1);
delay_ms(100);
_lcd_write_data(0x0f);
//moving LCD cursor to second row

upperNibble = 0xff;

for(i=0; i<2; i++)
{
//PORTB &= 0x0f ;
delay_ms(1);
KB_PORT_OUT = ~(0x01 << i);
delay_ms(1); //delay for port o/p settling
upperNibble = KB_PORT_IN | 0x03;

if (upperNibble != 0xff)
{
delay_ms(20); //key debouncing delay
upperNibble = KB_PORT_IN | 0x03;
if(upperNibble == 0x0f) goto OUT;

keyCode = (upperNibble & 0x0c) | (0x03 & ~(0x01 << i));

while (upperNibble != 0xff)
upperNibble = KB_PORT_IN | 0x03;
// PORTB &= 0x0f;
delay_ms(20); //key debouncing delay

switch (keyCode) //generating key characetr to display on LCD
{
case (0x09): keyPressed = '0';
break;
case (0x05): keyPressed = '1';
break;
case (0x0a): keyPressed = '2';
break;
case (0x06): keyPressed = '3';
break;

default : keyPressed = 'X';
}//end of switch

lcd_putchar(keyPressed);


OUT:;
}//end of if
}//end of for*/
}//end of while(1)
}//end of main()
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top