CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

atoi question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Dima49



Joined: 11 Apr 2005
Posts: 5

View user's profile Send private message Visit poster's website

atoi question
PostPosted: Sun Apr 17, 2005 7:28 pm     Reply with quote

hi,

I am a little stuck with the the code below; trying to code up a little controller board.

I am testing my rs232 comm code by writing to the pic and getting an input back via printf function. (I am using Hyperterminal on XP)

If I type in a number below 128 I get it back no prob. As soon as I move above this magic threshold the numbers become negative.

If PIC was a 32 bit processor and CCS had signed and unsigned ints I would understand what is going on, but currently I am a bit lost Smile <-- no pun intended...

As I see it, the problem is ether with the way Hyperterm views the data coming in or in my bonehead implementation of atoi function (I am voting for the later.)

if anyone has time to look and comment I would very much appreciate it.

d

Code:


char rxBuffer[4];         //the largest number recieved will be 255, that's 3 characters but termination (0x00) byte


#INT_RDA
void isr()
//generate an interrupt when serial-in buffer is filled
{
   char rxValue;
   int commandValue;
   
   int rxBufferIndex=0;
   
   output_high(PIN_B0);
   
   rxValue=getc();            //read in the value
   
   if(rxValue!='A')         //see if NULL character (end of string)
   {
      rxBuffer[rxBufferIndex]=rxValue;      //add a new value to the array
      rxBufferIndex++;
   }
   
   else
   {
      rxBuffer[rxBufferIndex]=0x00;      //cap off the buffer with a termination character (NULL)
      rxBufferIndex=0;               //reset RX buffer
      
      commandValue=atoi(rxBuffer);      //convert the characters which we recieved to intergers
      printf("---------------->%d\n\r",commandValue);
}
}
   
newguy



Joined: 24 Jun 2004
Posts: 1911

View user's profile Send private message

PostPosted: Sun Apr 17, 2005 7:34 pm     Reply with quote

Just a quick comment....

The default integer size for CCS is 8 bits, so if the aoti function is doing a signed transfer, then that's the reason why anything over 127 would be negative, because the msb (bit 8) is a 1.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Apr 17, 2005 8:08 pm     Reply with quote

CCS provides the source code for atoi() in this folder:
c:\program files\picc\drivers\stdlib.h

The function declaration is:
signed int atoi(char *s)
Dima49



Joined: 11 Apr 2005
Posts: 5

View user's profile Send private message Visit poster's website

thnx
PostPosted: Sun Apr 17, 2005 9:48 pm     Reply with quote

thanx for pointing me in the right direction. I didn't realize that function definitions where available. <-- at least that part was quite bonehead-like Smile

I think I might take the atol route and just give myself more bits to work with.

I also like the method used in:
http://ccsinfo.com/forum/viewtopic.php?t=20726&highlight=ascii2int8&sid=9523d7ad9a07bc8f0ee078dd9d605259

It's pretty elegant.

Pls tell if there are any other ways to tackle the problem, other wise thank you again. Help much appreciated.

d
Dima49



Joined: 11 Apr 2005
Posts: 5

View user's profile Send private message Visit poster's website

ikk! beating my head against the wall AGAIN
PostPosted: Sat Apr 23, 2005 8:28 pm     Reply with quote

OK sorry to resurrect this topic again but I am getting signed numbers AGAIN! one bit still manages to work against me Crying or Very sad

Why am I getting negative numbers as soon as I reach 128? The function below is supposed to return unsigned int. I am not setting or clearing MSB anywhere.

Once again sorry to bring this question up again and would very much appreciate help in this matter.

dima

Code:


unsigned char rxBuffer[RX_BUFFER_SIZE];   

unsigned int Ascii2Int()
//convert an ascii string to an interger
{
   return((rxBuffer[0]&0x0f)*100+(rxBuffer[1]&0x0f)*10+(rxBuffer[2]&0x0f));
}

#INT_RDA
void isr()
//generate an interrupt when serial-in buffer is filled
{
   char rxValue;
   unsigned int commandValue;
   
   static int rxBufferIndex=0;
   
   output_high(PIN_B0);
   
   rxValue=getc();            //read in the value
   
   if(rxValue!='A')         //see if NULL character (end of string) was passed
   {
      rxBuffer[rxBufferIndex]=rxValue;      //add a new value to the array
      rxBufferIndex++;
   }
   
   else
   {
      rxBuffer[rxBufferIndex]=0x00;      //cap off the buffer with a termination character (NULL)
      rxBufferIndex=0;               //reset RX buffer
      
      commandValue=Ascii2Int();         //convert the characters which we recieved to integer value
      printf("---------------->%d\n\r",commandValue);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 23, 2005 11:54 pm     Reply with quote

Instead of getting sad, you should go into analysis mode.
You should question your code, and look up anything that
you don't completely understand in the manual or the Help file.

You should ask yourself what is the %d in the printf statement ?
Why is it there ?
Does it affect how the numbers are displayed ?

printf("---------------->%d\n\r",commandValue);

Then you can look it up in some document and notice
that there are many options for printf. Maybe one
of them is for signed numbers, and another is for
unsigned numbers, etc.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_printf_type_field_characters.asp
In other words, all of this stuff should be done before you get sad. Mr. Green
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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