|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
helping me in coding 4x4 |
Posted: Sat Jul 19, 2008 8:50 pm |
|
|
Code: |
///////////////////////////////////////////////////////////////////////////
//// 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);
}
}
}
|
Is my main correct? I just want to try on a led when i press a key like '*'
but it cant work.
Please help. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Jul 21, 2008 8:29 am |
|
|
Quote: | No other use, reproduction or distribution is permitted without written permission. |
Remove the post of the copyrighted code. Everyone that has this compiler also has access to this file. Just reference it(#include it) don't show it. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Jul 21, 2008 8:56 am |
|
|
CCS Rev3.249 Pin D4 and D5 on PICDEM2 plus at 10Mhz
Code: | #include <16f877.h>
#device *=16
#use delay(clock=10000000)
#fuses hs,nowdt,noprotect,nolvp//we have a wdt now
#use rs232(baud=19200,xmit=PIN_E2,invert,stream=DEBUG,disable_ints)
#case
#zero_ram
#include "kbd.c"
unsigned char k;
void main()
{
fprintf(DEBUG,"start\n\r");
kbd_init();
while(1)
{
k=kbd_getc();
if(k=='*')
{
fprintf(DEBUG,"got it\n\r");
output_high(PIN_B0);
delay_ms(2000);
}
else
{
output_low(PIN_B0);
}
}
}
|
|
|
|
|
|
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
|