Discussion in "8051 Discussion Forum" started by    romel_emperado    Nov 3, 2010.
Fri Jul 15 2011, 08:48 PM
ok.. thanks majoka...
Fri Jul 15 2011, 10:42 PM
guys I made some keypad projects based on this code from the tutorial without really understanding how it works...
I understand the working of keypad but I have question in the code portion..

```unsigned char get_key()
{
unsigned char i,k,key=0;
k=1;
for(i=0;i<4;i++)   //loop for 4 rows
{
keyport &=~(0x08<<i);   //to make rows low 1 by 1

if(!col1)  //check if key1 is pressed
{
key = k+0;      //set key number
while(!col1);   //wait for release
return key;     //return key number
}
if(!col2)//check if key2 is pressed
{
key = k+1;      //set key number
while(!col2);   //wait for release
return key;     //return key number
}
if(!col3) //check if key3 is pressed
{
key = k+2;      //set key number
while(!col3);   //wait for release
return key;     //return key number
}

//		   if keypad is of 4*4 un comment thiss block

//           if(!col4)
//		   {      //check if key4 is pressed
//                    key = k+3;      //set key number
//                    while(!col4);   //wait for release
//                    return key;     //return key number
//           }
k+=3;                   //next row key number
keyport |= 0x08<<i;     //make the row high again
}
return FALSE;                   //return false if no key pressed
}```

How does the key variable return a value from 0 to 9 ? I dont get it that's the only portion I do not understand.

let say for example we press any button belonged in column 3 then how does it display 5? if the only value of key is key = k+2; and key is 3 because the initial value of K is 1.. im confused..

```            if(!col3) //check if key3 is pressed
{
key = k+2;      //set key number
while(!col3);   //wait for release
return key;     //return key number
}```
Mon Jul 18 2011, 02:18 PM
during every iteration of for loop k gets incremented by 3

```k+=3;
```

see it?
Mon Jul 18 2011, 02:30 PM
ah okay..it's clearer now... I've done 3 projects using this keypad code of you ajay without totally understanding how it works.. lol hehe

[ Edited Mon Jul 18 2011, 02:30 PM ]
Mon Jul 18 2011, 03:48 PM
I am glad its flexible enough to work in any condition
Thu Sep 01 2011, 04:52 PM
Hi guys,

I Have question in this scenario I have 8 parallel inputs w/ 8 switches and it is being pulled up by 8 resistors, so the normal values of my input read by the PIC when no switches pressed is 255.(all 8bit input are high/1)

the switch used is a push button switch.. NORmally open

when I press one switch located at PORTA.RA0 the input value will now be 255 -1 = 254..

the switch is momentary when I dont push it it will go back and the input value again will be 255.

so now the problem is I want to hold that value until the next activity of the switch. (the value 254)

I want to store that value inside a variable without losing it even if the switch is not press anymore

I tried something like this but after sometime inside a while 1 loop the value 254 disappear..

this is my code:
```if(input != 255) //when any of the switch is pressed
store = input

return store;  ```
Thu Sep 01 2011, 05:31 PM
first make sure that u make a PORTA as an input port
post a code for better understanding
for above code lines it is not clear that where is the problem
i think ur facing problem with storing of variable

u can try also in this manner
```if(input != 255) //when any of the switch is pressed
store = input
while(store==input);
return store; ```
Fri Sep 02 2011, 01:09 AM
no majoka, everything is working fine about the port settings. every thing is working accordingly but the only problem is I cant remain the button state..

I have 8bit input and pulled up by a resistore.. so now if we read the input when no button is press it is equal to 255 because of the pull up resistor in the input.

now if we press any key let say PORTB0 the value will be 255 - 1 = 254 because we press one key.
the switch I am using is normally open push button switch so when I release the button I pressed it will go back again to 255.

now, what I am trying to do is I want to store every button press and it will be replace only until next button is press..

```void main()
{
while(1)
{
PORTB = get_inputDATA();
}

}  ```

