CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Interfacing 4x4 keypad to 2x16 hd44780LCD Driver

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Marq



Joined: 06 Oct 2009
Posts: 9

View user's profile Send private message

Interfacing 4x4 keypad to 2x16 hd44780LCD Driver
PostPosted: Tue Oct 06, 2009 7:23 am     Reply with quote

I am currently need to connect 4x4 kepad to the hd44780 lcd driver with 40 pin pic16f874A, and I'm using ccs c compiler. I am new to c programming, can someone guide me on the hardware connection of the keypad with 40 pin pic, and the coding to link the keypad to lcd driver. I am really appreciate for any advice given, thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 06, 2009 1:09 pm     Reply with quote

Use this driver for the LCD:
http://www.ccsinfo.com/forum/viewtopic.php?t=24661

Use this driver for the 4x4 keypad:
http://www.ccsinfo.com/forum/viewtopic.php?t=28022&start=14
Make sure that you put 10K pullups on the Row pins of the keypad.
Or, you can put them on Port B, and enable Port B pullups on the PIC.
Marq



Joined: 06 Oct 2009
Posts: 9

View user's profile Send private message

PostPosted: Wed Oct 07, 2009 3:11 am     Reply with quote

From the code below, it only define lcd pin4 to pin7 how about lcd pin0 to pin3 not using it?
Code:

#define LCD_DB4   PIN_D0
#define LCD_DB5   PIN_D1
#define LCD_DB6   PIN_D2
#define LCD_DB7   PIN_D3
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 07, 2009 12:15 pm     Reply with quote

Character LCDs are normally used in 4-pin interface mode.
The Flex driver uses that mode.
Marq



Joined: 06 Oct 2009
Posts: 9

View user's profile Send private message

PostPosted: Thu Oct 08, 2009 9:12 am     Reply with quote

1) I come across to your keypad driver coding and notice that the following:

void kbd_init()
{
//set_tris_b(0xF0);
//output_b(0xF0);
port_b_pullups(true);
}

Do we need to unchecked the set_tris_b and output_b for the portb connected keypad to work?
If the set_tris_b is unchecked, then pinB4 to pinB7 is treated as input and pinB0 to pinB3 as output, isn't that keypad is for input only?

2)Regarding the internal pull_up resistor at portB, if the internal pull_up is enabled then i just follow the below connection:

//Keypad connection:
#define row0 PIN_B4
#define row1 PIN_B5
#define row2 PIN_B6
#define row3 PIN_B7
#define col0 PIN_B0
#define col1 PIN_B1
#define col2 PIN_B2
#define col3 PIN_B3

Do i need any extra connection to ground for the cols of the keypad?

The picture below (internal pull-up) show one side of the the switch-button is connected to pic pin, whereas the other connected to ground. As i know ground is important here in order not to let the PIC pin float.



I think same thing should apply for the keypad where the rows is connected to PIC pinB4 to pinB7 and the cols connected to ground and PIC pinB0 to pinB3.

But from the coding above the keypad cols is connected to pinB0 to pinB3 only not to the ground, then will it result floating to PIC pinB0 to pinB3? Or it is okey to just connect the cols to PIC pinB0-B3?


3)If the pull-up at portB is not enabled, then i need to connect the pull-up resistors in series with the Vcc at one end, and at another end the pull-up resistors will connect in parallel to the PIC pins and cols of keypad. And for the cols of keypad do i need to connect to V ground other than the connection to Pin_B0 to Pin_B3?


Correct me if i am wrong, thanks.

4) I have come across that internal pull-up in port b is weak, is it so for the case of using it for the keypad purpose? Should i change it to external pull-up rather than using internal pull-up?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 08, 2009 10:39 am     Reply with quote

Look at this explanation of how a keypad is read by a micro-controller:
http://www.maxim-ic.com/appnotes.cfm/appnote_number/3414

This post has an explanation of the CCS keypad driver. It's not the
same as the flex keypad driver, but the concepts are similar:
http://www.ccsinfo.com/forum/viewtopic.php?t=24443&start=7
Marq



Joined: 06 Oct 2009
Posts: 9

View user's profile Send private message

PostPosted: Sat Oct 10, 2009 8:33 pm     Reply with quote

I have gone through the review that you have directed me to, it really help thanks.

But from your 4x4 keypad driver i still have some doubt on the following line:
Code:

void kbd_init()
{
//set_tris_b(0xF0);
//output_b(0xF0);
port_b_pullups(true); 
}

From the above are you trying to say if port_b is pull up enabled then we should comment out these lines
Code:

//set_tris_b(0xF0);
//output_b(0xF0);

