Discussion in "8051 Discussion Forum" started by    romel_emperado    Nov 4, 2010.
Sat Jul 16 2011, 02:18 am
ok.. thanks majoka...
Sat Jul 16 2011, 04:12 am
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, 07:48 pm
during every iteration of for loop k gets incremented by 3

k+=3;


see it?
Mon Jul 18 2011, 08:00 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, 08:00 pm ]
Mon Jul 18 2011, 09:18 pm
I am glad its flexible enough to work in any condition
Thu Sep 01 2011, 10:22 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, 11:01 pm
@ romel_emperado
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, 06:39 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 = 1; //load data
    SPI_InLatch = 0;

    SPI_PLOAD = 0;
    SPI_PLOAD = 1;
    

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

    
    return data;

} 
Fri Sep 02 2011, 06:41 am
by the way the input is from input shift register..

74hc597
Fri Sep 02 2011, 10:41 am
@ romel_emperado
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

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Richardgar
Sat Apr 20 2024, 11:05 am
AntoniaRoons
Fri Apr 19 2024, 09:59 pm
carpinteyrowrl
Fri Apr 19 2024, 02:51 pm
DonaldJAX
Fri Apr 19 2024, 01:08 pm
Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am