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

problem of 4x4 keypad

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



Joined: 14 Mar 2010
Posts: 21

View user's profile Send private message

problem of 4x4 keypad
PostPosted: Sun Mar 14, 2010 3:50 am     Reply with quote

I have built a 4x4 keypad code, using port B0-7 col1=pin_b0.
Anyone can tell me what's the problem? It doesn't work....
Code:

#include <18F4620.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock = 20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <LCD16x2.c>

#byte   TRISB= 0xF95
#byte   PORTB = 0xF83      //move the port d into address 0xF83
#bit   row1 = PORTB.4      //set the row connector to port d bits
#bit   row2 = PORTB.5
#bit   row3 = PORTB.6
#bit   row4 = PORTB.7

int8 key_scan(void);

int8 keypad();

int8 key_scan(void)
{
int8 key_array[17]={1,4,7,12,2,5,8,0,3,6,9,14,10,13,11,15,16}; //set the pattern of keypad
   int8 i,key_no=0,temp = 0xFE;
   TRISB= 0xF0;            //set rows connector to high on software
   for (i=0;i<4;i++)         //loop four times to check buttons
   {
      PORTB = temp;
      if (row1) key_no++; else goto keyexit;
      if (row2) key_no++; else goto keyexit;
      if (row3) key_no++; else goto keyexit;
      if (row4) key_no++; else goto keyexit;
      temp*=2;
      temp++;
   }
keyexit:
   return key_array[key_no];      //export a number
}

int8 keypad()
{
  int8 key;

   key=key_scan();
if (key==0) printf(lcd_putc,"Y");
if (key==1) printf(lcd_putc,"1");
if (key==2) printf(lcd_putc,"4");
if (key==3) printf(lcd_putc,"7");
if (key==4) printf(lcd_putc,"2");
if (key==5) printf(lcd_putc,"5");
if (key==6) printf(lcd_putc,"8");
if (key==7) printf(lcd_putc,"3");
if (key==8) printf(lcd_putc,"6");
if (key==9) printf(lcd_putc,"9");
if (key==10) printf(lcd_putc,"N");
if (key==11) printf(lcd_putc,"C");
if (key==12) printf(lcd_putc,".");
if (key==13) printf(lcd_putc,"0");
if (key==14) printf(lcd_putc,"/");
if (key==15) printf(lcd_putc,"D");
   delay_ms(50);
   while (key_scan()!=16);         //wait for leave pressing
   delay_ms(50);
   return key;
}


void main()
{
  int8 key;
   setup_psp (PSP_DISABLED);

   lcd_init();

   printf(lcd_putc,"Biometric attendance");

   printf(lcd_putc,"Welcome!!");
   delay_ms(1000);
 
   lcd_init();
   printf(lcd_putc,"1.123456\n");
   printf(lcd_putc,"2.abc");

   delay_ms(50);

   while(true)

   key=key_scan();
   if (key == 1) 
      { printf(lcd_putc,"xxxx");
        delay_ms(50);
        while (key_scan()!=16);
        delay_ms(50);

       }
   if (key == 2) 
      { printf(lcd_putc,"yyyy");
        delay_ms(50);
        while (key_scan()!=16);
        delay_ms(50);

      }
   else
   { printf(lcd_putc,"99999");}
   }   
}

my sch., always high??
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Mar 14, 2010 5:50 am     Reply with quote

How do you determine, that particularly the keypad is "not working"? All debug output goes to the LCD.

Is it a real circuit or a simulation?

Where did you connect the LCD?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 14, 2010 3:43 pm     Reply with quote

Try using the Flex 4x4 keypad driver:
http://www.ccsinfo.com/forum/viewtopic.php?t=28022&start=14
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Sun Mar 14, 2010 3:51 pm     Reply with quote

Though I agreee 'use the existing driver', The reason it doesn't work, is the maths of 'temp'.
You want this to pull each line low in turn.
Ignoring the top nibble, you have 1110 in it at the first loop. At the end of this loop, you multiply it by 2 -> 1101, then increment it -> 1110. Er.....
You only need to multiply it by two to move the '0' left.

Best Wishes
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