问题
I'm using this function:
__delay_cycles(var);
and I get the following error:
Argument to
_delay_cycles
must be a constant expression
Fair enough! But how can I bypass this? I have to delay my program with a different value every time. I receive my data from RS232 and I sore it in an int
variable. I have to use this function and I can't modify its structure. I'm using AtMega16.
回答1:
One suggestion that immediately springs to mind is to call __delay_cycles()
with a constant argument, but do it in a loop, and vary the number of loop iterations.
The loop will add some overhead, so if you need precision you'll have to subtract the (constant) cost of one loop iteration from the (constant) argument to __delay_cycles()
.
回答2:
Don't use that function. It is apparently some non-standard Texas junk that doesn't behave according to the rules of the C language. Write your own delay function using on-chip timers instead, or find one on the net. Takes less than 1 hour of work, which is no doubt less time than you will spend pondering the meaning of various non-standard junk.
The real reason why the embedded industry have so many crappy compilers, is because embedded programmers accept to be constantly fed with non-standard junk, even when there is no reason what-so-ever to deviate from the C standard.
回答3:
if(var==1)
__delay_cycles(1);
else if(var==2)
__delay_cycles(2);
else if(var==3)
__delay_cycles(3);
...and so on.
来源:https://stackoverflow.com/questions/8056930/how-to-use-a-variable-as-a-parameter-in-a-function-that-only-suports-constant-ex