Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

4x4 matrix keypad & LCD with Atmega128 interfacing problem?

Status
Not open for further replies.

nihal_2000

New Member
Hi friends,
I am trying to interface a keyboard and LCD with ATmega128. I have manage to get lcd going but keyboard seems to be creating some problem…..it is not printing 1,5,9,13 which are on first row and first column.
Please try to find this strange behavior code and ckt is attached with.
Please help me!

Code:
#include <avr/io.h>             // Include file for AVR
#include <util/delay.h>
#include "lcd.h"
#define keyport PORTA           //Keypad Port
#define keyportddr DDRA         //Data Direction Register
#define keyportpin PINA         //Keypad Port Pins

#define col1 PA0                //Column1 PortA.0
#define col2 PA1                //Column2 PortA.1
#define col3 PA2                //Column3 PortA.2
#define col4 PA3                //Column4 PortA.3

#define TRUE 1
#define FALSE 0

unsigned char keyval;   //A variable


void key_init(){
        keyportddr = 0xF0;
        keyport = 0x0F;
}


unsigned char get_key(){
        unsigned char i,key=1;
        for(i=0;i<4;i++){               //Loop for 4 rows
                keyport &=~(0x80>>i);   //Make rows low one by one
                        if(!(keyportpin & (1<<col1))){
                         //check COL1
                                while(!(keyportpin & (1<<col1)));
                                //wait for release
                                return key;
                                //return pressed key value
                        }
                        if(!(keyportpin & (1<<col2))){
                         //Check COL2
                                key += 1;
                                //Second key pressed
                                while(!(keyportpin & (1<<col2)));
                                //wait for release
                                return key;
                                //return key value
                        }
                        if(!(keyportpin & (1<<col3))){
                         //Check COL3
                                key += 2;
                                //Third key pressed
                                while(!(keyportpin & (1<<col3)));
                                //Wait for release
                                return key;
                                //Return value
                        }
                        if(!(keyportpin & (1<<col4))){
                         //check COL4
                                key += 3;
                                //Fourth key pressed
                                while(!(keyportpin & (1<<col4)));
                                //Wait for release
                                return key;
                                //Return key value
                        }
                key +=4;        //Next row
                keyport |= 0x80>>i;
                //make read row high again
        }
        return FALSE;   //return false if no key pressed
}

int main(void){
   
   MCUCSR=0x80;
      MCUCSR=0x80;
   
   //Initialize LCD module
   InitLCD(LS_BLINK|LS_ULINE);

   //Clear the screen
   LCDClear();

   //Simple string printing
   LCDWriteString("Type the Number ");     
   
   unsigned char keyval;

   key_init();
   
   while(1){
   
      keyval = get_key();
      if(keyval != 0){

               LCDClear();

               LCDWriteString("You have entered");

               LCDWriteIntXY(1,1,keyval,3);

                     }
   
         }
   
   
   
   
         }

I have tried every possible thing with hardware ....i think there is some bug in my code it self please .....if you find guide me.

**broken link removed**
 
I've never seen the expression "&=~" before. Although we know what you mean, it would be interesting to see what the compiler says.

I just ran the simulator... yes, it is interesting.

[edit] I changed that line to force precedence and now it works perfectly.

Code:
keyport = ((~(0x80>>i)) | 0x0F);
Mine is pretty dirty, you can clean it up as you like. And you might want to use DDRA to pull port lines low so you don't have fault currents running between column drivers when more than one button is pressed.
 
Last edited:
Thanks mneary, I think i have manage to solve it ..by just making keyport high before making rows low.

Code:
unsigned char get_key(){
        unsigned char i,key=1;
        for(i=0;i<4;i++){               //Loop for 4 rows
                keyport |= 0xf0;           //Make rows high
                keyport &=~(0x80>>i);   //Make rows low one by one
                        if(!(keyportpin & (1<<col1))){
                         //check COL1
                                while(!(keyportpin & (1<<col1)));
                                //wait for release
                                return key;
                                //return pressed key value
 
where lcd.h please

Its a custom written LCD.H... I'm sure that the compiler will have a generic one.... Look in the source / examples directory in the compiler directory...
 
I do it quite simply But I don't display 'hidden' characters as I use a 4 key keypad...

Code:
char security()
	{
	int code = 0;
	char key=0;

	I2cReadStr(lcdBuf,0x390,16);   // external message  " SECURITY CODE "
	LCDline(1);
	LCDprint(lcdBuf);
	while(1)
		{
		sprintf(lcdBuf,"      %04d      ",code);
		LCDline(2);
		LCDprint(lcdBuf);
		key = keyhit();
		if(key == 4)
			{
			if(code == CODE ) return 1;
			else return 0;
			}
		if(key == 1 && code < 10000 )
			code*=10;
		if(key == 2 )
			code++;
		if(key == 3 && code)
			code--;
		if(key == 5)
			code = 0;
		}
	}
This code allows the user to input a four digit code that must be equal to the definition CODE the function returns 1 or 0
I do not have a 12 key or 16 key routine to hand, but it is easy to modify the above...
 
Last edited:
problem

i have a problem in this code to make password can u help me by sending code to u and u see what problem in my code if u want to help me give me your mail and i will send to u code
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top