Could someone tell me what I'm doing wrong with this simple code???
I can't seem to get atoi to perform the way I expect.
Thank you, Marc
/*
I'm running this program on a PIC18F450 at 20MHz using the
MikroC compiler
*/
unsigned short result;
char teststring[3]={0,0,0};
char uart_rd;
void main() {
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
UART1_Write_Text("Start");
UART1_Write(10);
UART1_Write(13);
while (1) {
if (UART1_Data_Ready()) {
uart_rd = UART1_Read();
result=atoi(uart_rd);// atoi is the function I'm having trouble with
ByteToStr(result,teststring);
UART1_Write(uart_rd); //the input character returns correctly here
UART1_Write(10);
UART1_Write(13);
delay_ms(1000);
UART1_Write(teststring); // character out (1-9) does not match
} //the input character
}
}
atoi issues
Re: atoi issues
uart_rd is not a string and thus not a valid parameter of atoi. What is it that you want to achieve?ventete wrote:result=atoi(uart_rd);// atoi is the function I'm having trouble with
Re: atoi issues
Hi ventete,
In atoi function desription (Help)
Could you try to type cast atoi result to unsigned short, like:
See Explicit Types Conversions (Typecasting) in Help
Regards
Nico
In atoi function desription (Help)
Atoi returns integer, and your "result" is unsigned short.Function converts the input string s into an integer value and returns the value
Could you try to type cast atoi result to unsigned short, like:
Code: Select all
result = (unsigned short) atoi(uart_rd);
Regards
Nico
Re: atoi issues
That won't work because atoi function argument must be a pointer to char, i.e. null terminated array of chars.nico0481 wrote:Atoi returns integer, and your "result" is unsigned short.
Could you try to type cast atoi result to unsigned short, like:Code: Select all
result = (unsigned short) atoi(uart_rd);
The easiest way to convert characters '0' to '9' to their respective integer values is:
Code: Select all
result = uart_rd - '0';
Re: atoi issues
To all:
Thank you for your help. I have solved my atoi issue with all your help. Basically what I am trying to accomplish is to control 2 servos with a joystick and an RS232 link. I've foun that the most efficient way is to send one long string over the link (format: 123456aok0). The 123 is positional info for the first servo and 456 for the second. The alpha character represents positional info for the joystick while the "ok" is the delimiter for the uart receiver.
I then parse the string and convert the chars to ints for further processing.
Now comes the really embarassing part: In the debugger(somehow) the result of the atoi command defaulted to a float output instead of int. In my ignorance of the compiler, I didn't realize that all i had to do was click on the variable in the Watch Window and change it to decimal.
Sorry. And thanks again for your help. Marc
Thank you for your help. I have solved my atoi issue with all your help. Basically what I am trying to accomplish is to control 2 servos with a joystick and an RS232 link. I've foun that the most efficient way is to send one long string over the link (format: 123456aok0). The 123 is positional info for the first servo and 456 for the second. The alpha character represents positional info for the joystick while the "ok" is the delimiter for the uart receiver.
I then parse the string and convert the chars to ints for further processing.
Now comes the really embarassing part: In the debugger(somehow) the result of the atoi command defaulted to a float output instead of int. In my ignorance of the compiler, I didn't realize that all i had to do was click on the variable in the Watch Window and change it to decimal.
Sorry. And thanks again for your help. Marc