1. 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.
    Dismiss Notice

4x4 matrix keypad & LCD with Atmega128 interfacing problem?

Discussion in 'AVR' started by nihal_2000, Mar 24, 2010.

  1. nihal_2000

    nihal_2000 New Member

    Joined:
    Aug 31, 2009
    Messages:
    26
    Likes:
    0
    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 (text):
    #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.

    [​IMG]
     
  2. mneary

    mneary New Member

    Joined:
    Feb 27, 2007
    Messages:
    4,502
    Likes:
    67
    Location:
    California USA
    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 (text):
    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: Mar 25, 2010
  3. nihal_2000

    nihal_2000 New Member

    Joined:
    Aug 31, 2009
    Messages:
    26
    Likes:
    0
    Thanks mneary, I think i have manage to solve it ..by just making keyport high before making rows low.

    Code (text):
    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
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. mneary

    mneary New Member

    Joined:
    Feb 27, 2007
    Messages:
    4,502
    Likes:
    67
    Location:
    California USA

    Whatever works.
     
  6. ahmedsamir77

    ahmedsamir77 New Member

    Joined:
    Oct 18, 2012
    Messages:
    5
    Likes:
    0
    where lcd.h

    where lcd.h please
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    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...
     
  8. ahmedsamir77

    ahmedsamir77 New Member

    Joined:
    Oct 18, 2012
    Messages:
    5
    Likes:
    0
    thank u

    do you know how to make password code please
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    I do it quite simply But I don't display 'hidden' characters as I use a 4 key keypad...

    Code (text):
    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: Oct 21, 2012
  10. ahmedsamir77

    ahmedsamir77 New Member

    Joined:
    Oct 18, 2012
    Messages:
    5
    Likes:
    0
    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
     
  11. ahmedsamir77

    ahmedsamir77 New Member

    Joined:
    Oct 18, 2012
    Messages:
    5
    Likes:
    0
    any one here
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    Just post the code.... I'll have a look..
     
  13. ahmedsamir77

    ahmedsamir77 New Member

    Joined:
    Oct 18, 2012
    Messages:
    5
    Likes:
    0
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    Why can't you just post it here?
     

Share This Page