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 with Keypad program

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



Joined: 09 Aug 2007
Posts: 82
Location: TN, India

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

Problem with Keypad program
PostPosted: Mon Jun 22, 2009 10:35 am     Reply with quote

Dear CCS friends,

I modified the sample keypad code from ccs forum for my new project. My problem, is when I press any key on keypad I don't get any keylayout data. I only received below result during key press and unpress time on rs232 monitor.
Quote:

key=
key=
.
.
.
.

I'm using 18LF452 microcontroller for this project and operating voltage is 3.3 Volt.

Please check my code and give me some idea for solve the problem and please check if fuse is correct for PIC18LF452.
Code:

#include <18F452.h>
#device adc=8
#include <stdlib.h>
#include <math.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES XT                       //High speed Osc (> 4mhz)
#FUSES PROTECT                  //Code protected from reads
#FUSES NOOSCSEN                 //Oscillator switching is disabled, main oscillator is source
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES PUT                      //Power Up Timer
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES LVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES WRT                      //Program Memory Write Protected
#FUSES WRTD                     //Data EEPROM write protected
#FUSES WRTB                     //Boot block write protected
#FUSES WRTC                     //configuration registers write protected
#FUSES CPD                      //Data EEPROM Code Protected
#FUSES CPB                      //Boot Block Code Protected
#FUSES EBTR                     //Memory protected from table reads
#FUSES EBTRB                    //Boot block protected from table reads

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

//Keypad connection:
#define row0 PIN_B0
#define row1 PIN_B1
#define row2 PIN_B2
#define row3 PIN_B3
#define row4 PIN_B4
#define row5 PIN_B5

#define col0 PIN_A1
#define col1 PIN_A2
#define col2 PIN_A3
#define col3 PIN_A4
#define col4 PIN_A5

//#byte port_b=7

// Keypad layout:
char const KEYS[6][5] =
{{'1','2','3','4','5'},  // 5 4 3 2 1
 {'6','7','8','9','0'},  // 0 9 8 7 6
 {'A','B','C','D','E'},  // E D C B A
 {'F','G','H','I','J'},  // J I H G F
 {'K','L','M','N','O'},  // O N M L K
 {'P','Q','R','S','T'}}; // T S R Q P

//////////////////////////////////////6 x 5 Key Pad/////////////////////////////
#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_b(0xFF);
//output_b(0xFF);
 
port_b_pullups(true);
}

short int ALL_ROWS (void)
{
if(input (row0) & input (row1) & input (row2) & input (row3) & input (row4) & input (row5))
   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);
        output_high(col4);
        break;
   
      case 1:
        output_high(col0);
        output_low(col1);
        output_high(col2);
        output_high(col3);
        output_high(col4);
        break;

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

      case 3:
        output_high(col0);
        output_high(col1);
        output_high(col2);
        output_low(col3);
        output_high(col4);
        break;
       
     case 4:
        output_high(col0);
        output_high(col1);
        output_high(col2);
        output_high(col3);
        output_low(col4);
        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;
         else if(!input (row4))
            row=4;
         else if(!input (row5))
            row=5;

         last_key =KEYS[row][col];
         kbd_down = true;
        }
      else
        {
         ++col;
         if(col==5)
            col=0;
        }
     }
   kbd_call_count=0;
  }
return(kchar);
}
///////////////////////////////////////////////////////////////////////////////


void main()
{

char key;

kbd_init();

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   disable_interrupts(GLOBAL);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab

SET_TRIS_C( 0x80 );
SET_TRIS_D( 0x00 );

   printf("start...."); //print in rs232

   while(True)
   {
   key=kbd_getc();
   printf("key=%c\n\r",key);
     
  }// while

}
Crying or Very sad
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 22, 2009 11:36 am     Reply with quote

Quote:
while(True)
{
key=kbd_getc();
printf("key=%c\n\r",key);

}// while

This is not the correct way to call the keypad driver. Look at
how CCS does it in this example file. You should do it the same way:
Quote:
c:\Program Files\PICC\Examples\Ex_lcdkb.c
karthickiw



Joined: 09 Aug 2007
Posts: 82
Location: TN, India

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

Thank you PCM
PostPosted: Mon Jun 22, 2009 9:08 pm     Reply with quote

Dear PCM,

Thank you for helping us. I try as per example program, it works good. Thank you very Much.

But I don't know why print key variable directly in this program?

My fuse is correct for 18LF452 controller?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 22, 2009 9:51 pm     Reply with quote

Quote:
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)

This should be changed to NOLVP.


Quote:
#FUSES WRT //Program Memory Write Protected
#FUSES WRTD //Data EEPROM write protected
#FUSES WRTB //Boot block write protected
#FUSES WRTC //configuration registers write protected
#FUSES CPD //Data EEPROM Code Protected
#FUSES CPB //Boot Block Code Protected
#FUSES EBTR //Memory protected from table reads
#FUSES EBTRB

You don't need to protect memory during program development.
Delete these fuses.
karthickiw



Joined: 09 Aug 2007
Posts: 82
Location: TN, India

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

thank you
PostPosted: Tue Jun 23, 2009 6:28 am     Reply with quote

Thank you PCM Programmer Smile
brianm



Joined: 08 Mar 2008
Posts: 17

View user's profile Send private message

explain the code -- wiring of a keypad
PostPosted: Sun Jun 28, 2009 10:26 pm     Reply with quote

I am going to use a 4 by 4 keypad and the 16f690 MPU.

Can someone explain what happens when a key is pushed based on the lcdkbd.c example.

The keypad has 8 pin outs and I will hook them to the A and B groups.

Now realizing that some pins don't have pullups I will make them all high via current limiting resistors.

Why is it that pushing a key will do anything when both the mpu pins are still high? Having trouble getting my head around this one.

Thanks so much.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 28, 2009 10:50 pm     Reply with quote

Read my long post in this thread which explains how the keypad driver
works:
http://www.ccsinfo.com/forum/viewtopic.php?t=24443
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