This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

atoi problems

I am having problems getting atoi to work.

here is how I populate the comin[] array

do
        {
        comin[index] = (getchar());
        ++index;
        }
        while (index<=4);

later in the program I do the following:-

command = atoi(comin);

expecting to get a 4 digit number in command. comin is working fine - no problems. But command is always 0 (zero). I have tried quite a few things, but no luck. I did a search on the forum andd came up with this

http://www.keil.com/forum/docs/thread10470.asp

Anybody else had this problem or know a work around?

Parents
  • hi,
    today I did some more work on this problem.

    1. I corrected the serial routine to collect chars 1 by 1
    2. If i look at comin, command and index using the simulator I can see that index is incrementing, comin is collecting the chars (I'm putting in the ascii codes, so i.e. digit '9' is 57decimal
    3. the reason atoi is not working is my code never goes through the atoi segment of the code - it always jumps over it.
    4. I experimented and removed the if statement and braces arounf th e aoi statement and atoi is working.
    5. The problem is my if statement . . .

    I have to admit I am stumped at this point.

    here is the serial input routine

    void uart_rx_isr (void) interrupt 4 using 3
    {
    
            if (RI==TRUE)           /* if  RI  is  set  then  execute       the  RxD routine,  else jump over and exit*/
    
            {
            comin[index] = SBUF;    // I tried (getchar()) which also works
            ++index;
            RI = FALSE;             /* clear the  RI interrupt flag and exit */
            }
    
    }
    
    

    here is the fragment that contains the atoi

    loop:
            /* this is the  main process  loop  - it services  pushbutton  and  */
            /* rotary encoder  inputs  as  well  as  th e inputs  from the remote */
    
    
            if (comin[5]=="\n")
            //if (index==6) can also be  used here, but I prefer the \n or some other command string termiator
            {
            command = atoi(comin);
            txprocess();  // go and process  the command
            index=0;
            }
    
            command = 0;                    /* now flush command and comin */
    
    
    
    goto loop;
      }
    
    
    

Reply
  • hi,
    today I did some more work on this problem.

    1. I corrected the serial routine to collect chars 1 by 1
    2. If i look at comin, command and index using the simulator I can see that index is incrementing, comin is collecting the chars (I'm putting in the ascii codes, so i.e. digit '9' is 57decimal
    3. the reason atoi is not working is my code never goes through the atoi segment of the code - it always jumps over it.
    4. I experimented and removed the if statement and braces arounf th e aoi statement and atoi is working.
    5. The problem is my if statement . . .

    I have to admit I am stumped at this point.

    here is the serial input routine

    void uart_rx_isr (void) interrupt 4 using 3
    {
    
            if (RI==TRUE)           /* if  RI  is  set  then  execute       the  RxD routine,  else jump over and exit*/
    
            {
            comin[index] = SBUF;    // I tried (getchar()) which also works
            ++index;
            RI = FALSE;             /* clear the  RI interrupt flag and exit */
            }
    
    }
    
    

    here is the fragment that contains the atoi

    loop:
            /* this is the  main process  loop  - it services  pushbutton  and  */
            /* rotary encoder  inputs  as  well  as  th e inputs  from the remote */
    
    
            if (comin[5]=="\n")
            //if (index==6) can also be  used here, but I prefer the \n or some other command string termiator
            {
            command = atoi(comin);
            txprocess();  // go and process  the command
            index=0;
            }
    
            command = 0;                    /* now flush command and comin */
    
    
    
    goto loop;
      }
    
    
    

Children