Hi guys!
I've a strange problem in CodeVision for programming a Ateml mega16.
it's the command:
void main () {
signed int d;
...
delay_us (d);
when i compiled the program, an error happened by this meaning only for red line:
constant integral expression required
why? i've defined the range of "d" between 1 to 100.
it never accepts the delay_us command and shows above error!
but when i change it to delay_ms , it compiles completely without error.
why!? can i replace this code:
delay_ms(d/1000)
instead of
delay_us
?
please please reply in English! otherwise i should use translator...
daley_us or delay_ms!?
Moderators: pedja089, stojke369, trax, InTheStillOfTheNight
Re: daley_us or delay_ms!?
That's not strange. delay_us can't be used with variable because it relies on the floating point parameter being calculated at compile time, so there's no need for run-time floating point calculation which would severely skew the actual delay time.
If you need a variable delay, do something like:
or use a timer. So, load the timer with a variable, possibly at prescale=1 and wait for the enabled timer interrupt.
This way you can still process code while the timer counts.
Have fun
If you need a variable delay, do something like:
Code: Select all
void my_delay_us(int n) {
while(n--) {
delay_us(10);
}
}
This way you can still process code while the timer counts.
Have fun
Re: daley_us or delay_ms!?
tanx for reply!
so it's forbidden to use variables in delay_us command!
i didn't know that...
about this code which you suggested:
i can't understand it completely!
i mean, where i should use code, within the program lines? is it a local command?
so sorry! I'm not so familiar in programming!
can i use this code, and change D value freely??
this code was compiled successfully! but, does it work real time?
for waiting 100us. i choose the D=10.
so it's forbidden to use variables in delay_us command!
i didn't know that...
about this code which you suggested:
Code: Select all
void my_delay_us(int n) {
while(n--) {
delay_us(10);
}
}
i mean, where i should use code, within the program lines? is it a local command?
so sorry! I'm not so familiar in programming!
can i use this code, and change D value freely??
this code was compiled successfully! but, does it work real time?
for waiting 100us. i choose the D=10.
Code: Select all
for (D=0 ; D<d ; D++)
{
delay_us(10);
}
Re: daley_us or delay_ms!?
Your code works too, but i think you want to assign d=10 instead D=10
If interested in myne example, you could learn more about functions first.
http://newdata.box.sk/bx/c/htm/ch05.htm
If interested in myne example, you could learn more about functions first.
http://newdata.box.sk/bx/c/htm/ch05.htm