Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

saving keypad pressed help

Status
Not open for further replies.

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
*/
 

Hi

Convert the data on port B to ASCII or to BCD and save it in a RAM array Indexed by a counter

unsigned char Array[50];
unsigned char Index = 0 ;

Array[Index++] =Port_B_Data
...

All the best

Bobi

The microcontroller specialist
 

i hav tried ur suggestion. but i stil cant have the output that i want.
i think its because of the while(1) condition. how can i save the pressed key?pls help me with the code.plsss...
 

bobcat1 :-

unsigned char Array[50];
unsigned char Index = 0 ;

declare it outside while loop or use it as global.
 

i have tried this one. but still it doesnt work.. pls help me check this one.thanks..



void code_enter(void)
{

while(PORTB!=12)
{

scanned_key = scan_keypad();
decoded_key = decode_key(scanned_key);
display_key(decoded_key);

if(PORTB!=0)
{
code1 = decoded_key;
i++;
// __delay_ms(150);
// PORTB=0;
return;
}
void main()
{
i = 0;
code_enter();
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top