View previous topic :: View next topic |
Author |
Message |
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
atoi32 not working |
Posted: Wed Aug 12, 2009 8:35 am |
|
|
I am receiving gps coords in a pic18f252 and using 4.092.
The ascii longitude looks like:
"09744.6622" and is assigned to a string called gps.
I then get rid of the decimal point and assign the new string to cal:
"097446622"
I then run the atoi32(cal) and my result is 13560542
Hmmmm????
Anyone have any ideas? |
|
|
Ttelmah Guest
|
|
Posted: Wed Aug 12, 2009 9:04 am |
|
|
When you generated your new string, did you remember to put a 'null' character into the location after the last '2'?.
If not, atoi32, will carry on walking through memory till it does find a '\0' character, and what you get will depend on what is in these latter characters...
Best Wishes |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
Re: atoi32 not working |
Posted: Wed Aug 12, 2009 9:13 am |
|
|
I have 4.064 and I used MPLAB SIM under DEBUGGER to create this test program:
Code: |
#include <18F252.h>
#fuses XT, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=4000000)
char c[] = "097446622" ;
int32 result;
#include <stdlib.h>
//=========================
void main()
{
result = atoi32(c);
delay_ms(1); //BREAKPOINT SET HERE
}
|
Using the WATCH option I get:
c = 0x3930 (14640 in decimal in the next column over)
result = 0x5CEEADE (97446622 in decimal in the next colum over)
so it looks like everything is okay. |
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Wed Aug 12, 2009 9:29 am |
|
|
Yes I have a null character as the last character. I don't see any differences from mkuang and my code. I am viewing as decimal. Latitude is coming out ok with the same exact code as longitude. I will keep looking. |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Wed Aug 12, 2009 9:32 am |
|
|
Ttelmah wrote: | When you generated your new string, did you remember to put a 'null' character into the location after the last '2'?.
If not, atoi32, will carry on walking through memory till it does find a '\0' character, and what you get will depend on what is in these latter characters...
Best Wishes |
When you declare a string like my_string = "xxxxxxx" doesn't the compiler automatically put a null termination character at the end of the string? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 12, 2009 10:32 am |
|
|
Quote: | I don't see any differences from mkuang and my code. |
Post your short, compilable test program that shows the problem. |
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Wed Aug 12, 2009 10:55 am |
|
|
A short compilable program will be difficult. I should mention that the exact same program running with V2.49 works just fine. Another guy took my program and is using the new V4 compiler and he is seeing the problem.
In the receive interrupt when I get to the longitude data, I run the following code:
Code: |
if ((a == 5) && (b !=',')){
gps[chrcnt]=b;
if (chrcnt == 9)
{
chrcnt++; // get rover longitude
gps[chrcnt] = 0;
//gps looks like "09744.6622"
cal[0] = gps[0];
cal[1] = gps[1];
cal[2] = gps[2];
cal[3] = gps[3];
cal[4] = gps[4];
cal[5] = gps[6];
cal[6] = gps[7];
cal[7] = gps[8];
cal[8] = gps[9];
cal[9] = 0;
//call looks like "097446622"
roverlon = atoi32(cal);
//roverlon = 13560542
}
chrcnt++;
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 12, 2009 11:10 am |
|
|
But you could make that into a test program similar to mkuang's fairly
easily. Show the declarations of 'cal' and 'roverlon'. |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Wed Aug 12, 2009 11:24 am |
|
|
Well, if you use this test program you get the same result as before:
Code: |
#include <18F252.h>
#fuses XT, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=4000000)
char cal[10];
char gps[] = "09744.6622";
int32 roverlon;
#include <stdlib.h>
void main(void)
{
cal[0] = gps[0];
cal[1] = gps[1];
cal[2] = gps[2];
cal[3] = gps[3];
cal[4] = gps[4];
cal[5] = gps[6];
cal[6] = gps[7];
cal[7] = gps[8];
cal[8] = gps[9];
cal[9] = 0;
roverlon = atoi32(cal);
delay_ms(1); //BREAKPOINT HERE
}
|
Using the WATCH option I get roverlon = 9744622. I would check to make sure you declared roverlon and cal correctly. |
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Wed Aug 12, 2009 2:51 pm |
|
|
Sorry, I meant V3.249 seems to work ok.
I just added this to my code:
Code: |
sprintf(cal,"97446622");
roverlon = atoi32(cal);
|
and I get the same wrong answer. |
|
|
Ttelmah Guest
|
|
Posted: Wed Aug 12, 2009 3:29 pm |
|
|
The source code for atoi32, is sitting accessible to you, in stdlib.h.
Now, things that could cause problems:
1) Remember it returns a signed int32, not an unsigned. Should only 'matter' if the value goes beyond 2^31.
2) It _will_ go wrong, on later compilers, if you have changed the default 'long' type (won't apply to any of the posted examples so far).
How is your watch setup?. The value you are seeing is exactly what you would expect if you were looking at three bytes only of the result. Have you actually tried printf and outputting the value?.
Best Wishes |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Wed Aug 12, 2009 3:37 pm |
|
|
ljbeng wrote: | Sorry, I meant V3.249 seems to work ok.
I just added this to my code:
Code: |
sprintf(cal,"97446622");
roverlon = atoi32(cal);
|
and I get the same wrong answer. |
Why don't you take my short program above and compile it using V3.249 and 4.092, look at the result in WATCH and see what you see. It shouldn't take more than 2 minutes, instead of trying to guess where the problem is. |
|
|
|