CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

delay_ms using floats

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
bwgames



Joined: 21 Jul 2005
Posts: 36

View user's profile Send private message

delay_ms using floats
PostPosted: Wed Aug 23, 2006 4:00 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 23, 2006 5:11 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 23, 2006 5:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 23, 2006 5:29 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 23, 2006 6:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 23, 2006 7:09 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Wed Aug 23, 2006 7:26 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 23, 2006 7:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 23, 2006 8:32 am     Reply with quote

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 Smile
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed Aug 23, 2006 10:13 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group