Keypad 3x4 with MikroC !!!

General discussion on mikroC.
Post Reply
Author
Message
antoine_z_h
Posts: 4
Joined: 16 Dec 2008 20:58

Keypad 3x4 with MikroC !!!

#1 Post by antoine_z_h » 27 Nov 2009 11:27

Hi, I wanted to know how to make the following code works with 3x4 keypad!
I mean how do I connect my 3x4 keypad to work with the following code ? since the supplied schematic with this code is for 4x4 keypads only.

Here is the code :

Code: Select all

unsigned short kp, cnt;
char txt[5];

void main() {
  cnt = 0;
  Keypad_Init(&PORTC);
  Lcd_Init(&PORTB);         // Initialize LCD on PORTC
  Lcd_Cmd(LCD_CLEAR);       // Clear display
  Lcd_Cmd(LCD_CURSOR_OFF);  // Cursor off

  Lcd_Out(1, 1, "Key  :");
  Lcd_Out(2, 1, "Times:");

  do {
    kp = 0;

    //--- Wait for key to be pressed
    do
      //--- un-comment one of the keypad reading functions
      kp = Keypad_Released();
      //kp = Keypad_Read();
    while (!kp);

    cnt++;

    //--- prepare value for output
    if (kp > 10)
      kp += 54;
    else
      kp += 47;

    //--- print it on LCD
    Lcd_Chr(1, 10, kp);
    WordToStr(cnt, txt);
    Lcd_Out(2, 10, txt);

  } while (1);
}//
~!
The Example circuit for 4x4 keypad :

Image

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#2 Post by anikolic » 01 Dec 2009 10:57

Hi,
You can use our current library for this, with certain adjustments in the decoding of input data:

Code: Select all

    switch (kp) {                     // Prepare value for output
      case  1: kp = '1'; break;
      case  2: kp = '2'; break;
      case  3: kp = '3'; break;
      case  5: kp = '4'; break;
      case  6: kp = '5'; break;
      case  7: kp = '6'; break;
      case  9: kp = '7'; break;
      case 10: kp = '8'; break;
      case 11: kp = '9'; break;
      case 13: kp = '*'; break;
      case 14: kp = '0'; break;
      case 15: kp = '#'; break;
      default: kp = 0; 
  }
As for schematics, you could just use the same connections, but leave out RB7 line which is connected to A B C and D buttons and therefore reduce the size of the keypad. Only thing is that this RB7 line cannot be used for any other purpose, because library scans it nevertheless.

Best regards,
Aleksandar
Web Department Manager

scaffold
Posts: 14
Joined: 22 Nov 2009 03:42

#3 Post by scaffold » 05 Jan 2010 18:55

About that, may i ask something. I've been trying do my project almost 1 mounth. My project is like, mobile keypad. I got 4x4 keypad and i want it to be like, when i press the number like 2, it would show A, in 3 seconds, if i press it again, it would show on Lcd, respectively, B. i got too many ways, but never get the right one. So, could anyone please help me on that..

Thank you...

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#4 Post by anikolic » 06 Jan 2010 16:00

Hi,
Please take a look at this post: http://www.mikroe.com/forum/viewtopic.p ... 966#113966

Aleksandar
Web Department Manager

scaffold
Posts: 14
Joined: 22 Nov 2009 03:42

#5 Post by scaffold » 07 Jan 2010 03:46

should it be like this??


#define DELAY_FOR_NEXT 1000 // Delay before orginal Key is returnd time depends on how foten the "Call_Me_Every_1ms()" rutine is called

// Key_list[number of keys][number of characters/key]
// if you whant to have more characters on some keys then use a null on empty spots like below
//const unsigned char Key_list[9][6]={{'A','B','C','1',!,¤},{'D','E',0,0,'F','2',%,0},{'G','H','I','3','<','>'},{'J','K','L','4',0,0},{'M','N','O','5',0,0},{'P','Q','R','6',0,0},{'S','T','U','7',0,0},{'V','W','X','8',0,0},{'Y','Z','9',' ',0,0}};

const unsigned char Key_list[9][4]={{'A','B','C','1'},{'D','E','F','2'},{'G','H','I','3'},{'J','K','L','4'},{'M','N','O','5'},{'P','Q','R','6'},{'S','T','U','7'},{'V','W','X','8'},{'Y','Z','9',' '}};
unsigned short Delay_Timer= 0; // Timer for delay
unsigned char NewKey= 0; // use this as a way to see if the value is stepped or a new Key were pressed

void Call_Me_Every_1ms(){ // call this rutine at a steddy pase, if not value will step every time the DoKey() rutine is called whit the same value
if( Delay_Timer < 0xffff ) Delay_Timer++;
}

unsigned char DoKey(unsigned char Key) { // call this rutine with the Key pressed "as a character 0-9" and it returnes the value to display if called whit any other it returns null
static unsigned char step, last; // local

if( Key < '0' || Key > '9' ) return 0; // if not a valid value return null
if( Key == last && (Delay_Timer < DELAY_FOR_NEXT) ) { step++; NewKey= 1; } else { NewKey= 0; last= Key; step= 0; Delay_Timer= 0; } // check if same Key and the amount of time is less then time before next Key should be displayd
if( (step >= sizeof(Key_list[0])) || (Key_list[Key-'0'][step] == 0 ) ) step= 0; // if step is bigger than amount of chars in list or if null go to beginning of list

return Key_list[Key-'0'][step]; // return the value for the Key as a character
}

unsigned char tstkey= 0; // only for test of this
unsigned char display_char; // only for test of this

void main( void )
{
Keypad_Init(&PORTB);
Lcd_Init(&PORTD);
Lcd_Cmd(LCD_CLEAR);
Lcd_Cmd(LCD_CURSOR_OFF);
do{
display_char= DoKey('1');
if( NewKey ) // this is a check if display should move to new position and desplay character, remove if not needed
display_char;
else
display_char;
Call_Me_Every_1ms(); // update timer
Delay_ms(1);
}while(1);
}

Rk17
Posts: 3
Joined: 24 Feb 2012 18:38

Re: Keypad 3x4 with MikroC !!!

#6 Post by Rk17 » 24 Feb 2012 19:23

sir please take a look at this. The training module im using has the following schematic.

it uses pull up resistors instead of pull down. :( is this ok??
my program wont work either, please help me. i need it for our project tnx a lot and more power!
Attachments
keypad.JPG
keypad.JPG (70.16 KiB) Viewed 8287 times

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Keypad 3x4 with MikroC !!!

#7 Post by filip » 27 Feb 2012 11:01

Hi,

Please, take a look at this manual for the proper Keypad schematics :
http://www.mikroe.com/eng/downloads/get ... l_v100.pdf

Regards,
Filip.

Post Reply

Return to “mikroC General”