View previous topic :: View next topic |
Author |
Message |
morebudwiser
Joined: 17 Oct 2005 Posts: 26
|
keypad entry problem |
Posted: Mon Feb 27, 2006 10:19 am |
|
|
hi i am using pic 16f877 and have an lcd connect to port d and a 4x4 keypad on port c . i have tested the hardware using the ccs lcdkb example and it is working ok. the problem i am having is i am trying to read multiple key press so if key 1 and 3 and press the * key i get a value of 13 returned. i am saving each key press to a string and then using the atoi funciton to change the value to an int but the value print on the lcd is not right here is my code below if anyone could help it would be much appreciated
#include "C:\Program Files\PICC\Drivers\lcd4x20test.h"
#include <LCD.C>
//#include <LCD420_d.C>
#include <KBD.C>
#include <stdlib.h>
char k , string[5];
char count, i = 0 ;
void main() {
lcd_init();
kbd_init();
lcd_putc("\fReady...\n");
while (TRUE)
{
do
{
k=kbd_getc();
}
while (k == '\0'); //wait for keypress
if (k == '*') // if keypress = * then
{
count = atoi(string); //copy string to count varible
printf(LCD_PUTC, "\n\f%d",count);
count = 0; //reset count
i = 0; //reset array
strcpy(string,"000000"); //reset string to = 0
}
else
{
string[i] = k; //copy value of key pressed to string
i++; //incerement i to point at next location
} //in array for further key press
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 27, 2006 1:41 pm |
|
|
Quote: | char k , string[5];
strcpy(string,"000000"); //reset string to = 0 |
1. How many characters can your 'string' array hold ?
2. How many characters are you copying to the string array ?
(Including the string terminator value of 0x00, which is at
the end of every string).
3. What will happen to your program if you overwrite ram
beyond the end of an array ? What if the compiler has
allocated other variables in that location ?
What will likely happen in that case ?
Quote: | count = atoi(string); //copy string to count varible |
The atoi function operates on a string.
How is the end of a string indicated in C ?
Does your code give a proper string to the atoi function ?
What will atoi() do if it doesn't see the end-of-string marker ? |
|
|
40inD
Joined: 30 Jul 2007 Posts: 112 Location: Moscow, Russia
|
|
Posted: Fri Sep 07, 2007 12:24 am |
|
|
same question.
If i need to enter from keypad 2-digit number into a string. How many chars this string must hold? string[2] or string[3] (for '\0')?
I think the code must be like this:
string[i]=k;
....
i++;
...
strcat(string, '\0')
number=atoi(string); |
|
|
|