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

kbd.c on portb and porta

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







kbd.c on portb and porta
PostPosted: Fri Jul 01, 2005 11:12 am     Reply with quote

Hi,
How i can modify kbd.c to work with portB and portA?
(portB2 = COL1, portB3 = COL2, portB4 = COL3)
(portA0 = ROW1, portA1 = ROW2, portA2 = ROW3, portA3 = ROW4)

Thanks
jahan



Joined: 04 Apr 2005
Posts: 63

View user's profile Send private message Send e-mail

PostPosted: Fri Jul 01, 2005 3:16 pm     Reply with quote

as far as I can tell you should only change the following lines of code on the top part of kbd.c

Code:

// Un-comment the following define to use port B
// #define use_portb_kbd TRUE

// Make sure the port used has pull-up resistors (or the LCD) on
// the column pins


#if defined(__PCH__)
#if defined use_portb_kbd
   #byte kbd = 0xF81                   // This puts the entire structure
#else
   #byte kbd = 0xF83                   // This puts the entire structure
#endif
#else
#if defined use_portb_kbd
   #byte kbd = 6                  // on to port B (at address 6)
#else
   #byte kbd = 8                 // on to port D (at address 8)
#endif
#endif

#if defined use_portb_kbd
   #define set_tris_kbd(x) set_tris_b(x)
#else
   #define set_tris_kbd(x) set_tris_d(x)
#endif



//Keypad connection:   (for example column 0 is B2)
//                Bx:

#ifdef blue_keypad  /////// modify for your keypad
#define COL0 (1 << 2)
#define COL1 (1 << 3)
#define COL2 (1 << 6)

#define ROW0 (1 << 4)
#define ROW1 (1 << 7)
#define ROW2 (1 << 1)
#define ROW3 (1 << 5)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 03, 2005 10:34 pm     Reply with quote

Quote:
How i can modify kbd.c to work with portB and portA?
(portB2 = COL1, portB3 = COL2, portB4 = COL3)
(portA0 = ROW1, portA1 = ROW2, portA2 = ROW3, portA3 = ROW4)

Here is a modified version of the CCS keypad driver that
will work with the column and row pins on different ports.
The driver shown below will work with the pins specified
in your post.

This is a test program that #includes the new driver.
It displays its output on a terminal window.
Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP   
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <kbd1.c>

//========================
void main()
{
 int8 k;

 printf("Start\n\r");

 while(1)
   {
    k = kbd_getc();
   
    if(k != 0)
      {
       if(k == '*')
         {
          putc('\n');
          putc('\r');
         }
       else
         {
          putc(k);
         }
      }
   }

}


This is the driver that puts the ROW pins on Port A and the
column pins on Port B. Pull-up resistors are required on
the four row pins on Port A. (Use 10K ohm resistors).
Code:

// KBD1.C

#if defined(__PCH__)
#if defined use_portb_kbd
   #byte kbd_rows = 0xF80    // Port A on 18F452   
   #byte kbd_cols = 0xF81    // Port B on 18F452     
#endif
#else
   #byte kbd_rows = 5        // Port A on 16F877
   #byte kbd_cols = 6        // Port B on 16F877
#endif

#define set_tris_kbd_rows(x) set_tris_a(x)
#define set_tris_kbd_cols(x) set_tris_b(x)

#define Bitmask(x) (1 << (x & 7))

// These statements define the connections between the keypad
// pins and the PIC pins. For example, the column 0 pin on the
// keypad connector goes to pin B2 on the PIC.   If you change
// the wiring on your board, then you must also change the
// definitions below to match your board.  I have used the
// Bitmask() macro in the following statements because I think
// it makes the connections easier to understand, compared to
// the way CCS does it in their driver.
#define COL0  Bitmask(PIN_B2)
#define COL1  Bitmask(PIN_B3)
#define COL2  Bitmask(PIN_B4)

#define ROW0  Bitmask(PIN_A0)
#define ROW1  Bitmask(PIN_A1)
#define ROW2  Bitmask(PIN_A2)
#define ROW3  Bitmask(PIN_A3)


#define ALL_ROWS (ROW0|ROW1|ROW2|ROW3)
#define ALL_COLS (COL0|COL1|COL2)

// Keypad layout:
char const KEYS[4][3] = {{'1','2','3'},
                         {'4','5','6'},
                         {'7','8','9'},
                         {'*','0','#'}};

// Set this number to apx n/333 where
// n is the number of times you expect
// to call kbd_getc each second.
#define KBD_DEBOUNCE_FACTOR 33   

