| View previous topic :: View next topic   | 
	
	
	
		| Author | 
		Message | 
	
	
		
			Ahmed
 
 
  Joined: 07 Mar 2006 Posts: 19
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| Flexible Keypad Driver | 
			 
			
				 Posted: Mon Mar 13, 2006 3:48 pm     | 
				     | 
			 
			
				
  | 
			 
			
				This is a modification of CCS keypad driver (KBDD.c) with flexible pins connections.
 
 
 	  | Code: | 	 		  ///////////////////////////////////////////////////////////////////////////
 
////                             Flex_KBD.C                            ////
 
////                  Generic keypad scan driver                       ////
 
////                                                                   ////
 
////  kbd_init()   Must be called before any other function.           ////
 
////                                                                   ////
 
////  c = kbd_getc(c)  Will return a key value if pressed or /0 if not ////
 
////                   This function should be called frequently so as ////
 
////                   not to miss a key press.                        ////
 
////                                                                   ////
 
///////////////////////////////////////////////////////////////////////////
 
 
 
 
//Keypad connection:  
 
 
#define col0 PIN_E1
 
#define col1 PIN_A5
 
#define col2 PIN_C0
 
#define row0 PIN_E0 
 
#define row1 PIN_C2
 
#define row2 PIN_C1
 
#define row3 PIN_E2
 
 
// Keypad layout:
 
char const KEYS[4][3] = {{'1','2','3'},
 
                         {'4','5','6'},
 
                         {'7','8','9'},
 
                         {'*','0','#'}};
 
 
#define KBD_DEBOUNCE_FACTOR 33    // Set this number to apx n/333 where
 
                                  // n is the number of times you expect
 
                                  // to call kbd_getc each second
 
 
 
 
void kbd_init() {
 
}
 
 
 
short int ALL_ROWS (void)
 
{
 
   if (input (row0) & input (row1) & input (row2) & input (row3))
 
      return (0);
 
   else
 
      return (1);
 
}
 
 
 
 
char kbd_getc( ) {
 
   static byte kbd_call_count;
 
   static short int kbd_down;
 
   static char last_key;
 
   static byte col;
 
 
   byte kchar;
 
   byte row;
 
 
   kchar='\0';
 
   if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
 
       switch (col) {
 
         case 0   : output_low(col0);
 
               output_high(col1);
 
               output_high(col2);
 
                    break;
 
         case 1   : output_high(col0);
 
               output_low(col1);
 
               output_high(col2);
 
                    break;
 
         case 2   : output_high(col0);
 
               output_high(col1);
 
               output_low(col2);
 
                    break;
 
       }
 
 
       if(kbd_down) {
 
         if(!ALL_ROWS()) {
 
           kbd_down=false;
 
           kchar=last_key;
 
           last_key='\0';
 
         }
 
       } else {
 
          if(ALL_ROWS()) {
 
             if(!input (row0))
 
               row=0;
 
             else if(!input (row1))
 
               row=1;
 
             else if(!input (row2))
 
               row=2;
 
             else if(!input (row3))
 
               row=3;
 
             last_key =KEYS[row][col];
 
             kbd_down = true;
 
          } else {
 
             ++col;
 
             if(col==3)
 
               col=0;
 
          }
 
       }
 
      kbd_call_count=0;
 
   }
 
  return(kchar);
 
} | 	 
  | 
			 
		  | 
	
	
		  | 
	
	
		
			jruibarroso
 
 
  Joined: 07 Jan 2006 Posts: 64 Location: Braga 
			
			 
			 
			 
			
			
			
			 
			
			
  
		  | 
		
			
				| Anyone can modify this excellent flex code to 4x4 keyboard ? | 
			 
			
				 Posted: Thu Oct 05, 2006 7:03 am     | 
				     | 
			 
			
				
  | 
			 
			
				Anyone can modify this excellent flex code to 4x4 keyboard ? 
 
 
#device <All_Members>
 
