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 support@ccsinfo.com

about kbd

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



Joined: 08 Sep 2008
Posts: 4

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

about kbd
PostPosted: Tue Oct 07, 2008 10:44 pm     Reply with quote

from this code
Quote:
///////////////////////////////////////////////////////////////////////////
//// 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

#include <18f4550.h>
#use delay(clock=20000000)
#fuses NOLVP,HS,.....

//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 COL3 (1 << 8)

#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|COL3)

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

#define KBD_DEBOUNCE_FACTOR 33

void kbd_init()
{
//set_tris_b(0xF0);
//output_b(0xF0);
port_b_pullups(true);
}

short int ROW_HIGH()
{
if(input (ROW0) || input (ROW1) || input (ROW2) || input (ROW3))
return (1);
else
{
return (0);
}
}

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_high(col0);
output_low(col1);
output_low(col2);
output_low(col3);
break;

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

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

case 3:
output_low(col0);
output_low(col1);
output_low(col2);
output_high(col3);
break;
}

if(kbd_down)
{
if(!ROW_HIGH())
{
kbd_down=false;
kchar=last_key;
last_key='\0';
}
}
else
{
if(ROW_HIGH())
{
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==4)
col=0;
}
}
kbd_call_count=0;
}
return(kchar);
}

void main(){


unsigned char k;

while(1)
{
k=kbd_getc();
if(k!=0)
{
if(k=='*')
output_high(PIN_B0);
else
output_low(PIN_B0);
}
}
}




if i want to keep value from this code for equation such as

55 (kg) = X for (X * X)/8

but this code can keep just last one

may be i do something wrong ?

thanks for you help..
_________________
11235813
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 07, 2008 11:33 pm     Reply with quote

It looks like you got that code from this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=28022&start=14
Then you modified it a little, and tacked the CCS copyright message
onto the front of it. This makes it look like its a driver file that comes
with the CCS compiler, but that's not true.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 08, 2008 5:40 pm     Reply with quote

1. Put the incoming characters from the keypad into an array.
Make sure you don't write beyond the end of the array.

2. When the user presses the '#' key, this marks the end of the data.
Write a 0x00 to the end of the data in the array, when you get this key.
This will make the ASCII numbers into a string.

3. Now use the atoi() or atol() function to convert the numeric string
in the array into an integer. Those functions are in this file. You need
to #include it in your program:
Quote:
c:\program files\picc\drivers\stdlib.h
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