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 4x3 problem

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



Joined: 01 Apr 2011
Posts: 17

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

Keypad 4x3 problem
PostPosted: Fri Dec 09, 2011 3:40 pm     Reply with quote

Hello!
I dont know why this code does not work ?
I use pic 16f877a +keypad (4x3)+lcd.
Nothing displayed on LCD. I use port B for LCD and Port D for Keypad.
Code:

#include "keypad2.h"
#include <KBD.C>
# use fast_io (D)
# use fast_io (b)

#define LCD_ENABLE_PIN PIN_B0
#define LCD_RS_PIN PIN_B1
#define LCD_RW_PIN PIN_B2
#define LCD_TYPE 1
#include <lcd.c>

void main()
{
   char c;
 
   lcd_init();
   kbd_init();
   lcd_putc("\fwaiting...\n");

  // port_b_pullups(TRUE);
   //setup_adc_ports(AN0);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   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);
   setup_vref(FALSE);
   
  // enable_interrupts(INT_TIMER0);
  // enable_interrupts(GLOBAL);

   // TODO: USER CODE!!
   while(TRUE)
  {
   c=kbd_getc();
   if(c!=0)
      if(c=='*')
         lcd_putc('\f');
      else
         lcd_gotoxy(1,2);
        // printf(lcd_putc,"%c",c);
           output_c(c);
   }     

}

the compiler version is 4.032
_________________
REPTILE404

55AA55AA55
temtronic



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

View user's profile Send private message

PostPosted: Fri Dec 09, 2011 4:00 pm     Reply with quote

You should say which PIC you're using as well as the hookup to the KPD,pullups,pulldowns,etc.
Does the LCD work without KPD OK ?
What's in the KPd header file ?
reptile404



Joined: 01 Apr 2011
Posts: 17

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

PostPosted: Sat Dec 10, 2011 7:29 am     Reply with quote

yes Lcd works pretty good , but keypad no
the KBD header is :

///////////////////////////////////////////////////////////////////////////
//// KBDD.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. ////
//// ////
///////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2003 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS C ////
//// compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, reproduction ////
//// or distribution is permitted without written permission. ////
//// Derivative programs created using this software in object code ////
//// form are not restricted in any way. ////
///////////////////////////////////////////////////////////////////////////

////////////////// The following defines the keypad layout on port D

// 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 ///////////////////////////////////// For the blue 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)

#else ////////////////////////////////////////////////// For the black keypad
#define COL0 (1 << 5)
#define COL1 (1 << 6)
#define COL2 (1 << 7)

#define ROW0 (1 << 1)
#define ROW1 (1 << 2)
#define ROW2 (1 << 3)
#define ROW3 (1 << 4)

#endif

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

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

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(ALL_PINS&~COL0);
kbd=~COL0&ALL_PINS;
break;
case 1 : set_tris_kbd(ALL_PINS&~COL1);
kbd=~COL1&ALL_PINS;
break;
case 2 : set_tris_kbd(ALL_PINS&~COL2);
kbd=~COL2&ALL_PINS;
break;
}

if(kbd_down) {
if((kbd & (ALL_ROWS))==(ALL_ROWS)) {
kbd_down=FALSE;
kchar=last_key;
last_key='\0';
}
} else {
if((kbd & (ALL_ROWS))!=(ALL_ROWS)) {
if((kbd & ROW0)==0)
row=0;
else if((kbd & ROW1)==0)
row=1;
else if((kbd & ROW2)==0)
row=2;
else if((kbd & 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(ALL_PINS);
return(kchar);
}
_________________
REPTILE404

55AA55AA55
temtronic



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

View user's profile Send private message

PostPosted: Sat Dec 10, 2011 7:55 am     Reply with quote

hmm...
#include "keypad2.h"
#include <KBD.C>

1) You've shown us the KBDD.C driver but use KBD.C

2) Do not know what's in keypad2.h

3) post the schematic of the project

4) are you're KPDs rows and columns correct? Pullup values ?
reptile404



Joined: 01 Apr 2011
Posts: 17

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

PostPosted: Sat Dec 10, 2011 8:24 am     Reply with quote

Well, you told me that I've shown you KBDD.c not KBD.c.
I used the driver that comes with the compiler which is kbd.c
and I've shown to you, here's the header (keypad.h):
Code:

#include <16F877A.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //Crystal osc <= 4mhz
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES WRT_50%                  //Lower half of Program Memory is Write Protected

#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

and here's the schematic :

_________________
REPTILE404

55AA55AA55
temtronic



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

View user's profile Send private message

PostPosted: Sat Dec 10, 2011 9:04 am     Reply with quote

Since the schematic is from ISIS, have to ask are you using the simulator or using real hardware ?

Have you confirmed with a meter or scope that the KPD wiring is correct ?

Looks like you should have a 'black ' keypad configuration ?

Your ISIS schematic is 'fuzzy' and small so I can't confirm the row/col/pin assignments.

Unless you have a 'black kpd' from CCS there's a chance yours is not configured the same way.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 11, 2011 5:02 pm     Reply with quote

Your keypad connections are incorrect, to both the PIC and the pullups.
Change your schematic to this:

PIC
pin -- Keypad pin
D0 -- N/C
D1 -- Row 0
D2 -- Row 1
D3 -- Row 2
D4 -- Row 3
D5 -- Col 0
D6 -- Col 1
D7 -- Col 2

Move the pullups so they are on the Row pins (not the column pins).
Each row pin must have a pullup.

The list of connections given above comes directly from here:
Code:

#else /////////////// For the black keypad
#define COL0 (1 << 5)
#define COL1 (1 << 6)
#define COL2 (1 << 7)

#define ROW0 (1 << 1)
#define ROW1 (1 << 2)
#define ROW2 (1 << 3)
#define ROW3 (1 << 4)
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