AD7124-main.c

Hello all,

I am doing a project with AD7124. Initially, I faced problem in Device ID reading, so that I have put all files that is AD7124_regs.h,AD7124.h and AD7124.C in Main.c as visible to compiler. Now I want to segregate this as AD7124_regs.h,AD7124.h and AD7124.C. For that, I used following driver usage that recommend by Analog in My main.c

/* Create a new driver instance */struct ad7124_device my_ad7124;struct ad7124_device *ad7124_handler = &my_ad7124; /* Other variables */long timeout = 1000;long ret;long sample;. . ./* Initialize the driver instance and let's use the ad7124_regs array defined in ad7124_regs.h */#define AD7124_INITAD7124_Setup(ad7124_handler, AD7124_SLAVE_ID, (ad7124_st_reg *)&ad7124_regs); /* Read data from the ADC */ret = AD7124_WaitForConvReady(ad7124_handler, timeout);if (ret < 0)    /* Something went wrong, check the value of ret! */  ret = AD7124_ReadData(ad7124_handler, &sample);if (ret < 0)        /* Something went wrong, check the value of ret! */

After I made My main.c same as above, I am getting ad7124_regs undeclared error and also default registers values are  not getting initialized. So, to solve this, what are all I have to include in My main.c. Please suggest a solution.

Thanks,

Muthu

  • Hi Muthu,

    The following code is an example of a main.c that uses the AD7124 driver to initialize the device and read all registers.

    #define AD7124_INIT
    #include "AD7124.h"         // AD7124 definitions.

    void main(void)
    {
        struct ad7124_device ad7124;
        enum ad7124_registers regNr;
        long ret = 0;
            
        /* Initialize AD7124 device. */
        ret = AD7124_Setup(&ad7124, AD7124_SLAVE_ID, (ad7124_st_reg *)&ad7124_regs);
        if(ret < 0)
        {
            // AD7124 initialization failed
        }
        else
        {
            // AD7124 initialization ok
        }
        
        /* Read all registers */
        for(regNr = AD7124_Status; (regNr < AD7124_REG_NO) && !(ret < 0); regNr++)
        {
            ret = AD7124_ReadRegister(&ad7124, &ad7124_regs[regNr]);
            if (ret < 0)
                  break;
        }
    }

    Please let me know if this is helpful or if you have any other questions.

    -Dan

  • Hi DNechita,

    Thanks.

    ret = AD7124_Setup(&ad7124, AD7124_SLAVE_ID, (ad7124_st_reg *)&ad7124_regs);

    I have used the above line in My main.c.For this line, I am getting ad7124_regs undeclared (first use in this function) error.

    How to clear this?

    main:

    #define AD7124_INIT 

    struct ad7124_device my_ad7124;
    struct ad7124_device *ad7124_handler = &my_ad7124;

    int main(void)
    {
    enum ad7124_registers regNr;
    long ret = 0;

    ret = AD7124_Setup(ad7124_handler, AD7124_SLAVE_ID, (ad7124_st_reg *)&ad7124_regs);

    if(ret<0)

    return;
    ret = AD7124_ReadRegister(ad7124_handler,ad7124_regs[AD7124_ID]);

    if(ret<0)

    return;

    adcID = ad7124_regs[AD7124_ID].value; // for test the adc device ID

    Thanks,

    Muthu

  • I think you need to add this line:

    #define AD7124_INIT 

    before adding this one:

    #include AD7124.h"

    -Dan

  • Hi DNechita,

    Thanks.

    ret = AD7124_Setup(&ad7124, AD7124_SLAVE_ID, (ad7124_st_reg *)&ad7124_regs);

     

    I have used the above line in My main.c.For this line, I am getting ad7124_regs undeclared (first use in this function) error.

     

    How to clear this?

     

    Main:

    #define AD7124_INIT 

    struct ad7124_device my_ad7124;
    struct ad7124_device *ad7124_handler = &my_ad7124;

    uint_32t sample;

    uint_8t adc_ID;

    int32_t timeout = 1000;

     

     

    int main(void)
    {
    enum ad7124_registers regNr;
    long ret = 0;

     ret = AD7124_Setup(ad7124_handler, AD7124_SLAVE_ID, (ad7124_st_reg *)&ad7124_regs);

    if(ret<0)

    return;
    ret = AD7124_ReadRegister(ad7124_handler,ad7124_regs[AD7124_ID]);

    if(ret<0)

    return;

    adcID = ad7124_regs[AD7124_ID].value; // for test the adc device ID

     ret = AD7124_WaitForConvReady(ad7124_handler, timeout);
    if (ret < 0)
    return ret;
    ret = AD7124_ReadData(ad7124_handler, &sample);
    if (ret < 0)
    return ret;

     }

    Thanks,

    Muthu

  • Thanks that error got cleared.But, Defined #define AD7124_INIT in main.c, not getting  initialized in #ifdef AD7124_INIT in AD7124_regs.h.

     

     

    Thanks,

    Muthu

  • So the problem is that the documentation at:

    AD7124 No-OS Software [Analog Devices Wiki]  is wrong and I will fix it.

    The solution is to always define AD7124_INIT before including the AD7124.h if you want to use the ad7124_regs from AD7124_regs.h. Some user might want to define their own array of registers and to do that they will have to skip defining the AD7124_INIT.

    Is everything working now?

    -Dan

  • Hi DNechita,

    Thanks. As you know, I have defined AD7124_INIT in main.c. 

    #ifdef AD7124_INIT in AD7124_regs.h is telling AD7124_INIT is not defined.

    How to clear this?

    Please suggest a solution.

    Thanks,

    Muthu

  • I don't know why this is happening. Can you show me the exact error message that the compiler outputs?

    Thanks,

    -Dan

  • Hi DNechita,

    My program is not giving any errors but registers value is not getting initialized. While I debug, I am getting that #ifdef AD7124_INIT in AD7124_regs.h is not true. Actually, if Macro definition present for AD7124_INIT , atmel studio shows AD7124_INIT with Pink color in the statement that uses AD7124_INIT.And without Macro definition, it shows AD7124_INIT  with Black color.

    But, in My case, Though definition present, it shows AD7124_INIT with black color in   #ifdef AD7124_INIT statement.

    This results in Registers value not initialized.

    Thanks,

    Muthu

  • The fact that atmel studio does not color your defines as you expected is not the only thing to rely on when you want to verify that a macro is defined. If AD7124_INIT is not defined as a macro then the compiler doesn't know anything about the ad7124_regs variable and any attempts to use it will cause the compiler to throw an error message exactly like the one you had in the first place.

    What does it mean that registers are not initialized? What values are you reading? Can you read this value: ad7124_regs[AD7124_ID].value twice? Once before you call AD7124_ReadRegister(ad7124_handler,ad7124_regs[AD7124_ID]); and once after?

     

    Thanks,

    -Dan