If port_b is not pull-up enable then just un-comment the following lines:
Code:

set_tris_b(0xF0);
output_b(0xF0);

Am I correct ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 10, 2009 10:06 pm     Reply with quote

It's not my driver. It was written by Ahmed and he posted it in the
code library forum:
http://www.ccsinfo.com/forum/viewtopic.php?t=26333
Someone asked for it to be expanded to a 4x4 keypad, so I did that
in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=28022
Apparently, I fixed someone else's code. They attempted to write
a 4x4 driver. They had the TRIS statements in there. I commented
them out because they are incorrect and not needed.

You must have pull-up resistors on the Row pins. Either internal or
external pull-ups, but they must be there. Putting in the TRIS
statements won't allow you to leave out the pull-ups.
Marq



Joined: 06 Oct 2009
Posts: 9

View user's profile Send private message

PostPosted: Sun Oct 11, 2009 3:44 am     Reply with quote

If i intend to use this 4x4 keypad code and applied it to my PORT_D then i should comment out the followings:

//set_tris_b(0xF0);
//output_b(0xF0);
//port_b_pullups(true);

And i should applied pull_up on my PORT_D (either internal or external).
Is it so?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 11, 2009 11:00 am     Reply with quote

Yes that's correct.
santicomp
Guest







Keypad 4*4
PostPosted: Wed Oct 28, 2009 2:21 pm     Reply with quote

Hi I have problems with the internal weak pull ups. I am simulating the program in Proteus. Here is the code.
Code:

#include "keypad.h"
#include "keypad4.c"
#include "flex_lcd.c"
void main()
{
char k;

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
   
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab

   // TODO: USER CODE!!



kbd_init();
lcd_init();
lcd_putc("\f Starting");
delay_ms(2000);

while(TRUE)

  {
   k=kbd_getc();
   lcd_putc("\f ");
   
   if(k!=0)
   
        printf(lcd_putc,"\f Boton %c", k);
        delay_ms(500);
     }

  }


Code:

//Keypad connection:
#define row0 PIN_B4
#define row1 PIN_B5
#define row2 PIN_B6
#define row3 PIN_B7
#define col0 PIN_B0
#define col1 PIN_B1
#define col2 PIN_B2
#define col3 PIN_B3

// Keypad layout:
char const KEYS[4][4] =
{{'7','8','9','F'},
 {'4','5','6','E'},
 {'1','2','3','D'},
 {'0','A','B','C'}};


#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()
{
//set_tris_d(0x0F);
//output_b(0xF0);
port_b_pullups(true);

}

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);
        output_high(col3);
        break;
   
      case 1:
        output_high(col0);
        output_low(col1);
        output_high(col2);
        output_high(col3);
        break;

      case 2:
        output_high(col0);
        output_high(col1);
        output_low(col2);
        output_high(col3);
        break;

      case 3:
        output_high(col0);
        output_high(col1);
        output_high(col2);
        output_low(col3);
        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==4)
            col=0;
        }
     }
   kbd_call_count=0;
  }
return(kchar);
}

I don't understand how the pullup is connected to the keypad.
Do I have to define the columns in another port and define them
as inputs ?
Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 28, 2009 2:35 pm     Reply with quote

Quote:
void kbd_init()
{
port_b_pullups(true);
}

I have problems with the internal weak pull ups. I am simulating the
program in Proteus.

Do you know for certain that Proteus simulates the weak pull-ups on
Port B ? If you're not sure, then add external 10K pullups on the row
pins of the keypad to your Proteus schematic.
santicomp
Guest







keypad 4*4
PostPosted: Wed Oct 28, 2009 2:51 pm     Reply with quote

I tried to put the pullup in false and when I run in Proteus it goes to a tristate and when I activate the pullups it goes to read high. It looks like the internal pullups are working but if the column and the row are high when the button is pushed it should produce a short circuit. Do I need to put the columns in another port ?
Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 28, 2009 3:00 pm     Reply with quote

Quote:
Do I need to put the columns in another port ?

No, you don't.
takumi



Joined: 07 Mar 2009
Posts: 14

View user's profile Send private message

PostPosted: Fri Jul 30, 2010 9:22 pm     Reply with quote

//Keypad connection:
#define row0 PIN_D0 // default PIN_B4-PIN_B7
#define row1 PIN_D1
#define row2 PIN_D2
#define row3 PIN_D3
#define col0 PIN_B0
#define col1 PIN_B1
#define col2 PIN_B2
#define col3 PIN_B3

How about this. I have purchased a board where the keypad use diff configuration. How can I modify the code so there is no error to display the key to the lcd ? Please help.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group