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

Get two numbers from Keypad.

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



Joined: 18 Mar 2010
Posts: 9

View user's profile Send private message

Get two numbers from Keypad.
PostPosted: Tue Mar 30, 2010 4:36 pm     Reply with quote

Hi, my problem is I need to get two numbers by reading them from a 4x3 keypad with function kbd_getc (), but it could only get one. I'd like to read two and keep the two numbers as just one in a variable. Anyone can help me with this? thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 30, 2010 4:47 pm     Reply with quote

See this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=36304&start=2
elektronische



Joined: 18 Mar 2010
Posts: 9

View user's profile Send private message

PostPosted: Tue Mar 30, 2010 6:54 pm     Reply with quote

Look, even now what I am trying to do is something like this:
Code:

#include <16f877a.h>                     
#fuses XT,NOWDT,NOPROTECT,NOLVP           
#use delay (clock=4000000)               
#define use_portb_kbd TRUE

#INCLUDE "lcd.c"                         
#INCLUDE "kbd.c"

char num1=0;
char num2=0;

void main(void)
{
   port_b_pullups(TRUE);
   kbd_init();
   lcd_init();
   temperature();

}

void temperature()
{
while(TRUE)
{               
   do
   {
   num1=kbd_getc();
   
   }while(num1==0);
   do
   {
   num2=kbd_getc();
   
   }while(num2==0);

}
}

I wonder how I once obtained num1 and num2, concatenated into a single integer variable and hence whether to display that variable in another function using lcd_putc (), is this possible?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 30, 2010 8:24 pm     Reply with quote

You have two ASCII characters. Create an array. Put the first ASCII
character in the first position of the array. Put the 2nd byte in the 2nd
position. Put a 0x00 in the 3rd position. You now have a string.
Then run atoi() on it, and convert the string to an integer (byte).
elektronische



Joined: 18 Mar 2010
Posts: 9

View user's profile Send private message

PostPosted: Wed Mar 31, 2010 4:28 pm     Reply with quote

Hi, i'm trying to do that but it don't work Confused


Code:

#include <16f877a.h>                      //PIC utilizado
#fuses XT,NOWDT,NOPROTECT,NOLVP           //Configuramos los fuses
#use delay (clock=4000000)                //Oscilador a 4Mhz
#define use_portb_kbd TRUE
#INCLUDE "lcd.c"                         
#INCLUDE "kbd.c"
#INCLUDE "stdlib.h"


char num[3]={0,0,0};
void temperatura();
int temp;
int dec,uni;

void main(void)
{
   port_b_pullups(TRUE);
   kbd_init();
   lcd_init();
   temperatura();
}


void temperatura()

{


while(TRUE)
{               
   do
   {
   num[0]=kbd_getc();
    }while(num[0]==0);
   lcd_gotoxy(1,1);
   lcd_putc(num[0]);
   do
   {
   num[1]=kbd_getc();
    }while(num[1]==0);
   lcd_gotoxy(1,2);
   lcd_putc(num[1]);


   do
   {
   dec=atoi(num[0]);
   dec=(dec*10);
   uni=atol(num[1]);
   temp=dec+uni;
   lcd_gotoxy(8,1);
   lcd_putc(temp);
   }while(num[1]!=0);
   
   

}

}




I suppose in the position (8.1) should appear with an integer number, but nothing appears, am I misusing atoi function?

thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 31, 2010 5:09 pm     Reply with quote

Quote:

do
{
dec=atoi(num[0]);
dec=(dec*10);
uni=atol(num[1]);
temp=dec+uni;
lcd_gotoxy(8,1);
lcd_putc(temp);
}while(num[1]!=0);

I'm not sure what you're trying to do here. You're getting ASCII values
from the keypad, and then converting them to non-ascii integers, and
then trying to write those values to the LCD. The LCD needs ASCII
values. Why not just take the direct ASCII numbers from the keypad
and write them to the LCD ?

In addition to that, your code won't work. The atoi() function wants
a pointer to a string, not a character. When you do this num[0], you're
giving it the character.
elektronische



Joined: 18 Mar 2010
Posts: 9

View user's profile Send private message

PostPosted: Wed Mar 31, 2010 5:18 pm     Reply with quote

Shocked
I had completely forgotten that the lcd receives ASCII, that explains many things, the problem is that later I need that temperature value as an integer value, because I compare that value that gives me a temperature sensor.
This value does not display the temperature in the lcd, "But the value stored in an integer variable? which is what I need later.
thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 01, 2010 1:38 pm     Reply with quote

Then use atoi to convert the 2-digit 'num' string to an integer.
Give the atoi() function the 'num' array name as the parameter.
elektronische



Joined: 18 Mar 2010
Posts: 9

View user's profile Send private message

PostPosted: Thu Apr 01, 2010 3:31 pm     Reply with quote

Finally it works! doing this:

Code:

do
   {
   temp=atoi(num);
   lcd_gotoxy(8,1);
   printf(lcd_putc,"\%i",temp);
   }while(num[1]!=0);



Thanks a lot !
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