vaghn73
Newbie level 3
- Joined
- Dec 26, 2009
- Messages
- 3
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Location
- philippines
- Activity points
- 1,317
this code works just fine. everytime a key is pressed, its binary equivalent is reflected at portB. but im having a problem on how i can store each value that is pressed on certain register(say press).the user can save as many digits as he wants but will terminate if * is pressed. can someone help me regarding this problem?this is the original code.i'm using pic16f877 and high tech c compiler. thankss.
/*
* HARDWARE:
* - PORTB<3:0> --> 4 LEDs
* - PORTD<3:0> --> Keypad rows
* - PORTD<6:4> --> Keypad column
*/
#include <pic.h>
#include <htc.h>
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif
//__CONFIG(HS & WDTDIS & LVPDIS);
__CONFIG(XT & WDTDIS & LVPDIS & UNPROTECT);
unsigned char scanned_key = 0x00;
unsigned char decoded_key = 0x00;
void init_LED(void)
{
PORTB = 0xF0;
TRISB = 0xF0;
}
void init_KEYPAD(void)
{
TRISD = 0x70; //PORTD<6:4> input, COLUMNS
TRISD = ~0x0F; //PORTD<3:0> output, ROWS
PORTD = 0b00001111; //PORTD<3:0> initially high
}
unsigned char decode_key(unsigned char val)
{
unsigned char key;
switch(val)
{
case 0x60: //0
key = 0x01;
break;
case 0x50: //1
key = 0x02;
break;
case 0x30: //2
key = 0x03;
break;
case 0x61: //3
key = 0x04;
break;
case 0x51: //4
key = 0x05;
break;
case 0x31: //5
key = 0x06;
break;
case 0x62: //6
key = 0x07;
break;
case 0x52: //7
key = 0x08;
break;
case 0x32: //8
key = 0x09;
break;
case 0x63: //9
key = 0x0A;
break;
case 0x53: //10
key = 0x0B;
break;
case 0x33: //11
key = 0x0C;
break;
default:
key = decoded_key;
break;
}
return key;
}
void display_key(unsigned char key)
{
PORTB = key;
}
unsigned char scan_keypad()
{
unsigned char val_1 = 0x00;
unsigned char val_2 = 0x00;
unsigned char count1;
for(count1=0;count1 < 4;count1++)
{
PORTD = ~(0x01 << count1); //scan row
__delay_ms(35);
val_1 = PORTD & 0x70; //read column, RD<6:4>
//while clearing RD<7> &
//RD<3:0>
if (val_1 != 0x70) //if a column is pressed
{
val_2 = (val_1 | count1); //column (high-nibble) and count (low-nibble)
return val_2; //return value
}
}
return 0xFF; //return any value
}
void main()
{
init_LED(); //initialize PORTB<3:0>, 4 LEDS connected
init_KEYPAD(); //initialize PORTD
//PORTD<3:0> as output
//PORTD<6:4> as input
while(1)
{
scanned_key = scan_keypad();
decoded_key = decode_key(scanned_key);
display_key(decoded_key);
}
}
/*
1 0x60
2 0x50
3 0x30
4 0x61
5 0x51
6 0x31
7 0x62
8 0x52
9 0x32
asterisk 0x64
0 0x54
hash 0x34
*/
/*
* HARDWARE:
* - PORTB<3:0> --> 4 LEDs
* - PORTD<3:0> --> Keypad rows
* - PORTD<6:4> --> Keypad column
*/
#include <pic.h>
#include <htc.h>
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif
//__CONFIG(HS & WDTDIS & LVPDIS);
__CONFIG(XT & WDTDIS & LVPDIS & UNPROTECT);
unsigned char scanned_key = 0x00;
unsigned char decoded_key = 0x00;
void init_LED(void)
{
PORTB = 0xF0;
TRISB = 0xF0;
}
void init_KEYPAD(void)
{
TRISD = 0x70; //PORTD<6:4> input, COLUMNS
TRISD = ~0x0F; //PORTD<3:0> output, ROWS
PORTD = 0b00001111; //PORTD<3:0> initially high
}
unsigned char decode_key(unsigned char val)
{
unsigned char key;
switch(val)
{
case 0x60: //0
key = 0x01;
break;
case 0x50: //1
key = 0x02;
break;
case 0x30: //2
key = 0x03;
break;
case 0x61: //3
key = 0x04;
break;
case 0x51: //4
key = 0x05;
break;
case 0x31: //5
key = 0x06;
break;
case 0x62: //6
key = 0x07;
break;
case 0x52: //7
key = 0x08;
break;
case 0x32: //8
key = 0x09;
break;
case 0x63: //9
key = 0x0A;
break;
case 0x53: //10
key = 0x0B;
break;
case 0x33: //11
key = 0x0C;
break;
default:
key = decoded_key;
break;
}
return key;
}
void display_key(unsigned char key)
{
PORTB = key;
}
unsigned char scan_keypad()
{
unsigned char val_1 = 0x00;
unsigned char val_2 = 0x00;
unsigned char count1;
for(count1=0;count1 < 4;count1++)
{
PORTD = ~(0x01 << count1); //scan row
__delay_ms(35);
val_1 = PORTD & 0x70; //read column, RD<6:4>
//while clearing RD<7> &
//RD<3:0>
if (val_1 != 0x70) //if a column is pressed
{
val_2 = (val_1 | count1); //column (high-nibble) and count (low-nibble)
return val_2; //return value
}
}
return 0xFF; //return any value
}
void main()
{
init_LED(); //initialize PORTB<3:0>, 4 LEDS connected
init_KEYPAD(); //initialize PORTD
//PORTD<3:0> as output
//PORTD<6:4> as input
while(1)
{
scanned_key = scan_keypad();
decoded_key = decode_key(scanned_key);
display_key(decoded_key);
}
}
/*
1 0x60
2 0x50
3 0x30
4 0x61
5 0x51
6 0x31
7 0x62
8 0x52
9 0x32
asterisk 0x64
0 0x54
hash 0x34
*/