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

Keypad, LCD and 16F887

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



Joined: 19 Jun 2010
Posts: 3

View user's profile Send private message

Keypad, LCD and 16F887
PostPosted: Sun Jun 20, 2010 3:37 am     Reply with quote

Dear CCS Friend!

I copied source code of keypad driver from CCS forum for my project.
when i check LCD to display, that is ok. But I press button on keypad, I can not get any thing. Please help me and explain why is it. I am looking to hear from friends soon.
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.                        ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////

//The PCH compiler defines this pre-processor identifier.  It may be used to determine if the PCH compiler is doing the compilation.

//Keypad connection:

#define col0 PIN_B0
#define col1 PIN_B1
#define col2 PIN_B2
#define row0 PIN_B4
#define row1 PIN_B5
#define row2 PIN_B6
#define row3 PIN_B7

// 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() {
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);
                    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);
}



///------------------------------------
#include <16F887.H>
//#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use rs232(baud=9600)
#include <flex_lcd.c>
#define use_portb_kbd TRUE
#include <flex_kbd.c>

//========================
void main()
{
char k;
kbd_init();
lcd_init();
lcd_gotoxy(1,1);
printf(lcd_putc,"testing keypad");
delay_ms(2000);
while(TRUE)
    {
   k=kbd_getc();
   lcd_gotoxy(1,2);
   printf(lcd_putc,"Boton = %c", k);       
   delay_ms(500);
     }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sun Jun 20, 2010 4:20 am     Reply with quote

The point is that keys are not 'got', and waiting for you to read. You need to be _looping_ calling kbd_getc, _unitl_ it returns a key.
So:
Code:

void main() {
   char k;
   kbd_init();
   lcd_init();
   lcd_gotoxy(1,1);
   printf(lcd_putc,"testing keypad");
   delay_ms(2000);
   while(TRUE) {
      k=0;
      while (k==0) { //Loop waiting for the key
         k=kbd_getc();
         delay_ms(1);
      }
      lcd_gotoxy(1,2);
      printf(lcd_putc,"Boton = %c", k);
      //delay_ms(500); //No point in this, get back to scanning
      //the keys ASAP.
   }
}
vuphanhuan



Joined: 19 Jun 2010
Posts: 3

View user's profile Send private message

PostPosted: Sun Jun 20, 2010 5:11 am     Reply with quote

Dear Ttelmah!

I tried with looping calling kbd_getc(). so, the "Botom =" name don't display or k=kbd_getc() do not working. I use resistor 10k on B4, B5, B6, B7 and Vcc=5V.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 20, 2010 11:18 am     Reply with quote

Code:

#define row2 PIN_B6
#define row3 PIN_B7

Pins B6 and B7 are used by the ICD debugger/programmer. Do you
have the ICD plugged into the board ? The debugger has 4.7K pull-down
resistors inside it, on those signal lines. This will reduce the voltage
of your pull-up resistors to an inadequate level.

Un-plug the ICD debugger before testing your program.

Or better, don't use pins B6 and B7 for anything, if possible. Use two
different pins for the keypad.
vuphanhuan



Joined: 19 Jun 2010
Posts: 3

View user's profile Send private message

PostPosted: Mon Jun 21, 2010 3:34 am     Reply with quote

At moment I use to simulate by Proteus 7.5 and I changed row 2 = A6, row3 = A7. But it do not display what button pressed.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 21, 2010 12:43 pm     Reply with quote

Pins A6 and A7 are used for the oscillator. If you want to use them as
normal i/o pins for the LCD, then you need to use the INTRC_IO fuse.
Are you using that fuse ?

My advice is to download the 16F887 data sheet and look at the pin
diagram for the PIC, and choose some i/o pins that are not used by
http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf
Also look at the pin descriptions in this table. It will help you to decide
which pins to use:
Quote:
TABLE 1-2: PIC16F884/887 PINOUT DESCRIPTION
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