|
|
View previous topic :: View next topic |
Author |
Message |
Guan Choon Lim Guest
|
Keypad Routine to read ascii decimal |
Posted: Sun Jun 15, 2003 10:52 pm |
|
|
Hi, all. I have the following code to read numers (any number from 1 to 9999) from a 4x3 keypad (4 rows, 3 cols). For example, if I press the key "2", "4", "5", and "6", followed by a "*" key, I want the code to store the keypress as a character string "2456". The number will also be echoed to the LCD. Next, I will want to convert the string to an integer number.
I have the CCS PCW compiler (V3.158, bought last month) and I am modifying the EX_LCDKB.C example for my purposes. This is how part of my code looks like
#include <lcd.c>
#include <kbd.c>
#include <stdlib.c>
void main(){
char string[4];
int ix,count, asciinum;
lcd_init();
kbd_init();
lcd_putc("\fReady...\n");
For (ix=0;ix<=4;ix++)
{
if (kbd_getc() !=0)
if (kbd_getc() =="*")
lcd_putc('\f');
else
{
string[ix]=kbd_getc();
lcd_putc(string[ix]);
}
}
asciinum =atoi(string); // convert string to dec
// Test whether asciinum is correct by toggling
// an LED connected to PIN_A0.
// If asciinum is 9, LED blinks 9 times
for (ix=1;ix<=asciinum;ix++) {
output_high(PIN_A0);
delay_ms(1000);
output_low(PIN_A0);
delay_ms(1000);
}
while(1);
}
Somehow, the FOR loop doesn't work!! Guess I did not get the algo correct.
Appreciate if anyone could advise me!
Many Thanks
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515270 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Keypad Routine to read ascii decimal |
Posted: Sun Jun 15, 2003 11:15 pm |
|
|
I'll give you a tip, to get you farther along:
You need to read the key once, at the top of the "for" loop
and save it in a variable. Then do your tests on that variable.
<PRE>
char c;
<BR>
for(i = 0; i < 4; i++)
{
c = kbd_getc(); // Read the key and put it in c
if(c != 0)
{
if(c == '*')
// Do something
}
else
{
// Do something else
<BR>
}
// etc.
}
etc.
</PRE>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515272 |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
Re: Keypad Routine to read ascii decimal |
Posted: Sun Jun 15, 2003 11:20 pm |
|
|
You didn't mention which FOR doesn't work. But I can see a couple of problems with your first loop:
1-The first time you do a kbd_getc() store in a variable and the use that variable for all your condition checkings.
2-You loop forces you to enter 5 characters. But if for any reason '*' is pressed in the middle of the process, your 'string[4]' will get corrupted because it will not store the '*' in it but the index ix will increase, and also because eventually you'll end up storing 5 bytes in it while it is only a 4-byte array. Same thing happens if the user presses '0'.
3-As far as I rememeber, functions like atoi() rely on a null character to mark the end of the string. Since your string doesn't end with a \0, atoi() may get confused.
4-It may be a better idea to replace the first FOR with a WHILE and break out of it when '*' is pressed. This way you are not forced to press 4 keys everytime.
Hope this helps.
Ali
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515273 |
|
|
Guan Choon Lim Guest
|
Re: Keypad Routine to read ascii decimal |
Posted: Mon Jun 16, 2003 4:29 am |
|
|
Hi, Ali & PCM programmer
I must admit that I am not so sharp in programming. I am a RF engineer trying to get my hardware controlled with the PIC MCU. Nevertheless, I change my code to the ones below:
///////////////////////////////
#include <lcd.c>
#include <kbd.c>
#include <stdlib.h>
main() {
char k, string[5];
int ix, count;
lcd_init();
kbd_init();
strcpy(string,"000000"); // Initialise array to contain null
ix=0;
lcd_putc("\fReady....\n");
k=kbd_getc();
while (k!='*')
{
if (k!=0) // user has pressed a key!
{
lcd_putc(k);
string[ix]=k;
ix++;
}
k=kbd_getc();
}
count=atoi(string);
for (ix=0;ix
{
output_high(PIN_A0);
delay_ms(1000);
output_low(PIN_A0);
delay_ms(1000);
}
while(TRUE);
}
/////// End of Program ////////
I compile the code. No error. I hope this will work on my CCS software prototyping board.
Do give me some advice, if you find some bugs in my program. Really appreciated if you could help
Regards
Guan Choon Lim
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515286 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|