-2

I am newbie here but have a problem without answer. I'm working with a TM4C1294 from Texas and in code there are countless errors. But at first, I have this code. How you can see, x3 have the read from PIN ADC0, but the error give to me in the lane is (obs. ignore asterick before # and the code is not complete because the rest of the body in the CCS is too large). Thanks for the attention!!

 %declaration of pointers to PIN read
 #define F_SAMPLE   2000    
 #define  x0    *ADC3_read  
 #define  x1    *ADC2_read  
 #define  x2     *ADC1_read  
 #define  x3     *ADC0_read  
 #define  x4     *ADC4_read

 %declaration of variables
 int PI = 3.14159;
 int teste = 0;
 float y_0, ya1, yb1, y_1;
 float y0_aux, ya1_aux, yb1_aux;
 int alfa, i, j;

 %declaration of PIN read
 uint32_t ADC_read[5];              
 uint32_t *ADC0_read=&ADC_read[0];  
 uint32_t *ADC1_read=&ADC_read[1];  
 uint32_t *ADC2_read=&ADC_read[2];   
 uint32_t *ADC3_read=&ADC_read[3];   
 uint32_t *ADC4_read=&ADC_read[4];

%control code
   for( i=0; i <= 12000; i++)
{
    alfa = alfa + (2*PI/200);
   if (alfa >= 2*PI)
      alfa = alfa - 2*PI;
j++;
y0_aux = y0_aux + x3[i]; %error=identifier "x3" is undefined 
ya1_aux = ya1_aux + x3[i]*sin(alfa); 
yb1_aux = yb1_aux + x3[i]*cos(alfa); 
if(j==200){
   y_0 = y0_aux/200;
   ya1 = ya1_aux/200;
   yb1 = yb1_aux/200;

  y_1 = sqrt((ya1 * ya1) + (yb1 * yb1)); 

  y0_aux = 0;
  ya1_aux = 0;
  yb1_aux = 0;
  j = 0;
    }
}
}
  • Redefine as follows (e.g.): `#define x3(offset_) ADC0_read[offset_]` and I think that will work. Likewise, for your other macros. – Craig Estey Oct 24 '18 at 19:47
  • @CraigEstey now he gives the follow error in expression `y0_aux = y0_aux + *x3(i);` _operand of "*" must be a pointer_ . I have changed to a pointer but nothing happens, what I have to do? – Luiz Arthur Tarralo Passatuto Oct 24 '18 at 22:13
  • Follow [original] instructions :-). That is, I said change the `#define` but _not_ anything else. Your original equation was `y0_aux = y0_aux + x3(i)`. With that, and my suggested `#define` change, it would have worked. But, also, you [incorrectly] added back a `*` by doing `y0_aux = y0_aux + *x3(i)`. Please understand that to dereference a pointer with offset, you can do: `*(ptr + (off))` OR [and this is often preferable]: `ptr[off]`. Your second attempt was the equivalent of `*ptr[off]`, which is not the same. So, just remove the extra `*` – Craig Estey Oct 24 '18 at 23:36
  • I have follow your original instructions :) But not worked :( This is why I have made the change. However, even adopting what you said, the error still persists. – Luiz Arthur Tarralo Passatuto Oct 25 '18 at 00:26
  • At this point, I'd edit your question and post [more of] your complete code, including the definition(s) of `ADC0_read`, etc. My assumption was that it was something like `#define ADC0_read 0x8010`. And, the definitions of `y0_aux`, etc. Along with that, post the exact error messages as well – Craig Estey Oct 25 '18 at 00:37
  • Also, note that `^` is the XOR operator [and _not_ "raise to a power"]. So, if you're trying to square a number, `y_1 = sqrt(ya1^2 + yb1^2)` while syntactically correct is probably suspect and should be `y_1 = sqrt((ya1 * ya1) + (yb1 * yb1))`. `ya1 * ya1` is probably faster than (e.g.) `pow(ya1,2)` – Craig Estey Oct 25 '18 at 00:38
  • @CraigEstey I followed your instructions and edited the question as requested, I hope to be clearer and I thank you heartily for the help. The only error in the control code is now in the line of the first calculation of y0_aux, that you can see in the code. Thank you one more time!! – Luiz Arthur Tarralo Passatuto Oct 25 '18 at 02:45

1 Answers1

0

Caveat: This may be incomplete as I'm not sure about your final result, but we can get the compilation errors out. I had to take a slight liberty to add a main function so I could get something compilable.

Here is a reindented version of your latest program [which has errors]:

#include <stdint.h>
#include <math.h>

// declaration of pointers to PIN read
#define F_SAMPLE   2000
#define  x0    *ADC3_read
#define  x1    *ADC2_read
#define  x2     *ADC1_read
#define  x3     *ADC0_read
#define  x4     *ADC4_read

int
main(void)
{

    // declaration of variables
    int PI = 3.14159;
    int teste = 0;
    float y_0,
     ya1,
     yb1,
     y_1;
    float y0_aux,
     ya1_aux,
     yb1_aux;
    int alfa,
     i,
     j;

    // declaration of PIN read
    uint32_t ADC_read[5];
    uint32_t *ADC0_read = &ADC_read[0];
    uint32_t *ADC1_read = &ADC_read[1];
    uint32_t *ADC2_read = &ADC_read[2];
    uint32_t *ADC3_read = &ADC_read[3];
    uint32_t *ADC4_read = &ADC_read[4];

    // control code
    for (i = 0; i <= 12000; i++) {
        alfa = alfa + (2 * PI / 200);
        if (alfa >= 2 * PI)
            alfa = alfa - 2 * PI;
        j++;

        // error = identifier "x3" is undefined
        y0_aux = y0_aux + x3[i];
        ya1_aux = ya1_aux + x3[i] * sin(alfa);
        yb1_aux = yb1_aux + x3[i] * cos(alfa);

        if (j == 200) {
            y_0 = y0_aux / 200;
            ya1 = ya1_aux / 200;
            yb1 = yb1_aux / 200;

            y_1 = sqrt((ya1 * ya1) + (yb1 * yb1));

            y0_aux = 0;
            ya1_aux = 0;
            yb1_aux = 0;
            j = 0;
        }
    }
}

This is the gcc error output:

fix1.c: In function ‘main’:
fix1.c:9:17: error: invalid type argument of unary ‘*’ (have ‘uint32_t {aka unsigned int}’)
 #define  x3     *ADC0_read
                 ^
fix1.c:46:21: note: in expansion of macro ‘x3’
   y0_aux = y0_aux + x3[i];
                     ^~
fix1.c:9:17: error: invalid type argument of unary ‘*’ (have ‘uint32_t {aka unsigned int}’)
 #define  x3     *ADC0_read
                 ^
fix1.c:47:23: note: in expansion of macro ‘x3’
   ya1_aux = ya1_aux + x3[i] * sin(alfa);
                       ^~
fix1.c:9:17: error: invalid type argument of unary ‘*’ (have ‘uint32_t {aka unsigned int}’)
 #define  x3     *ADC0_read
                 ^
fix1.c:48:23: note: in expansion of macro ‘x3’
   yb1_aux = yb1_aux + x3[i] * cos(alfa);
                       ^~

Here is one way to change things [this compiles cleanly]:

#include <stdint.h>
#include <math.h>

// declaration of pointers to PIN read
#define F_SAMPLE   2000
#define  x0    ADC3_read
#define  x1    ADC2_read
#define  x2    ADC1_read
#define  x3    ADC0_read
#define  x4    ADC4_read

int
main(void)
{

    // declaration of variables
    int PI = 3.14159;
    int teste = 0;
    float y_0,
     ya1,
     yb1,
     y_1;
    float y0_aux,
     ya1_aux,
     yb1_aux;
    int alfa,
     i,
     j;

    // declaration of PIN read
    uint32_t ADC_read[5];
    uint32_t *ADC0_read = &ADC_read[0];
    uint32_t *ADC1_read = &ADC_read[1];
    uint32_t *ADC2_read = &ADC_read[2];
    uint32_t *ADC3_read = &ADC_read[3];
    uint32_t *ADC4_read = &ADC_read[4];

    // control code
    for (i = 0; i <= 12000; i++) {
        alfa = alfa + (2 * PI / 200);
        if (alfa >= 2 * PI)
            alfa = alfa - 2 * PI;
        j++;

        // error = identifier "x3" is undefined
        y0_aux = y0_aux + x3[i];
        ya1_aux = ya1_aux + x3[i] * sin(alfa);
        yb1_aux = yb1_aux + x3[i] * cos(alfa);

        if (j == 200) {
            y_0 = y0_aux / 200;
            ya1 = ya1_aux / 200;
            yb1 = yb1_aux / 200;

            y_1 = sqrt((ya1 * ya1) + (yb1 * yb1));

            y0_aux = 0;
            ya1_aux = 0;
            yb1_aux = 0;
            j = 0;
        }
    }
}

Here is an alternate way to code it [this also compiles cleanly]:

#include <stdint.h>
#include <math.h>

// declaration of pointers to PIN read
#define F_SAMPLE   2000
#define  x0(o)    ADC3_read[o]
#define  x1(o)    ADC2_read[o]
#define  x2(o)    ADC1_read[o]
#define  x3(o)    ADC0_read[o]
#define  x4(o)    ADC4_read[o]

int
main(void)
{

    // declaration of variables
    int PI = 3.14159;
    int teste = 0;
    float y_0,
     ya1,
     yb1,
     y_1;
    float y0_aux,
     ya1_aux,
     yb1_aux;
    int alfa,
     i,
     j;

    // declaration of PIN read
    uint32_t ADC_read[5];
    uint32_t *ADC0_read = &ADC_read[0];
    uint32_t *ADC1_read = &ADC_read[1];
    uint32_t *ADC2_read = &ADC_read[2];
    uint32_t *ADC3_read = &ADC_read[3];
    uint32_t *ADC4_read = &ADC_read[4];

    // control code
    for (i = 0; i <= 12000; i++) {
        alfa = alfa + (2 * PI / 200);
        if (alfa >= 2 * PI)
            alfa = alfa - 2 * PI;
        j++;

        // error = identifier "x3" is undefined
        y0_aux = y0_aux + x3(i);
        ya1_aux = ya1_aux + x3(i) * sin(alfa);
        yb1_aux = yb1_aux + x3(i) * cos(alfa);

        if (j == 200) {
            y_0 = y0_aux / 200;
            ya1 = ya1_aux / 200;
            yb1 = yb1_aux / 200;

            y_1 = sqrt((ya1 * ya1) + (yb1 * yb1));

            y0_aux = 0;
            ya1_aux = 0;
            yb1_aux = 0;
            j = 0;
        }
    }
}

But, indexing off from ADC0_read doesn't make sense to me. Your for loop goes to 12000, but ADC_read has only 5 elements, so either variant above will probably segfault because you're going way past the end of the array.

More likely, you may want something like:

uint32_t ADC_read[12000];

#define x3(o)   ADC_read[(o) + 0]
#define x2(o)   ADC_read[(o) + 1]
...

Honestly, I don't see how the individual pointers (ADC3_read, etc.) fit in [well]

Craig Estey
  • 30,627
  • 4
  • 24
  • 48