View previous topic :: View next topic |
Author |
Message |
Guan Choon Lim Guest
|
Keypad Routine to convert string to number |
Posted: Tue Jun 03, 2003 4:29 am |
|
|
Hi,all. I am new to using PIC C. I am designing a Direct Digital Frequency synthesizer which comes with a LCD display and a 4x4 keypads. The LCD will prompt user to input the desired frequency in Hz through the keypad.
Problem:
Is there a simple routine or function to convert a number string to integer? The keybd.h driver provided by CCS does not seem to allow me to do so, especially when the input is multiple characters such as "2340".
Also, how does one usually implement a Carriage Return or Enter Key in the program?
Your help is hereby sought!!
Thank You
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514997 |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
Re: Keypad Routine to convert string to number |
Posted: Tue Jun 03, 2003 5:02 am |
|
|
CCS has a few functions for this purpose. One of them is atoi() as I remember. You can check the CCS help for the rest of them.
Ali
___________________________
This message was ported from CCS's old forum
Original Post ID: 144514998 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Keypad Routine to convert string to number |
Posted: Tue Jun 03, 2003 1:04 pm |
|
|
:=Is there a simple routine or function to convert a number string to integer? The keybd.h driver provided by CCS does not seem to allow me to do so, especially when the input is multiple characters such as "2340".
:=
:=Also, how does one usually implement a Carriage Return or Enter Key in the program?
:=
:=Your help is hereby sought!!
:=
:=Thank You
--------------------------------------------------------
The following routine takes less ROM than the full atoi
function. When reading this function, remember that in CCS,
a "long" is an unsigned 16 bit value. (ie., int16)
Code: |
// Convert a string of up to 5 ascii decimal digits
// ("65535" max) to a binary long. Any value that
// is not ascii decimal will end the conversion. For
// example "25," will convert the first two digits and
// then stop at the comma, and return 25 as the result.
long adtol(char *ptr)
{
long retval;
char c;
char i;
retval = 0;
for(i = 0; i < 5; i++)
{
c = *ptr++; // Read char from string
if(c < '0') // Test if it's an ascii decimal value
break;
if(c > '9')
break;
retval = (retval << 3) + (retval << 1) + (c - '0');
}
return(retval);
} |
To answer your other question:
On a 3x4 or 4x4 keypad, there is typically no Enter key.
Use the '#' key or the '*' key as the Enter key.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515011
Edited two times, to fix the font size and also to put it in a
code block for easy reading.
Last edited by PCM programmer on Tue Jan 17, 2006 12:37 pm; edited 2 times in total |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
Re: Keypad Routine to convert string to number |
Posted: Tue Jun 03, 2003 7:40 pm |
|
|
If you want a quick & easy solution, here are the functions you can use:
atoi()
atoi32()
atol()
atof()
strtod()
strtol()
strtoul()
__Ali
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515022 |
|
|
|