View previous topic :: View next topic |
Author |
Message |
nick...7
Joined: 03 Dec 2013 Posts: 4 Location: Greece
|
Simple code keypad- lcd (help) |
Posted: Tue Dec 03, 2013 7:46 am |
|
|
Hi, I have this simple code which I can print captured key to lcd. I want to use this number that I print to lcd to another part of code if I push e.g. the # key. How can I modify this code?
e.g. if I push 2(two) 4(four) and # I want to use the 24 (twenty-four) number to do something else
Thanks in advance!
Code: | #include "16f877a.h"
#use delay(clock=4000000)
#fuses XT, NOLVP, NOWDT, NOPROTECT
#include "flex_lcd.c"
#include "kbd.c"
void main()
{
char k=0;
delay_ms(1);
lcd_init();
kbd_init();
for(;;){
lcd_gotoxy(1,1);
lcd_putc("puse any button");
lcd_gotoxy(1,2);
while(1) // infinite loop
{
k = kbd_getc();
if(k!=0)
{
lcd_putc(k);
k=0;
delay_ms(250);
}
delay_ms(1);
}
} |
|
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Tue Dec 03, 2013 3:04 pm |
|
|
Assuming you want the decimal number 24 and not a string with the ascii characters 2 and 4 in it, you need to convert the incoming digits to the number you want. In this case, 24 is actually (2*10) + 4. Each time you press another digit, you need to multiply the existing total by 10 then add the new number to the total. You start with a total of 0. Make sure you define a large enough total variable to hold any possible number you may enter (you may want to be able to handle 3 or 4 digits or whatever is needed for your application.) How to un-do the last digit pressed if you make a mistake is left as an exercise for later :-)
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
nick...7
Joined: 03 Dec 2013 Posts: 4 Location: Greece
|
|
Posted: Wed Dec 04, 2013 2:41 pm |
|
|
gpsmikey wrote: | Assuming you want the decimal number 24 and not a string with the ascii characters 2 and 4 in it, you need to convert the incoming digits to the number you want. In this case, 24 is actually (2*10) + 4. Each time you press another digit, you need to multiply the existing total by 10 then add the new number to the total. You start with a total of 0. Make sure you define a large enough total variable to hold any possible number you may enter (you may want to be able to handle 3 or 4 digits or whatever is needed for your application.) How to un-do the last digit pressed if you make a mistake is left as an exercise for later :-)
mikey |
Thank you for your explanation! My knowledge in programming are few (to do something from the start), so I Will try to modify a code for a calculator that I just found! |
|
|
|