NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » float to int?

February 27, 2010
by Hexorg
Hexorg's Avatar

I don't now how many band nerds are our here on NerdKits, but I surely am one. Trying to make a metronome. So, I have bpm as an input, and to find delay in micro-seconds i do uint16_t delay1 = 60000000 / bpm; by all the type conversion rules I know, if, say, bpm is 85, i should get 705882 in delay1, but I get something really wierd like 15023, which is a power-of-ten short anyway. First, I though, maybe the math unit was bad, so I made a spare "double tmp;", assigned value to it, and output to the lcd - works great! ... So, is there any special way to convert floating-point values to int?

February 27, 2010
by Hexorg
Hexorg's Avatar

oh! Silly me... was trying to insert a 20bit value into a 16 bit variable

February 27, 2010
by Hexorg
Hexorg's Avatar

ok... Womething is wierd about my calculations....

There are 60 000 000 micro-seconds in a minute, right? Now, if I need x amount of beats per minute i do

60000000 / x; // To find a delay between "ticks", right?

So, If I have 6 leds and I want only the ones at the end to represent a "tick" i do

(60000000 / x) / 6; //or
10000000 / x; //Right?

well... Trouble is it seems like my metronome is working twice as slow as it should... I would just divide the delay by 2 somewhere, but I wanted to find out where exactly my error was.

February 27, 2010
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi Hexorg,

How are you causing your delay? The delay_us and delay_ms functions take a uint16_t parameter -- so anything bigger than 65535 microseconds for delay_us won't do what you are expecting.

You can also make use of the next larger data type, the uint32_t, if you wish. You could write your own "delay_many_us" function which takes a uint32_t parameter, and uses a uint32_t counter. If you peek into the code for delay_us, you'll see that there are just a bunch of NOPs to accomplish a ~1us delay. But because your counter increment and comparison will take more cycles, you'll have to take out some NOPs. Check out this post about delay_us which shows how we're counting cycles.

Finally, another option is to make some use of Timer/Counter modules. See our Crystal Real Time Clock video -- you may be able to modify this for your project. Using a Timer/Counter which fires an interrupt every so often is probably closest to the "right" way of accomplishing what you want, but the delay methods could work for you too.

Let us know which option you pick!

Mike

February 27, 2010
by Hexorg
Hexorg's Avatar

Well, changing the functions from delay.c.h is exactly what I did, and it was causing troubles. I'll take a look at the real time clock, and try that instead, thank you :) I'll let you know how it will turn out. Thanks :)

Post a Reply

Please log in to post a reply.

Did you know that hobby servos are controlled through a particular kind of PWM (Pulse Width Modulation) signal? Learn more...