Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Forums > General Electronics Chat


General Electronics Chat This forum is for general chat about electronics, eg: Dont know what a part does? Dont know how to read a circuit? Want to get an opinion?

Reply
 
Tools
Old 15th July 2008, 12:11 PM   #16
Default

Hi,
From the schematic, it's clearly shown that for checking '1' source RB7 and read from RB3.
__________________
bananasiong
bananasiong is offline  
Old 15th July 2008, 12:36 PM   #17
Talking

so it's means that i the program that i wrote i correct already?

Last edited by darsin; 15th July 2008 at 12:58 PM.
darsin is offline  
Old 15th July 2008, 02:37 PM   #18
Default

Hi Darsin,

To read from a matrix keyboard you need to make 1 of the row bits an output and make it high. You can then read 4 column bits.

See if this function will read your keyboard,
Code:
unsigned int ReadKeys(){
unsigned int Key;
    TRISB=0b01111111;       //make row 1 output
    LATB=0b10000000;        //and make it high
    Key=PORTB&0x0f;         //read the columns into bits 0-3
    TRISB=0b10111111;       //make row 2 output
    LATB=0b01000000;        //and make it high
    Key|=(PORTB&0x0f)<<4;   //read the columns into bits 4-7
    TRISB=0b11011111;       //make row 3 output
    LATB=0b00100000;        //and make it high
    Key|=(PORTB&0x0f)<<8;   //read the columns into bits 8-11
    TRISB=0b11101111;       //make row 4 output
    LATB=0b00010000;        //and make it high
    Key|=(PORTB&0x0f)<<12;  //read the columns into bits 12-15
    return Key;
}
When you call the function then it will return an integer with a bit set for each key that is pressed.

HTH.

Mike.
Pommie is offline  
Old 15th July 2008, 04:01 PM   #19
Default

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?
darsin is offline  
Old 15th July 2008, 04:18 PM   #20
Default

I don't understand your question. However, the integer returned from the function would have a bit set for each key pressed. They would be,

bit 0 = A,
bit 1 = 3,
bit 2 = 2,
.
.
bit 14 = 0,
bit 15 = *,

So, to do something if the * key was pressed you would do,
Code:
if(ReadKeys()&(1<<15)!=0){
    //do something here
}
HTH

Mike.
Pommie is offline  
Old 15th July 2008, 04:26 PM   #21
Default

Quote:
Originally Posted by darsin View Post
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-proj...-debounce.html

I see Mike is still on here and I'm so slow...

Last edited by BeeBop; 15th July 2008 at 04:28 PM.
BeeBop is offline  
Old 15th July 2008, 04:34 PM   #22
Default

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
darsin is offline  
Old 15th July 2008, 04:45 PM   #23
Talking

dear beebop,
can you show me the steps?
i'm not understand about bit masks..
thanks!
darsin is offline  
Old 15th July 2008, 05:21 PM   #24
Default

Quote:
Originally Posted by darsin View Post
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....
BeeBop is offline  
Old 15th July 2008, 05:49 PM   #25
Default

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...
darsin is offline  
Old 15th July 2008, 06:00 PM   #26
Default

Quote:
Originally Posted by darsin View Post
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?
Quote:
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?

Quote:
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 by BeeBop; 15th July 2008 at 06:05 PM.
BeeBop is offline  
Old 15th July 2008, 06:24 PM   #27
Default

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 by darsin; 15th July 2008 at 06:43 PM.
darsin is offline  
Old 16th July 2008, 07:20 AM   #28
Default

ok so my keypad is work...
thank you bananasiong, pommie and beebop for helping me...
i'm really appreaciate it...
darsin is offline  
Reply

Tags
4x4, code, keypad, problems

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Problems with 4x4 keypad interact with LCD.. chunei General Electronics Chat 17 10th April 2008 02:23 PM
Problem with keypad code pic_programm3r Micro Controllers 20 2nd April 2008 07:37 AM
what wrong with my keypad code eleceyes Micro Controllers 0 20th February 2008 03:08 AM
Keypad Code gregmcc Micro Controllers 5 11th May 2006 09:01 AM
code lock keypad questions.... eyevancsu Electronic Projects Design/Ideas/Reviews 7 10th April 2003 08:17 AM



All times are GMT. The time now is 10:33 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker