I've been encountering a lot of problems trying to do serial communication with a digital compass device, and have noticed after experimenting for hours that if I run "atoi" on the same char string containing the heading info 3 times in a row (i.e. 3 lines right after the other doing the same atoi into 3 different variables) get different results. The last two results are correct, but the first result oscillates between the correct value and some strange erroneous value every now and then. Why is this? Here's a code snippet of what I'm talking about...
Param1DegStr = UART1Buffer; Param1DegStr = UART1Buffer; Param1DegStr = '\0'; Heading1 = atoi(Param1DegStr); Heading2 = atoi(Param1DegStr); Heading3 = atoi(Param1DegStr);
The values Heading2 & Heading3 will consistantly read the same number, i.e. 17, however, Heading1 will show 17 most of the time but sometimes show some strange value like 12596 instead?@? I've just got a break point right after all of the "Headingx = " statements and expected to at least see that they all get the same result, but when I saw that they don't have wondered if the problem lies with atoi itself? Am I calling it too soon after making the assignment into my char string or something?