daley_us or delay_ms!?

Rasprava o AVR mikrokontrolerima, AVR projekti i drugo vezano za AVR...

Moderators: pedja089, stojke369, trax, InTheStillOfTheNight

Post Reply
masoud
Pocetnik na forumu
Pocetnik na forumu
Posts: 28
Joined: 13-02-2009, 22:12

daley_us or delay_ms!?

Post by masoud »

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
? :roll:

please please reply in English! otherwise i should use translator... :(
User avatar
Kizo
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 387
Joined: 01-11-2009, 22:40
Location: Hrvatska

Re: daley_us or delay_ms!?

Post by Kizo »

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:

Code: Select all

void my_delay_us(int n) { 
 while(n--) { 
 delay_us(10); 
 } 
}
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. :wink:

Have fun
masoud
Pocetnik na forumu
Pocetnik na forumu
Posts: 28
Joined: 13-02-2009, 22:12

Re: daley_us or delay_ms!?

Post by masoud »

tanx for reply!
so it's forbidden to use variables in delay_us command! :o
i didn't know that... :oops:

about this code which you suggested:

Code: Select all

    void my_delay_us(int n) {
    while(n--) {
    delay_us(10);
    }
    }
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! :oops:

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);
     } 
User avatar
Kizo
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 387
Joined: 01-11-2009, 22:40
Location: Hrvatska

Re: daley_us or delay_ms!?

Post by Kizo »

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
Post Reply