View previous topic :: View next topic |
Author |
Message |
bwgames
Joined: 21 Jul 2005 Posts: 36
|
delay_ms using floats |
Posted: Wed Aug 23, 2006 4:00 am |
|
|
Does delay_ms support using floats?
e.g. I have a variable, delay, which has the value 3.999999E+01 (from fprintf,"%f,delay).
I then use delay_ms(delay);.
Should this work? I seem to be getting strange results (excessively long times etc)... |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Aug 23, 2006 5:11 am |
|
|
From the C Reference Manual:
_____________________________________________________
DELAY_MS()
Syntax: delay_ms(time)
Parameters: time a variable 0-255 or a constant 0-65535
_____________________________________________________
Using the CCS built in delay functions with an integer number as parameter you can get delays from the nanosec to hours, what is your real need to use a float as parameter?
Humberto |
|
|
bwgames
Joined: 21 Jul 2005 Posts: 36
|
|
Posted: Wed Aug 23, 2006 5:21 am |
|
|
I have an int32 which contains a sample frequency in Hz.
I want to convert this to the corresponding delay time.
This works fine, by doing:
Code: |
//sample_num, delay and sample_speed are all defined as INT32.
sample_num = atoi32(number_samples);
//Convert sample speed to time delay between samples.
//THIS CODE ONLY WORKS WHEN MAX FREQ IS <= 1KHZ!!!!!!!!
delay = 1.0/(sample_speed); //gets time from frequency (1/f =t)
delay = delay*1000; //converts delay in seconds to delay in mS
|
However inserting that into delay_ms appears to give strange results...? E.g. 100mS+ instead of 10mS.
It is definitely the correct 'delay' value, this has been confirmed by printf.
On a second note, ideally I would like a way to expand this beyond 1Khz, anyone have any ideas?
This would entail using delay_us, but I need a way to differentiate between when us and ms is needed? |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Aug 23, 2006 5:29 am |
|
|
Quote: |
However inserting that into delay_ms appears to give strange results...? E.g. 100mS+ instead of 10mS.
|
Whenever the parameter is bigger than 255 the result will be unpredictable.
Quote: |
On a second note, ideally I would like a way to expand this beyond 1Khz, anyone have any ideas?
|
You can do it using an int32 variable and decrementing it accordingly inside a loop.
Code: |
delay = 1.0/(sample_speed); //gets time from frequency (1/f =t)
delay = delay*1000; //converts delay in seconds to delay in mS
do
{
delay_ms(1);
}while(--delay);
|
Humberto |
|
|
bwgames
Joined: 21 Jul 2005 Posts: 36
|
|
Posted: Wed Aug 23, 2006 6:23 am |
|
|
Cheers, I tried implementing that, but I got
Line 1160(15,20): Floating point numbers not supported for this operation
If I do (int32) typecasting, will it round it up to the nearest integer?
I.e. i have 4.999E+2, will it become 500? |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Aug 23, 2006 7:09 am |
|
|
Quote: |
Line 1160(15,20): Floating point numbers not supported for this operation
|
I donīt know what are you trying to do using float, but I guess that you misunderstand the use of float and int32.
Humberto |
|
|
Darren Rook
Joined: 06 Sep 2003 Posts: 287 Location: Milwaukee, WI
|
|
Posted: Wed Aug 23, 2006 7:26 am |
|
|
FYI - In version 4, you can pass 16bit variables to delay_ms() and delay_us(). _________________ I came, I saw, I compiled. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Aug 23, 2006 7:39 am |
|
|
If you start with an Int frequency I would scale it and divide to produce an Int microseconds. Then I would loop delay_us(255) as many times as needed, plus another delay_us() for the remainder. Simple, straightforeward, with no costly floating point overhead needed.
Ideally this should be done with timers, but try the above first. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
bwgames
Joined: 21 Jul 2005 Posts: 36
|
|
Posted: Wed Aug 23, 2006 8:32 am |
|
|
Humberto wrote: | Quote: |
Line 1160(15,20): Floating point numbers not supported for this operation
|
I donīt know what are you trying to do using float, but I guess that you misunderstand the use of float and int32.
Humberto |
Apologies.
I meant, I have a int32 which contains the sample speed in Hz.
I then convert this to a delay time as a float, using 1.0/sample_speed, which then gets multiplied by 1000 to convert it into mS.
For example, 10Khz = 10,000 Hz = 0.0001 secs delay, or 0.1 mS delay.
I got round the general problem by type-casting to int32 to use a for loop, however as I understand it, speeds over 1Khz still wouldn't be supported?
E.g. the above 10Khz delay as 0.1mS would typecast to int32 as 0.
However I believe by changing my code to convert it to uS and use delay_us instead of delay_ms in the loop, I can support up to 10Khz now.
Thanks |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Wed Aug 23, 2006 10:13 am |
|
|
The best way, in my opinion, to make delays is to use one of the timers. This will give you a highly accurate time with good resolution. Delay_ms() simply forces the processor to sit and loop, forcing all of your other functions to wait. Timers allow you to count cycles until an event is ready, allowing the processor to run all of it's other functions while you wait.
Ronald |
|
|
|