void kbd_init()
{
}


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   : set_tris_kbd_cols(ALL_COLS&~COL0);
                    kbd_cols=~COL0&ALL_COLS;
                    break;
         case 1   : set_tris_kbd_cols(ALL_COLS&~COL1);
                    kbd_cols=~COL1&ALL_COLS;
                    break;
         case 2   : set_tris_kbd_cols(ALL_COLS&~COL2);
                    kbd_cols=~COL2&ALL_COLS;
                    break;
       }

       if(kbd_down) {
         if((kbd_rows & (ALL_ROWS))==(ALL_ROWS)) {
           kbd_down=FALSE;
           kchar=last_key;
           last_key='\0';
         }
       } else {
          if((kbd_rows & (ALL_ROWS))!=(ALL_ROWS)) {
             if((kbd_rows & ROW0)==0)
               row=0;
             else if((kbd_rows & ROW1)==0)
               row=1;
             else if((kbd_rows & ROW2)==0)
               row=2;
             else if((kbd_rows & ROW3)==0)
               row=3;
             last_key =KEYS[row][col];
             kbd_down = TRUE;
          } else {
             ++col;
             if(col==3)
               col=0;
          }
       }
      kbd_call_count=0;
   }
  set_tris_kbd_rows(ALL_ROWS);
  set_tris_kbd_cols(ALL_COLS);

  return(kchar);
}
Guest








PostPosted: Mon Jul 04, 2005 12:56 pm     Reply with quote

Ok Wink ,
thanks PCM programmer
Vitoco.lr



Joined: 08 Jul 2017
Posts: 5

View user's profile Send private message

PostPosted: Tue Jul 11, 2017 9:53 pm     Reply with quote

Hey, I tried to use this code on my PIC16f877a with a matrix keypad 4x4, i made these modifications to the code but it is working really weird... I need some help on int, I'm using RB0 for interrupts so that's why I have the mask like it is.. Please

Sad

This are the pins I can use

Code:

#define COL0  Bitmask(PIN_B1)
#define COL1  Bitmask(PIN_B2)
#define COL2  Bitmask(PIN_B3)
#define COL3  Bitmask(PIN_B4)

#define ROW0  Bitmask(PIN_A2)
#define ROW1  Bitmask(PIN_A3)
#define ROW2  Bitmask(PIN_A4)
#define ROW3  Bitmask(PIN_A5)


I did this because is 4x4

Code:

#define ALL_ROWS (ROW0|ROW1|ROW2|ROW3)
#define ALL_COLS (COL0|COL1|COL2|COL3)

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


Code:

   if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
       switch (col) {
         case 0   : set_tris_kbd_cols(ALL_COLS&~COL0);
                    kbd_cols=~COL0&ALL_COLS;
                    break;
         case 1   : set_tris_kbd_cols(ALL_COLS&~COL1);
                    kbd_cols=~COL1&ALL_COLS;
                    break;
         case 2   : set_tris_kbd_cols(ALL_COLS&~COL2);
                    kbd_cols=~COL2&ALL_COLS;
                    break;
         case 3   : set_tris_kbd_cols(ALL_COLS&~COL3);
                    kbd_cols=~COL3&ALL_COLS;
                    break;
       }



And at last

Code:

else {
             ++col;
             if(col==4)
               col=0;
          }


But really I can't find where the mistake is... Please help...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 11, 2017 10:29 pm     Reply with quote

Do you have pull-ups on the row pins ? You need them. Each keypad row
pin should have this circuit on it:
Code:

               +5v
               |
               <
               > 10K       
               <         
               |
PIC pin -------o-------- Keypad pin
 
Vitoco.lr



Joined: 08 Jul 2017
Posts: 5

View user's profile Send private message

PostPosted: Tue Jul 11, 2017 11:59 pm     Reply with quote

Yes I have them! I ended up making it work... but now for somehow the interruption isn't working... is there any way of making the pins working for the matrix keypad more than the "entire" port B?, maybe there is a problem with that but to be honest I wouldn't know how to solve it. At this point that's the last thing I need to solve...
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jul 12, 2017 4:59 am     Reply with quote

You may want to look here...
http://www.ccsinfo.com/forum/viewtopic.php?t=26333&highlight=flexkbd

This is a 'flexible' version of the keyboad driver. I use it, it works, just be sure that the I/O pins you select can be used for inputs if used for rows and outputs if used for columns. Not all PIC pins can be used for ins and outs...

Jay
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