#include <Thank_you> | 
			 
		  | 
	
	
		  | 
	
	
		
			PCM programmer
 
 
  Joined: 06 Sep 2003 Posts: 21708
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		 | 
	
	
		  | 
	
	
		
			jruibarroso
 
 
  Joined: 07 Jan 2006 Posts: 64 Location: Braga 
			
			 
			 
			 
			
			
			
			 
			
			
  
		  | 
		
			
				| Can you separate this code in "driver" code and &q | 
			 
			
				 Posted: Sun Oct 08, 2006 9:02 am     | 
				     | 
			 
			
				
  | 
			 
			
				Can you separate this code in "driver" code and "main" code 
 
 
Thanks | 
			 
		  | 
	
	
		  | 
	
	
		
			PCM programmer
 
 
  Joined: 06 Sep 2003 Posts: 21708
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sun Oct 08, 2006 10:27 am     | 
				     | 
			 
			
				
  | 
			 
			
				In the link given above, the driver is all the code between the two
 
dashed lines:
 
 
 	  | Code: | 	 		  
 
//=======================
 
 
// driver code is in here.
 
 
//=======================
 
 | 	  
 
 
The main() code is above and below the dashed lines. | 
			 
		  | 
	
	
		  | 
	
	
		
			40inD
 
 
  Joined: 30 Jul 2007 Posts: 112 Location: Moscow, Russia 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Wed Aug 01, 2007 3:07 am     | 
				     | 
			 
			
				
  | 
			 
			
				| I suggest to output_float the "row" pins in the end of scan procedure if you use this driver in interrupt routine. In some cases interrupt works only 1 time w/out this. | 
			 
		  | 
	
	
		  | 
	
	
		
			ankur_rustagi12
 
 
  Joined: 29 Mar 2009 Posts: 3
  
			
			 
			 
			 
			
			
			
			
			 
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri Apr 03, 2009 6:31 am     | 
				     | 
			 
			
				
  | 
			 
			
				I changed the pin config to B0 to B6
 
 
but when i am running the above programme it is showing the following
 
errors:
 
 
Warning [361] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 27.5 function declared implicit int
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 27.12 undefined identifier "PIN_C3"
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 27.29 undefined identifier "PIN_C4"
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 27.46 undefined identifier "PIN_C5"
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 27.63 undefined identifier "PIN_C6"
 
Warning [361] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 47.10 function declared implicit int
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 47.21 undefined identifier "PIN_C0"
 
Warning [361] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 48.1 function declared implicit int
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 48.13 undefined identifier "PIN_C1"
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 49.13 undefined identifier "PIN_C2"
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 63.10 undefined identifier "false"
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 69.12 undefined identifier "PIN_C3"
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 71.17 undefined identifier "PIN_C4"
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 73.17 undefined identifier "PIN_C5"
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 75.17 undefined identifier "PIN_C6"
 
Error   [192] C:\Documents and Settings\Ankur\Desktop\Ankur\pic\keypad.c; 78.12 undefined identifier "true"
 
 
 
help me out... | 
			 
		  | 
	
	
		  | 
	
	
		
			PCM programmer
 
 
  Joined: 06 Sep 2003 Posts: 21708
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri Apr 03, 2009 11:44 am     | 
				     | 
			 
			
				
  | 
			 
			
				You're using the Hi-Tech C compiler.  Code on this forum will not work
 
with that compiler. Please go to the Hi-Tech forum and use their sample code:
 
http://forum.htsoft.com/all/ubbthreads.php | 
			 
		  | 
	
	
		  | 
	
	
		
			ankur_rustagi12
 
 
  Joined: 29 Mar 2009 Posts: 3
  
			
			 
			 
			 
			
			
			
			
			 
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri Apr 03, 2009 11:03 pm     | 
				     | 
			 
			
				
  | 
			 
			
				yup..that may be the reason..
 
