element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Help with Scan code for 4x4 Multiplex keypad with 74hc595/74hc165 Shift Register
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 9 replies
  • Answers 2 answers
  • Subscribers 391 subscribers
  • Views 2796 views
  • Users 0 members are here
  • arduino
Related

Help with Scan code for 4x4 Multiplex keypad with 74hc595/74hc165 Shift Register

obulorn
obulorn over 7 years ago

Helo,

My final aim is to do an 8x8 keypad, presently I have 4x4 keypad available,so am trying out with that. Am using Arduino Uno R3 compatible. My setup is as in the attached: My problem is how to detect the column that went low on key press as to determined the key. Presently any key on Col 0 returns 240; Col1 returns 232; etc (dont know if that is ok. )Please help.

image

Am coding with visual studio 2015 cSharp +Visual Micro My code is as follow:

const int kbdRows = 4;

const int kbdCols = 4;

 

int LatchIn = 3; //165 pin1

int ClockPin = 5; // 595 pin11 & 165 pin2

int DataIn = 2; //165 pin9

int LatchOut = 6; // 595 pin12

int DataOut = 7; //595 pin14

 

int led = 7;

byte mask = 0x80;

byte lastmask = 0x10;

byte  incoming=0 ;

byte dataOuttest;

byte dataIntest;

 

int PinState = 0;

 

char keys[kbdRows][kbdCols] =

{

    { '1','2','3','4' },

    { '5','6','7','8' },

    { '9','0','A','B' },

    { 'C','D','E','F' }

};

 

 

byte KeyDown()

{

    //595 Here

   

    for (int row  = 0; row < kbdRows; row++)

    {

       

                digitalWrite(ClockPin, LOW);

                delayMicroseconds(5);

                digitalWrite(LatchOut, LOW);//

 

                shiftOut(DataOut, ClockPin, LSBFIRST, mask);

                digitalWrite(LatchOut, HIGH);//

                digitalWrite(ClockPin, HIGH);

               

                delay(100);

 

                Serial.println("  ");

                Serial.print("Row: "); // here was to test for Data Shift Out

                Serial.print(row);

 

                Serial.print(" ");

 

                Serial.print("Mask: ");

                Serial.println(mask);

                Serial.println("  ");

                Serial.println(" ");

                delay(100);

 

   

    //165 Here

       

                                

    for (int col = 0; col < kbdCols; col++)

 

    {

             digitalWrite(ClockPin, LOW);

       

                digitalWrite(LatchIn, LOW);

                delayMicroseconds(5);

                digitalWrite(LatchIn, HIGH);

       

                incoming = shiftIn(DataIn, ClockPin, LSBFIRST);

               digitalWrite(ClockPin, HIGH);

 

 

        Serial.print("Col: ");

        Serial.println(col);

        Serial.print("InComing: ");

        Serial.println(incoming);

 

       

    /////if(incoming & mask)/////////////////

    ///{

        /// byte keypress = keys[row][col];

        /// Serial.print("KeyPRESS: ");

    ///    Serial.println(keypress); ////////////// here is my probs, dont know how to return the pressed key with my setup

 

 

    /// }// end of if

       

       

        delay(600);

 

    } // end of col

    if (mask == lastmask)

        mask = 0x80;

    else

    mask = mask >> 1;

 

    } // row end

 

}

 

 

 

 

void setup()

{

 

  /* add setup code here */

    pinMode(ClockPin, OUTPUT);

    pinMode(DataOut, OUTPUT);

    pinMode(DataIn, INPUT);

    pinMode(LatchOut, OUTPUT);

    pinMode(LatchIn, OUTPUT);

 

    digitalWrite(LatchOut, LOW);

   

    digitalWrite(ClockPin, LOW);

    Serial.begin(9600);

 

    digitalWrite(led, HIGH);

   

}

 

void loop()

{

 

  /* add main program code here */

 

void setup()

{

 

  /* add setup code here */

    pinMode(ClockPin, OUTPUT);

    pinMode(DataOut, OUTPUT);

    pinMode(DataIn, INPUT);

    pinMode(LatchOut, OUTPUT);

    pinMode(LatchIn, OUTPUT);

 

    digitalWrite(LatchOut, LOW);

   

    digitalWrite(ClockPin, LOW);

    Serial.begin(9600);

 

    digitalWrite(led, HIGH);

   

}

 

 

void loop()

{

 

  /* add main program code here */

 

 

  KeyDown();

  delay(300);

 

}

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 7 years ago in reply to obulorn +1 suggested
    Hi Obulor, The line /////if(incoming & mask) should not have /// at the beginning, this is commenting it out. Nor should the subsequent lines have the //. Anyway that line if(incoming & mask) (i.e. without…
Parents
  • shabaz
    0 shabaz over 7 years ago

    Hi Obulor,

     

    Assuming the rest of the code is functioning, then in your KeyDown function you should have a line somewhere that says

    return(keypress);

    That line will make the function complete, and return control back to your function that called it in the first place, i.e. loop() function.

     

    Your loop() function is currently just calling KeyDown, but not making use of any result.

    It should declare a variable of some name, e.g. pressed_char, and the put the return value into it:

    byte pressed_char;
    pressed_char=KeyDown();

     

    To learn such things, this is described in books on C. It is highly recommended to read up on this, or try some online resources on C programming.

    C is simple (in principle) that you can learn a lot just by spending a couple of days reading a book.  Functions and return values will be covered in any book on C.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • obulorn
    0 obulorn over 7 years ago in reply to shabaz

    Hi shabaz,

    Many thanks for your time. I understand the point you raised, if you look through the code, the area I commented out, should handle that, but for now, the function is displaying it on the screen. The way  it run now is that, it display (or returns) all the keys in the array, one after another even without any keypress. I believe there should be an if condition that will make the condition display (or return) ONLY the pressed key. The IF condition is what I think should solve my problem. Please help.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • obulorn
    0 obulorn over 7 years ago in reply to shabaz

    Hi shabaz,

    Many thanks for your time. I understand the point you raised, if you look through the code, the area I commented out, should handle that, but for now, the function is displaying it on the screen. The way  it run now is that, it display (or returns) all the keys in the array, one after another even without any keypress. I believe there should be an if condition that will make the condition display (or return) ONLY the pressed key. The IF condition is what I think should solve my problem. Please help.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • shabaz
    0 shabaz over 7 years ago in reply to obulorn

    Hi Obulor,

     

    The line

    /////if(incoming & mask)

    should not have /// at the beginning, this is commenting it out.

    Nor should the subsequent lines have the //.

    Anyway that line

    if(incoming & mask)

    (i.e. without the //) means that you're checking for the signal to be high.

    But, your circuit pulls all the columns to logic high by default.

     

    You need the opposite logic, i.e. the row lines to all be high except one, and

    then test for low.

    To do that, change:

    shiftOut(DataOut, ClockPin, LSBFIRST, mask);

    to

    shiftOut(DataOut, ClockPin, LSBFIRST, mask^255);

     

    and change

    if(incoming & mask)

    to

    if((incoming & mask)==0)

     

    It might work then. I've not examined it in detail due to time availability. But to troubleshoot, closely examine what you're setting on the rows and what you're expecting to read in as a result.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube