4x4 Key pad Help please

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
jnk
Posts: 73
Joined: 05 Apr 2007 06:56
Location: India

4x4 Key pad Help please

#1 Post by jnk » 29 Apr 2009 18:07

Hi
I am trying a circuit with 4x4 keypad with 16F886.
For testing the keypad, the code is such that when the key is pressed, its value is displayed. if no key is pressed, the value displayed is 255. unfortunately the value displayed is either 255 or 10.

The circuit is as follows

Image
The code is as follows

Code: Select all

#include <built_in.h>


sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D4 at RC4_bit;

// Pin direction
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D7_Direction at TRISC7_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC4_bit;

const char *msgDelayTime     = "DelayTime:      ms  ";
const char *msgAuto          = "Auto Inc :      ms  ";
const char *msgSelect        = " <SELECT FUNCTION>  ";

unsigned int delayTime;
unsigned int ReadData;
unsigned short autoData;
char Text[21];
char  keypadPort at PORTB;

// --- Copying strings from ROM to RAM
void strConstCpy(char *dest, const char *source)
 {
 while(*source)
 *dest++ = *source++ ;
 *dest = 0 ;
 }

 void WriteDelaytime ()
 {
   char txt[7];
   WordToStr (delaytime,txt);
   LCD_Out (2,11, txt);
 }
 void WrAutoData()
 {
   char txt[4];
   Lcd_cmd (160);
   ByteToStr (autoData,txt);
   LCD_Out_cp (txt);
  }
  // Look for key input and return value as per key pressed
 unsigned short Key_in ()
 {
    unsigned short keyval ;
    keyval = Keypad_Key_Press();
   switch (keyval) {
      case  0: keyval = 255; break; // No key pressed
      case  1: keyval = 1; break; // 1
      case  2: keyval = 2; break; // 2
      case  3: keyval = 3; break; // 3
      case  4: keyval = 10; break; // A
      case  5: keyval = 4; break; // 4
      case  6: keyval = 5; break; // 5
      case  7: keyval = 6; break; // 6
      case  8: keyval = 11; break; // B
      case  9: keyval = 7; break; // 7
      case 10: keyval = 8; break; // 8
      case 11: keyval = 9; break; // 9
      case 12: keyval = 12; break; // C
      case 13: keyval = 13; break; // *
      case 14: keyval = 0; break; // 0
      case 15: keyval = 14; break; // #
      case 16: keyval = 15; break; // D
     }
 return keyval;
 }


void dataScreen ()
{
   LCD_Cmd (_LCD_CLEAR);
   LCD_Cmd(_LCD_CURSOR_OFF);
   WriteDelaytime ()   ;
  strConstCpy(text, msgDelayTime) ;
   LCD_Out(2,1, Text);
   strConstCpy(text, msgAuto) ;
   LCD_CMD(148)    ;
   LCD_Out_cp(Text);
   strConstCpy(text, msgSelect) ;
   LCD_CMD(212)    ;
   LCD_Out_cp(Text);
}


void main()
{
  osccon = 0x71;
  INTCON = 0x40;
  porta = 0x00;
  portb = 0x00;
  trisa = 0xFF;
  anselh = 0x00;
  wpub = 0x00;
  trisc = 0x00;
  option_reg = 0xC0;
  t1con = 0x30;
  porta.f4 = 0x01;

  LCD_init();                // initialize  (4-bit interface connection)
  Keypad_Init ();
  portc.f1 = 1;
  DelayTime = 9000;
  autoData = 100;
  dataScreen ();
  wrAutodata ();
  WriteDelayTime();
  delay_ms (5000);
  while (1)
  {
  autoData = Key_in ();
  WrAutoData ();
  delay_ms (500);
  }

  }
Please tell me where i am wrong. I feel that I am doing a simple mistake. Checked the keypad wiring & there is no shorting anywhere.

Please help
[b]Thanks...!!
jnk[/b]

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

#2 Post by Mince-n-Tatties » 29 Apr 2009 20:38

in your schematic R4, 5, 6 are pulling down cols. R7 is pulling down a row. eek... are you sure you want this? looks to me like the matrix is not set out correctly.

Looking at the mE layout, I think you should be only pulling rows down!

not the news you want to hear (unless the schematic does not match the PCB) but i hope it helps anyway.

Stefan Uhlemayr
Posts: 158
Joined: 17 Jan 2009 21:33

#3 Post by Stefan Uhlemayr » 29 Apr 2009 21:16

Mince-n-Tatties wrote:...
Looking at the mE layout, I think you should be only pulling rows down!...
I second your found problem with the keypad-schematic, but in the mP PRO - help there are the cols pulled down (I think, the libraries are the same for Pascal and C) - but they have to be placed at the port-pins 0..3, while the rows are placed at the port-pins 4..7.

Greetings,
Stefan

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

#4 Post by Mince-n-Tatties » 29 Apr 2009 23:12

Stefan Uhlemayr wrote:but they have to be placed at the port-pins 0..3, while the rows are placed at the port-pins 4..7.
yes i am sure either row or col could be pull-down but obviously not mixed.

