1

I get this error at the line where __delay_cycles is called. #664 expected an integer constant C/C++ Problem

The functions' parameters: __delay_cycles(unsigned long cycles);

This code isn't practical, but demonstrates my problem. I tried initing a with or without volatile - same error. I tried type casting it in to the function, no luck.

The code:

unsigned long a = 100;

void main() {
    Pin16_SetAsOutput();
    while(1) {
        a++;
        Pin16_SetHigh();
        __delay_cycles(a);

        Pin16_SetLow();
        __delay_cycles(a);
    }
}
ChrisAdmin
  • 982
  • 1
  • 12
  • 32

1 Answers1

3

Check out the docs - the argument to __delay_cycles must be a compile time constant, you can't pass in a value calculated at runtime; hence the complaint.

This isn't some arbitrary pedantic restriction you can force the tools to ignore, this is done because the way __delay_cycles is implemented makes it impossible to do otherwise. The actual code the compiler generates depends on the value you pass in, so the value must be known at compile time. The compiler emits some number of instructions, possibly including a hardwired fixed length loop, that take exactly the number of cycles you asked for to execute.

If you say const unsigned long a = 100; and take out the a++, that'll work. If you want a variable length delay, you'll need an alternative way to do that, such as putting a __delay_cycles() with a constant argument inside a loop, accounting for the loop overhead when determining the argument.

moonshadow
  • 86,889
  • 7
  • 82
  • 122
  • I see, i could create a function that loops a number of times around the const cycles to reproduce the same effect. don't suppose you know of a better replacement for texas instruments crappy __delay_cycles()? – ChrisAdmin Aug 21 '15 at 11:43