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.

code problems for 4x4 keypad

Status
Not open for further replies.
yes it could work already thanks!
and it is for just row4 (*,0,#,D) on at pin B3-B0 respectively
if i wan it to light up all then have to write the second ReadKeys again?

Sorry Darsin,

When I looked yesterday, it was very quick, and not so helpful...

Pommies function will return a word containing the info on which keys are pressed in your pad. It handles all of the keys. If any keys are pressed the corresponding bits will be set in the word it returns. If no keys are pressed you will get 0x00 back. Use a bit mask to see which keys have been pressed. Do you understand how to do this, or do you need more info?

What I noticed in your last post, is that you turned on the LED for only 200 cycles. If your mcu is running at 4MHz then this isn't long enough to see it on... its only 200 micro seconds... 2 tenths of a thousandth of a second... You have to turn it on longer than this or you won't see it come on.

You should also look up switch debouncing because you will find that the keys are not reliable:

http://www.ganssle.com/debouncing.pdf.

http://www.best-microcontroller-projects.com/switch-debounce.html

I see Mike is still on here and I'm so slow...
 
Last edited:
oh..i knew it
so u mean if i wanna press the key '*', then the code should be:
if (ReadKeys()&(1<<15)!=0)
then if i put a led on the port c0 and when i press '*' i want to high it is :
void main()
{
TRISC=0b00000000; //make port c output
while (1)
{
if (ReadKeys()&(1<<15)!=0)
{
LATC=0b00000001; //high the pin c0
}
}

}
i try this but this could not work for my led
 
dear beebop,
can you show me the steps?
i'm not understand about bit masks..
thanks!

OK, a bit mask is a word, (or a byte, but here you are using 16 bits) which contains the number you want to check for. You AND that with what ReadKeys() returns. To and it, you use the bitwise operator &.

When you and two words, if any two bit positions are set (logic 1) then the corresponding bit in the result is set.
For example, say you use this:
Code:
#define myKey 0x8000    // same as (1<<15)

if(ReadKeys() & myKey !=0) {
    //myKey has been pressed
}

To see how it works, use the binary numbers:
0x8000 = 1000 0000 0000 0000

looking at ReadKeys() it sets the Most Significant Bit
if row 4 key 4 was pressed = 1000 0000 0000 0000

so
1000 0000 0000 0000 &
1000 0000 0000 0000 equals
1000 0000 0000 0000 since the 2 most significant bits were set

if any other key is pressed, say row 2 col 1:
ReadKeys() returns 0000 0000 0001 0000 and when you and this with your bit mask you will get zero:

1000 0000 0000 0000 &
0000 0000 0001 0000 equals
0000 0000 0000 0000

Hope I've explained it well, but if you need more just ask. I need a coffee....
 
err...
frankly speaking it's a bit complicated for me @.@...
well as u mention just now the "row 4 key 4 is press" means is pressing which key?
i thought that (1<<15) is pressing the '*' as Pommie said just now?
err.. i'm sorry..i'm always trouble u all...
 
err...
frankly speaking it's a bit complicated for me @.@...
well as u mention just now the "row 4 key 4 is press" means is pressing which key?

Pommie is correct; I'm not...

His ReadKeys() reads a 4x4 key pad.

Just to clarify, you are using a 4x4 keypad, yes? or a 3x4?
i thought that (1<<15) is pressing the '*' as Pommie said just now?
1<<15 shifts the bit left 15 times so you get
0x0001 << 15 =
1000 0000 0000 0000

Does it make sense now?

err.. i'm sorry..i'm always trouble u all...

That's what this forum is for :) ... You don't need to apologize!
 
Last edited:
oh..i'm using the 4x4 keypad
i now can catch a bit what u mean..
the row 2 col1:
row2 =1000 0000 0000 0000
col1 = 0000 0000 0001 0000
is it like that?
why col1 will be 0000 0000 0001 0000?
 
Last edited:
ok so my keypad is work...
thank you bananasiong, pommie and beebop for helping me...
i'm really appreaciate it...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top