i actually dont use the mE driver as i wrote my own to cope with multiple keypress detection

jnk
Posts: 73
Joined: 05 Apr 2007 06:56
Location: India

#5 Post by jnk » 30 Apr 2009 05:19

Mince-n-Tatties wrote:in your schematic R4, 5, 6 are pulling down cols. R7 is pulling down a row. eek... are you sure you want this? looks to me like the matrix is not set out correctly.

Looking at the mE layout, I think you should be only pulling rows down!

not the news you want to hear (unless the schematic does not match the PCB) but i hope it helps anyway.
Stefan Uhlemayr wrote: I second your found problem with the keypad-schematic, but in the mP PRO - help there are the cols pulled down (I think, the libraries are the same for Pascal and C) - but they have to be placed at the port-pins 0..3, while the rows are placed at the port-pins 4..7.
Thank you for the replies.
The Schematic was wrong since it was made for posting on the forum.
The original PCB is as per mikroC and the pull down resistors on RB4 to RB7 were added to the PCB externally.
what was overlooked was that the rows and colums are interchanged in mikroC PRO.

It was all aproblem of migrating the code to mikroC PRO :roll:
Let me try it out again after doing all that modifications on PCB.

Thanks again.
:D
[b]Thanks...!!
jnk[/b]

jnk
Posts: 73
Joined: 05 Apr 2007 06:56
Location: India

#6 Post by jnk » 30 Apr 2009 07:07

:(
I tried after changing the connections. Unfortunately it is not working. It is showing either 255 or 10.
Hardware is all checked for continuity & for any shorts.
I think I am doing some mistake in my code.

please help

regards
[b]Thanks...!!
jnk[/b]

Stefan Uhlemayr
Posts: 158
Joined: 17 Jan 2009 21:33

#7 Post by Stefan Uhlemayr » 30 Apr 2009 11:16

jnk wrote:... The original PCB is as per mikroC and the pull down resistors on RB4 to RB7 were added to the PCB externally. ...
Hm, though haven't used this keypad-routine up to now, I'm sure, that you will need the pull-downs at RB0 to RB3. After having had a short look to the compiled code of this routine it looks like (I hope, I haven't overlooked something), that RB0 - RB3 are the inputs for reading in the keys, which are pressed. The keypad-routine puts out a high at RB4 - RB7 and looks, which col (RB0-RB3) is getting high then (depending on the pressed key). If no key is pressed, you will need the external pull-down-resistor to read in the low-signal. The resistors on RB4 - RB7 might not disturb this routine, but they are of no help for this problem...

Hope this helps,
Stefan

jnk
Posts: 73
Joined: 05 Apr 2007 06:56
Location: India

#8 Post by jnk » 30 Apr 2009 11:54

Stefan Uhlemayr wrote: I'm sure, that you will need the pull-downs at RB0 to RB3. After having had a short look to the compiled code of this routine it looks like (I hope, I haven't overlooked something), that RB0 - RB3 are the inputs for reading in the keys, which are pressed. The keypad-routine puts out a high at RB4 - RB7 and looks, which col (RB0-RB3) is getting high then (depending on the pressed key). If no key is pressed, you will need the external pull-down-resistor to read in the low-signal. The resistors on RB4 - RB7 might not disturb this routine, but they are of no help for this problem...

Hope this helps,
Stefan
Yes. It helped. shifting the pull down resistors to RB0-RB3 solved all problems
May be it should be posted as a correction in the mikroC PRO Help

Thank you Stefan. Thank you. :D :D

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

#9 Post by Mince-n-Tatties » 30 Apr 2009 12:31

Stefan Uhlemayr wrote:
jnk wrote:... The original PCB is as per mikroC and the pull down resistors on RB4 to RB7 were added to the PCB externally. ...
Hm, though haven't used this keypad-routine up to now, I'm sure, that you will need the pull-downs at RB0 to RB3. After having had a short look to the compiled code of this routine it looks like (I hope, I haven't overlooked something), that RB0 - RB3 are the inputs for reading in the keys, which are pressed. The keypad-routine puts out a high at RB4 - RB7 and looks, which col (RB0-RB3) is getting high then (depending on the pressed key). If no key is pressed, you will need the external pull-down-resistor to read in the low-signal. The resistors on RB4 - RB7 might not disturb this routine, but they are of no help for this problem...

Hope this helps,
Stefan
i had just got the hardware setup as per the example and came to the same finding while running the mE example.

it does work with all 8 bits having pull-downs asserted although i guess it would mean a rise in current consumption.

Stefan Uhlemayr
Posts: 158
Joined: 17 Jan 2009 21:33

#10 Post by Stefan Uhlemayr » 30 Apr 2009 22:24

jnk wrote: Yes. It helped. shifting the pull down resistors to RB0-RB3 solved all problems
May be it should be posted as a correction in the mikroC PRO Help

Thank you Stefan. Thank you. :D :D
No problem, you're welcome. :D

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

#11 Post by filip » 05 May 2009 07:35

Hi jnk,

It will be fixed.

Regards,
Filip.

Post Reply

Return to “mikroC PRO for PIC General”