Hi following is the routine i found at net for 4x4 matrix keypad in C.
www.8051projects.net/.../8051-programming.php
can i use this to display digits using the keypad inputs or do i need any keypad numabers matrix to be assigned?. If yes, Can sombody reply me the simple main code[while(1)] to monitor the key lets say if 5 is pressed then make P1 as high or something like that?. I just want to display digit 5 using keypad.
Thanks in advance.
But the code you already have has one function that must called before you may use the other function. Always initialization functions first.
And it has one function that will return a number between 0 and 16, where 0 means no key was pressed.
All you have to do is call the init function. Then in a loop call the get-key function and assign the result to a variable.
Then use a switch statement or a number of if statements to decide what to do for the 17 different results you may get from the keyboard function.
If you still do not know what to do, you will just have to start by getting a book about the C language. It really is important that you read up on the language before you try to use real hardware. You will not get a working program unless you understand what different code lines does, and until you have a basic understanding of the available building blocks and how they can be combined to from a program.
Thanks for your nice information, As u mentioned, i am little weak in C concepts, But understanding easily through practical applications than theory books. I mean the lines, what is it actually doing and all. Anyhow will write a code as u said and will try. Since i am week in C only asked that 4 line codes from others. Once i get the basics, i will expand it to complex applications. Will try it soon. Thanks.
Does the below program make Port 1 high when keypad given the value 5 as output and Port 1 as low if key value is 6?
void main(){ keypad_init(); while (1){ get_key(key); if (key == 5) { P1=1; } if (key == 6) { P1=0; } } }
Above code works with the given routine?. Since i have no access to kiel right now, i am simply asking this now. Its just my rough code, Please help me if any mistakes. Thanks
The function get_key() does not take any parameter. It returns a value, so you call it with key=get_key().
Thanks a lot. this is where my actual doubt is.
So, while (1){ key = get_key(); If(key == 5){ P1 = 1; } If(key == 6){ P1=0; } }
So it will keep monitoring key value from get_key() and will assign to local variable key, if 5 P1 -> high, If 6 P1->low.
Will try the routine and this code with keil.
Hi Per Westermark, Thanks for your help, It is working fine. Will give the remaining code since it will helpful to other peoples like me, The remaining code for the above routine is,
void main(){ unsigned int key; key_init(); while (1){ key = get_key(); If(key == 5){ P1 = 0xFF; } If(key == 6){ P1=0; } } }
Where the above works fine which will simply make P1 as high if key value is 5 and low if 6. For remaining keys it will latch the previous value.
It is often better to use space instead of tab for indenting the code. With spaces, the indent will be consistent when switching between editors or when presenting the code on the net.
Another thing. For an 8-bit processor, the code will be smaller and faster if you use 8-bit variables whenever you can, instead of using the 16-bit int data type.
Don't retype source code when making posts. Always copy/paste, to make sure you don't introduce errors. In C, the keyword is "if", and not "If".
In C, a function that doesn't take parameters should be specified with "void" instead of an empty list between the parentheses.
int main(void){ unsigned char key; key_init(); while (1) { key = get_key(); if (key == 5) { P1 = 0xFF; } if (key == 6) { P1 = 0; } } return 0; }
I would simply recommend that you always use spaces for indenting code - never use TABs!
The interpretation of TABs is entirely undefined - even different users of the same application may see different results due to different user settings!
The only thing you can be certain of with TABs is that they will not give reliable results!
Thanks for your kind guidance, Since i am in learning phase, i am making many mistakes. Will try to rectify it through experience.
I made the program as u said, It was working great. So i got excited, Your coding style is really good, Will follow it in future.
Thanks.