|
|
View previous topic :: View next topic |
Author |
Message |
Karl Guest
|
Weird multiplication |
Posted: Thu Sep 28, 2006 2:44 am |
|
|
Hi,
I have a simple routine to get a 4 digit(up to) number from the keypad, I then try to return this value as an int16. When I try to display the returned value on the screen I get weird values - eg when i type in 9876, the lcd displays 9102 (I think). Here is the code:
unsigned int16 KEYPAD_GetVal(void)
{
unsigned int8 i8_len = 0;
unsigned int8 i16_keyarr[6];
unsigned int8 i8_end = FALSE;
unsigned int16 i16_returnvalue;
while (i8_end == FALSE)
{
i16_keyarr[i8_len] = KEYPAD_GetKeyPress(); // KEYPAD_GetKeyPress() returns int8
if ( ((i16_keyarr[i8_len]) < 10) && (i8_len < 5))
{
lcd_putc(i16_keyarr[i8_len] + 0x30);
i8_len++;
}
else if (i16_keyarr[i8_len] == KEY_STOP)
{
if (i8_len == 0) { i8_end = TRUE; }
else {lcd_putc('\b'); lcd_putc(' '); lcd_putc('\b'); i8_len--; putc(0x02);putc(0x6A);putc(0X03);}
}
else if (i16_keyarr[i8_len] == KEY_ACCEPT)
{
if (i8_len != 0) { i8_end = TRUE; }
}
}
if (i8_len == 0) i16_returnvalue = (int16)(0);
if (i8_len == 1) i16_returnvalue = (int16)(i16_keyarr[0]);
if (i8_len == 2) i16_returnvalue = (int16)((i16_keyarr[0]*10) + i16_keyarr[1]);
if (i8_len == 3) i16_returnvalue = (int16)((i16_keyarr[0]*100)+(i16_keyarr[1]*10) + i16_keyarr[2]);
if (i8_len == 4) i16_returnvalue = (int16)((i16_keyarr[0]*1000)+(i16_keyarr[1]*100)+(i16_keyarr[2]*10) + i16_keyarr[3]);
return i16_returnvalue;
}
static unsigned char uc_buf[8];
printf(lcd_putc,"\f %s ",itoa(i16_returnvalue,10,uc_buf));
Is the problem I am trying to multiply 8 bit ints together to give a 16bit int?
Any help greatly appreciated!!
Thanks,
Karl |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 28, 2006 2:41 pm |
|
|
CCS has a function that will do the conversion from ascii decimal to
an unsigned integer. It's called atol. All you have to do is pass it
a string of digits. Just make sure there's a string-terminator byte
of 0x00 at the end of the digits (however many there are), and make
sure the array is large enough to hold the digits and the terminator.
Here's a demo program for atol(). It displays the following output:
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <stdlib.h>
//================================
void main()
{
int8 key_array[6] = {"9876"};
int16 result;
result = atol(key_array);
printf("key_array = %lu \n\r", result);
while(1);
} |
|
|
|
Karl Guest
|
Weird multiplication |
Posted: Fri Sep 29, 2006 1:40 am |
|
|
Hi PCM programmer,
Thanks for your reply, that is a much easier way of doing it thanks.
Karl |
|
|
|
|
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
|