daley_us or delay_ms!?

Hier finden sie die archivierten Beiträge des alten PHPBB Forums von www.loetstelle.net

daley_us or delay_ms!?

Von masoud66 am 08.06.2011 19:08

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

please please reply in English! otherwise i should use translator... :(

Von Elektron am 08.06.2011 23:04


Von masoud66 am 09.06.2011 01:43

tanx for reply
your right. now i read in a book that delay_us command, can be used with a fixed number only! no variable. for example delay_us(100), NOT delay_us(d)

when i changed it to a fixed number, problem solved and no error happened.

but i should be able to control and change this delay time, in us scale!!! so i should use variables, which is forbidden and impossible!

a friend, suggested to use this code:

void my_delay_us(int n) {
while(n--) {

I've never used this form of command before! i can't induct it!
can u explain me?

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.

     for (D=0 ; D<d ; D++)

Von Elektron am 09.06.2011 13:51

The problem is that the compiler wants to have a constant expression. You can trick him by casting to a constant expression:


int a = 100;
delay_us((const int)a);

This looks really weird, but it should make your program compile.

Von derguteweka am 09.06.2011 18:48


masoud66 hat folgendes geschrieben:
...this code was compiled successfully! but, does it work real time?
for waiting 100us. i choose the D=10.

     for (D=0 ; D<d ; D++)

Yes and no. It might do something like you expected, but it's not accurate. The time, which is needed for the "for" statement (which results usually in some increment, compare and "branch-if-not-zero" assembler instructions) is not taken into account.

I doubt, that anyone is able to write proper code in C, with execution times this precisely predictable.
If you really need the accuracy in microseconds, imho you have to use assembler instead of C and look into the datasheet, how many cycles every asm instruction will take.


Von Sepp am 20.01.2012 14:49


_delay_us(1000) eg. _delay_ms(1000)

You forgot the first _

and you need to include this:
#include <util/delay.h>