Thread: HELP WITH atoi()

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    3

    HELP WITH atoi()

    I'm trying get to get R character instructs the ATmega328P to read from the EEprom at a specifiedbyte address and return the result in decimal format to the serial monitor. It will befollowed by 3 characters indicating the read address in decimal. (e.g. R000, R010,R123 will read from EEprom addresses 0, 10 and 123 respectively and return thecontents)
    this is what I have but it's working as expected (don't mind the interrupts):
    int main (){


    char msg [100];
    int8_t R = atoi (msg);
    uint8_t W = 0;
    init();
    InitUsartMode();

    EnableInterrupts();
    sprintf(msg,"program begin\n");
    mySerialWrite(&(msg[0]));

    while (1){
    memset(msg, 0, sizeof(msg));
    DisableInterrupts(); // critical section
    if (rxIndex !=0)
    {
    W = 1;
    for(R = 0; R < rxIndex; R++)
    {
    msg [R] = rxBuffer [R];

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I don't understand the scenario you're trying to describe - perhaps you can elaborate in more detail?

    You should also post your code in code tags, ensuring the formatting is neat and consistent:

    Code:
    char msg [100];
    int8_t R = atoi (msg);
    uint8_t W = 0;
    init();
    InitUsartMode();
    
    EnableInterrupts();
    sprintf(msg,"program begin\n");
    mySerialWrite(&(msg[0]));
    
    while (1)
    {
        memset(msg, 0, sizeof(msg));
        DisableInterrupts(); // critical section
        if (rxIndex !=0) 
        {
            W = 1;
            for(R = 0; R < rxIndex; R++)
            {
                msg [R] = rxBuffer [R];
    Line 2 is wrong. This doesn't make "R" update automatically later in the code. You need to call "atoi()" after you have a valid string stored in "msg".

    It also looks like you're trying to use "R" for two separate purposes - as an index in your receive loop, and as the result of converting the received message to an int. If this is the case, it is clearly a problem.

    It looks like your "for()" loop is receiving a message from the serial port. Is this correct? If so, where is the code for accessing the EEPROM?

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    3
    thank you so much Matticus for the input, this is what I am actually asked to do:

    The R character instructs the ATmega328P to read from the EEprom at a specifiedbyte address and return the result in decimal format to the serial monitor. It will befollowed by 3 characters indicating the read address in decimal. (e.g. R000, R010,R123 will read from EEprom addresses 0, 10 and 123 respectively and return thecontents)
    here is the snippet of the code I'm concerned with, I am a Newbie but if I can get some help figuring this part out, I can do the rest. the actual program has many other files.

    Code:
    #include <stdint.h>#include "avr/interrupt.h"
    #include "defineFiles.h"
    #include "usart.h"
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void EnableInterrupts(void)
      {
      SREG_REG = SREG_REG | BIT7;
      }
    void DisableInterrupts(void)
    {
      SREG_REG = SREG_REG & (~BIT7);
      }
    int main (){
    
    
      char buffer [256];
      int R;
      uint8_t W = 0;
      init();
      InitUsartMode();
      
      EnableInterrupts();
    //  sprintf ("program begin");
      fgets (buffer, 256, stdin);
      R = atoi (buffer);
      sprintf ("R%d.",R);
      //mySerialWrite(&(msg[0]));
     
      while (1){
        memset(buffer, 0, sizeof(buffer));
       DisableInterrupts(); // critical section
       if (rxIndex !=0) 
       {
        W = 1;
       for(R = 0; R < rxIndex; R++)
       {
        buffer [R] = rxBuffer [R];
       }
        rxIndex = 0;
       }
       EnableInterrupts(); 
       if (W == 1)
        {
        W =0;
        mySerialWrite(&(buffer[0]));
        }
      }
      
      return 0;
    
    
    }
    Last edited by dhoow; 12-10-2015 at 11:17 AM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your formatting is almost there - you just need to ensure the indentation is consistent - and I'd suggest more than one space of indentation (I personally use 4 spaces).

    I'm not familiar with the ATmega328P, but a quick look through your code looks like it might not be right.

    First, let's look the requirements:

    The R character instructs the ATmega328P to read from the EEprom at a specifiedbyte address and return the result in decimal format to the serial monitor. It will befollowed by 3 characters indicating the read address in decimal. (e.g. R000, R010,R123 will read from EEprom addresses 0, 10 and 123 respectively and return thecontents)
    It sounds like you're supposed to read a string from the serial port, that consists of a letter and three digits.

    Like any word problem, we want to extract the requirements into a list:

    • Read input from the serial port and store in an array
    • Check if the first character is "R" - if so, continue
    • Convert the following three digits (after the "R") into a number - this is the EEPROM address
    • Query the EEPROM for the data at the given address, and store this value
    • Send that value back through the serial port


    You can use this list to guide you through coding.

    It might also be a good idea to go through each item on the list, and expand them into a list of discrete steps. Each step should roughly correlate to one or two lines of code (though the code should not be started until all the steps have been determined). This is referred to as pseudo code.

    Now you can use this list of steps to start generating your code.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Please don't cross-post - it's considered poor etiquette.

  6. #6
    Registered User
    Join Date
    Dec 2015
    Posts
    3

    thanks

    thank you, I had no idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Atoi and such
    By Khabz in forum C++ Programming
    Replies: 13
    Last Post: 03-19-2013, 02:56 PM
  2. use of atoi.
    By mgracecar in forum C Programming
    Replies: 3
    Last Post: 03-06-2012, 09:02 PM
  3. Atoi
    By r_james14 in forum C Programming
    Replies: 2
    Last Post: 02-29-2012, 11:19 AM
  4. C++.NET Can I use atoi ??
    By Swaine777 in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2004, 06:54 PM
  5. atoi()
    By Unregistered in forum C Programming
    Replies: 17
    Last Post: 07-13-2002, 02:09 AM