View previous topic :: View next topic |
Author |
Message |
Christoph Guest
|
delay with variable |
Posted: Mon Feb 27, 2006 9:10 am |
|
|
hello!
i´m shure it is very simple, but:
why it is not the same?
"delay_ms(1000);"
AND
"
int X;
X=1000;
delay_ms(X);
"
with the first statement the delay is exactly 1second,
but with the second one it is much lower... why?
thanks in advance, christoph |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Feb 27, 2006 9:28 am |
|
|
The first is using 1000
the second uses a 1000 stored in a 8 bit variable.
8 bit can at max hold 255.
so
1000=0x3E8 only the E8 is stored
thus e8=232
you get 232mS
CCS int is by default and int8 |
|
|
Christoph Guest
|
|
Posted: Mon Feb 27, 2006 9:35 am |
|
|
okay, i see...
should it work with an int16? |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Feb 27, 2006 9:40 am |
|
|
To find the biggest number a variable can hold take 2^n
to 2^16 = 65535 BUT
delay_ms will take an int8 variable.
Thought to look it up after I pressed submit.
edited...
Look to the link of PCM below. It shows the delay_ms inside a wrapper
that executes it multi times and DOES take the int16 variable.
Last edited by treitmey on Mon Feb 27, 2006 9:44 am; edited 3 times in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 27, 2006 9:41 am |
|
|
The delay_ms() function only accepts an 8-bit value when the parameter
is a variable. It can accept a 16-bit constant. To use a 16-bit variable
you need to use a loop. See this thread for an example function:
http://www.ccsinfo.com/forum/viewtopic.php?t=375 |
|
|
Christoph Guest
|
|
Posted: Mon Feb 27, 2006 9:47 am |
|
|
thank you...
i understand.
but when i´m using int16 it´s also not working.
the delay is still not 1 sec... |
|
|
Christoph Guest
|
|
Posted: Mon Feb 27, 2006 9:50 am |
|
|
thank you guys...
i will try! |
|
|
Guest
|
|
Posted: Mon Feb 27, 2006 4:22 pm |
|
|
Christoph wrote: | thank you guys...
i will try! |
Better yet to set up a timer interrupt so that you can track time and not have to block waiting for the delay() call at all.
DLC |
|
|
|