So if i uninstall the hi-tech compiler and install ccs compiler and compile in mplab ide environment...then will this program work???...will it work??? | 
			 
		  | 
	
	
		  | 
	
	
		
			Scythe
 
 
  Joined: 05 Mar 2010 Posts: 5
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri May 28, 2010 11:31 am     | 
				     | 
			 
			
				
  | 
			 
			
				| I need to ask something. I read a lot about this code but I understand if you use this library with port_b_pullups(true), you don't need additional resistors for pull up the rows. You can directly connect rows and cols to b port.  Am I wrong ? | 
			 
		  | 
	
	
		  | 
	
	
		
			PCM programmer
 
 
  Joined: 06 Sep 2003 Posts: 21708
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri May 28, 2010 12:08 pm     | 
				     | 
			 
			
				
  | 
			 
			
				| That's correct. | 
			 
		  | 
	
	
		  | 
	
	
		
			GabrielVinicios
 
 
  Joined: 27 May 2011 Posts: 2
  
			
			 
			 
			
			
			
			
			 
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri Sep 13, 2013 5:28 pm     | 
				     | 
			 
			
				
  | 
			 
			
				My library for doing this follow below:
 
 
 	  | Code: | 	 		  
 
/*    gv_teclado.c
 
My library can be used in keypads 3x4 with those numbers:
 
 
1 2 3
 
4 5 6
 
7 8 9
 
* 0 #
 
But you can change those characters the way you want.
 
 
EXEMPLE OF USE:
 
 
char c;
 
do{
 
c=le_teclado();
 
}while (c=='\0');
 
 
The function returns '\0' when no key is pressed.
 
 
*/
 
 
#define COL1 PIN_B0
 
#define COL2 PIN_B1
 
#define COL3 PIN_B2
 
#define LIN1 PIN_B3
 
#define LIN2 PIN_B4
 
#define LIN3 PIN_B5
 
#define LIN4 PIN_B6
 
 
char le_teclado(){
 
//seleciona coluna1
 
output_high(COL1);
 
output_low(COL2);
 
output_low(COL3);
 
 
if (input(LIN1)==1){
 
   return '1';
 
}
 
 
if (input(LIN2)==1){
 
   return '4';
 
}
 
 
 
if (input(LIN3)==1){
 
   return '7';
 
}
 
 
if (input(LIN4)==1){
 
   return '*';
 
}
 
 
 
 
 
//seleciona coluna2
 
output_low(COL1);
 
output_high(COL2);
 
output_low(COL3);
 
 
if (input(LIN1)==1){
 
   return '2';
 
}
 
 
if (input(LIN2)==1){
 
   return '5';
 
}
 
 
 
if (input(LIN3)==1){
 
   return '8';
 
}
 
 
if (input(LIN4)==1){
 
   return '0';
 
}
 
 
//seleciona coluna3
 
output_low(COL1);
 
output_low(COL2);
 
output_high(COL3);
 
 
if (input(LIN1)==1){
 
   return '3';
 
}
 
 
if (input(LIN2)==1){
 
   return '6';
 
}
 
 
 
if (input(LIN3)==1){
 
   return '9';
 
}
 
 
if (input(LIN4)==1){
 
   return '#';
 
}
 
 
return '\0';
 
 
 
}
 
 | 	 
  | 
			 
		  | 
	
	
		  | 
	
	
		
			criss_m
 
 
  Joined: 09 Mar 2006 Posts: 9 Location: Chile 
			
			 
			 
			
			
			
			
			 
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Tue May 20, 2014 3:02 am     | 
				     | 
			 
			
				
  | 
			 
			
				Its very strange:
 
 
i am trying to use the following pins in a SOIC 18F258:
 
i use flex LCD in same pins (plus PINC7)
 
 
#define COL1 PIN_A0 
 
#define COL2 PIN_A1 
 
#define COL3 PIN_A2
 
 
 
#define LIN1 PIN_C3 
 
#define LIN2 PIN_C4 
 
#define LIN3 PIN_C5 
 
#define LIN4 PIN_C6 
 
 
LCD seems to work fine , but keypad does not work ...
 
I tried putting pull up resistors in the rows ... but no luck either...
 
 
Any ideas  ??? | 
			 
		  | 
	
	
		  | 
	
	
		 |