```unsigned char get_inputDATA()
{
int i,data;

SPI_InLatch = 0;

for (i=7; i>
=0; i--)
{
if(SPI_SI)
data |= (1 << i); //store data received
toogle_clock();
}

return data;

} ```
Fri Sep 02 2011, 01:11 AM
by the way the input is from input shift register..

74hc597
Fri Sep 02 2011, 05:11 AM
ru checking it in Proteus
if yes then upload that schematic
74hc597 is a serial in parallel out shift register and ur using SPI for receiving data
first check ru receiving data or not from spi routine
i an data r two variables it should be unsigned char type
spi part is important for a while that is it working or not with shift register then go next
and i remember u do a spi code before has u remeber that link i lost that link
i think code was in hitec c

### Information

#### Conversation

Wed Jan 22 2020, 01:16 PM
MINU CHAUDHARY
I m getting errors while simulation process during interfacing socket output and lcd... Can i get the report of ur project which is same as us.
Tue Jan 14 2020, 02:08 AM
when i am trying to simulate the program in Proteus 8 professional, I am getting some errors as cannot find module file dhtxx.md can u help me
Tue Dec 17 2019, 08:44 AM
Devangi
hello..i am programming industrial timer using 8051 with 24lc64 eeprom. i have query in storing the 3 diff value in eeprom with enter button. can anyone share c code ?
Tue Dec 17 2019, 05:30 AM
Andrius
Hello. Can anyone share the code in Bascon and scheme in Proteus?
Thu Nov 21 2019, 07:30 AM
raj2555
how to interface 8051 microcontroller with NRF24L01+ module
Sat Oct 12 2019, 05:23 PM
Sat Oct 12 2019, 05:18 AM
i am doing a countdown timer with 8051 for my project, unfortunately our professor had to take a leave of absence before he could teach us most of it.. can someone explain the .asm code of it for me?
Mon Sep 23 2019, 02:37 AM
Nathaniel
I am doing a project for my schools wireless security system with cameras,sensors and spot lights using 8051 micro controller, could you help me out with the design and its source code.
Wed Sep 18 2019, 03:48 PM
Caryn Kover
Mon Aug 05 2019, 02:09 PM
@Ismail ..post in forum, give details

### Recent

brucewilliams20
Thu Jan 23 2020, 11:47 AM
Nippy56
Sun Jan 19 2020, 02:13 AM
usb5962
Sat Jan 11 2020, 10:42 AM
elt
Tue Jan 07 2020, 06:53 PM
mg3914697
Tue Jan 07 2020, 12:42 PM
afds43
Sun Dec 29 2019, 05:54 AM
Rupeshpatil
Fri Dec 27 2019, 06:14 PM
pyxus
Sat Dec 14 2019, 12:31 PM

#### Online

Guests: 42, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 38117
Forum Activity
##### Posted By ExperimenterUK
Fri Jan 17 2020, 10:24 PM
We will need the circuit diagram.A Proteus simulat...
##### Posted By Billz
Sat Jan 11 2020, 05:43 PM
hi everyone please attached files are my c code fo...
##### Posted By ExperimenterUK
Tue Jan 07 2020, 08:18 PM
Not a stupid question.Setting up "Eclipse" can be...
##### Posted By elt
Tue Jan 07 2020, 07:06 PM
So i have just began learning programming micros a...
##### Posted By Billz
Mon Jan 06 2020, 09:22 PM
I think it would be better if you guys try to come...
##### Posted By ExperimenterUK
Mon Jan 06 2020, 12:22 AM
Can you post a circuit diagram and any code you ha...
##### Posted By Billz
Sun Jan 05 2020, 08:17 PM
Need help regarding C code (specifically for PIC 1...
##### Posted By ExperimenterUK
Mon Dec 30 2019, 06:42 PM
Can you give more details of your problem.
##### Posted By Rupeshpatil
Fri Dec 27 2019, 06:21 PM
How to copy program my old microcontroller to new ...
##### Posted By ilylily07
Thu Dec 12 2019, 02:26 PM