CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

keypad entry problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
morebudwiser



Joined: 17 Oct 2005
Posts: 26

View user's profile Send private message

keypad entry problem
PostPosted: Mon Feb 27, 2006 10:19 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Feb 27, 2006 1:41 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 07, 2007 12:24 am     Reply with quote

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);
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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