View previous topic :: View next topic |
Author |
Message |
elektronische
Joined: 18 Mar 2010 Posts: 9
|
Get two numbers from Keypad. |
Posted: Tue Mar 30, 2010 4:36 pm |
|
|
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
|
|
|
elektronische
Joined: 18 Mar 2010 Posts: 9
|
|
Posted: Tue Mar 30, 2010 6:54 pm |
|
|
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
|
|
Posted: Tue Mar 30, 2010 8:24 pm |
|
|
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
|
|
Posted: Wed Mar 31, 2010 4:28 pm |
|
|
Hi, i'm trying to do that but it don't work
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
|
|
Posted: Wed Mar 31, 2010 5:09 pm |
|
|
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
|
|
Posted: Wed Mar 31, 2010 5:18 pm |
|
|
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
|
|
Posted: Thu Apr 01, 2010 1:38 pm |
|
|
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
|
|
Posted: Thu Apr 01, 2010 3:31 pm |
|
|
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 ! |
|
|
|