we had update library for keypad
def mikroc compiler built in working as:
tris_port = 0x0f;
new function working a :
tris_port = 0xf0;
you can use this function if there is lcd in the same port
_KEYPAD_C
/*
* Description:
* <library for working with 4x4 keypad; routines can also
* be used with 4x1, 4x2, or 4x3 keypad.>
* NOTES:
* < It supports keypads with 1 to 4 rows and 1 to 4 columns >
* Revision History:
* <1.0.0> -
* Author : a.a.b
* Test configuration:
Dev.Board: b.r.v
Ext. Modules: x
SW: mikroc compiler v6.2.1
*/
#ifndef _KEYPAD_C
#define _KEYPAD_C
#include "keypad.h"
//////////////////////////////////////////////////////////////////
#define _TRISTATE 0x80
#define _PORT_STATE 0xf0
//////////////////////////////////////////////////////////////////
char *tmp_keypad_pointer = 0;
//////////////////////////////////////////////////////////////////
// function def //
//////////////////////////////////////////////////////////////////
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ---++++++++++ void _Keypad_Init(char *port) +++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Arguments: <char *port> Returns: <void>
// NOTES: <Initializes port to work with keypad.
// The function needs to be called before using other routines
// from Keypad library.
// Requires : include header file keypad.h
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
void _Keypad_Init(char *port)
{
tmp_keypad_pointer = port;
port+= _TRISTATE;
*port = _PORT_STATE;
}
// END FUNCTION < _Keypad_Init >
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//////////////////////////////////////////////////////////////////
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ---++++++++++ unsigned short _keypad_read(void) +++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Arguments: < void>
// Returns:1..16,depending on the key pressed, or 0 if no key is pressed.
// Description : Checks if any key is pressed. Function returns 1 to 16,
// depending on the key pressed, or 0 if no key is pressed.
// Requires : Port needs to be appropriately initialized
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
unsigned short _keypad_read(void)
{
unsigned short mask =1,i=0,val=0,shift=0;
for(i=0;i<4;i++){
*tmp_keypad_pointer = mask;
val = *tmp_keypad_pointer& 0xf0;
////////////////////////////////////////////////////////////
#ifdef _DEBUG_KEY_
if(val)return ((val & 0xf0) |mask);
mask<<1;
}
#endif //-----------------------------------------
//////////////////////////////////////////////////////////////
#define sethere
#ifdef sethere
asm{ swapf _keypad_read_val_L0,1} // val = val>>4;
if(val){
while(shift<4){
if(val&(1<<shift)){
return (((i<<2)+shift)+1);
}
shift++;
}
}
mask<<=1;
}
#endif //----------------------------------
return 0;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// END FUNCTION < _keypad_read >
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//////////////////////////////////////////////////////////////////
#endif // _KEYPAD_C
_KEYPAD_H
/*
* Description:
* <library for working with 4x4 keypad; routines can also
* be used with 4x1, 4x2, or 4x3 keypad.>
* NOTES:
* < It supports keypads with 1 to 4 rows and 1 to 4 columns >
* Revision History:
* <1.0.0> -
* Test configuration:
Dev.Board: b.r.v
Ext. Modules: x
SW: mikroc compiler v6.2.1
*/
#ifndef _KEYPAD_H
#define _KEYPAD_H
// function prototype declare //
//////////////////////////////////////////////////////////////////
// Initializes port to work with keypad.
// The function needs to be called before using other routines
// from Keypad library.
void _Keypad_Init(char *port_keypad);
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
// Checks if any key is pressed.
// Function returns 1 to 16, depending on the key pressed,
// or 0 if no key is pressed.
unsigned short _keypad_read(void);
//////////////////////////////////////////////////////////////////
#endif // _KEYPAD_H
//////////////////////////////////////////////////////////////
use this example
#include"keypad.h"
char key =0;
void main()
{
trisb =0;
portb=0;
_keypad_init(&portd);
while(1)
{
key = _Keypad_read();
// #define _DEBUG_KEY_
#ifdef _DEBUG_KEY_
switch(key){
case 0x11:key = 1;break;
case 0x21:key = 2;break;
case 0x41:key = 3;break;
case 0x81:key = 4;break;
case 0x12:key = 5;break;
case 0x22:key = 6;break;
case 0x42:key = 7;break;
case 0x82:key = 8;break;
case 0x14:key = 9;break;
case 0x24:key = 10;break;
case 0x44:key = 11;break;
case 0x84:key = 12;break;
case 0x18:key = 13;break;
case 0x28:key = 14;break;
case 0x48:key = 15;break;
case 0x88:key = 16;break;
}
#endif // ----------------------------------
if(key)portb =key;
}
}
///////////////////////////////////////////////
///////////////////////////////////////////////