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.

Need help with 4*4 keypad LCD

Status
Not open for further replies.

mannoooj

Newbie level 5
Joined
Feb 20, 2011
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,344
Hello,

I am interfacing 4*4 keypad and LCD with AT89S52. I need help in C programming.
I want to store the key pressed as Float value.
eg. keypad
1 2 3 <-(Backspace)
4 5 6 ->(Enter)
7 8 9 .(point)
* 0 #
I want to take 1.2 as input from keypad, display it on LCD and when I press ->, it should store 1.2 as float value.
Program should also use Backspace.

Thank you.
 

how is the keypad interfaced? e.g. via row and column digital IO pins or an SPI or similar decoder
 

Keypad is connected to Port 0
Column 0,1,2,3 to Port 0.0, 0.1, 0.2, 0.3
Row 0,1,2,3 to Port 0.4, 0.5, 0.6, 0.7
 

I have a 4 * 3 keypad connected so all the rows and colums have pullup resistors (see attached circuit)

the algorithm I use to determine which key is pressed is
Code:
LOOP writing 0 to each row in turn KPR1 thru KPR4 (the other rows being 1's)
   if columns are all 1's no key is pressed, i.e. columns are pulled up to 3.3v
  else
       determine which column (KPC1 or KPC2 or KPC3) is 0 hence the calculate key value
 

Attachments

  • Keypad.jpg
    Keypad.jpg
    20 KB · Views: 101
Last edited:

I have program to scan rows of keypad and display the key pressed on LCD.
but I want to store the key pressed.
 

say you have read the keypad characters pressed into a char array you can use sscanf() to convert to a float, e.g.
Code:
    char data[50]="1.2";
    float value;
    sscanf(data,"%f", &value);
    printf("value as float %f \n", value);
prints
Code:
value as float 1.200000
 
Thank you for code, horace1.
So I have to store key pressed in char array then convert it to Float value.
 

that is the simplest way.
the other way is convert it as you read each character checking if it is a digit or a . - if a digit is it before or after the . etc etc
you then have to check for errors yourself

Using sscanf() it will check for you, e.g.
Code:
    char data[50]="s1.2";
    float value;
    if(sscanf(data,"%f", &value) == 1)
        printf("value as float %f \n", value);
    else
        printf("error in number\n");
if the %f conversion works sscanf() returns 1 otherwise, if there is an error 0 (you then try reading fom the keypad again)
 
But while working on Keil, above program gives error.
"TEST SSCANF.C(6): error C141: syntax error near '[' " for "char data[50]="s1.2";"

---------- Post added at 17:04 ---------- Previous post was at 17:01 ----------

That code should not use 'data' as variable in Keil.
I changed it to 'd' and it is working
Thanx.

---------- Post added at 17:18 ---------- Previous post was at 17:04 ----------

Do you have any other code for conversion?
Because it is not working with Keil. Compiler does not create .hex file after including the code.
 

what is the error message?
you could use the atof() function
atof - C++ Reference

e.g.
Code:
    char data[50]="1.2";
    float value;
    value=atof(data);
    printf("value as float %f \n", value);
 
Error:
*** ERROR L119: REFERENCE MADE TO ERRONEOUS SEGMENT
SEGMENT: ?C_C51STARTUP
MODULE: STARTUP.obj (?C_STARTUP)
ADDRESS: 070FH

Is it possible to convert without using any function?
because it is taking too much code memory.
I have used the statement only once and it is taking almost 2K bytes memory. I have only 8k memory and in my program i have to use the statement multiple time.
 

Error:
I have used the statement only once and it is taking almost 2K bytes memory. I have only 8k memory and in my program i have to use the statement multiple time.
I tend to use PIC24's with 128k or 256K of program memory - although the chips cost more they save time which is more valuable unless we are dealing tens of thousands (or more) units!
to avoif scanf() you can convert it as you read each character checking if it is a digit or a . - if a digit is it before or after the . etc etc
For example this converts an integer
Code:
    char data[100]="12345";
    int i, value=0;
    for(i=0; data[i] != 0; i++)
        if((data[i]>='0') && (data[i] <='9'))
           value=value*10 + data[i]-'0';
    printf("value %d\n", value);
displays
Code:
value 12345
for a float it is more complex as you have to look for the decimal point . and then read the fraction component
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top