1024hz to milliseconds

I need accurate time for my project — specifically milliseconds.

I plan to use a DS3231 RTC and it's 1024hz output.

I can keep count of the output but of course I am stuck with a "millisecond" of 1024 every second.

What's the most elegant way of converting a range of 0 to 1024 to 0 to 1000?

My plan is to keep a count (that rolls from 1023 to 0) and then whenever I request a millisecond do the maths to convert it from 0-999

I can either do the obvious "hzCount * (hz/1000)" or I could keep a look up table in memory and just do "msLUT[hzCount]"

The truth is that I'll probably be calling a getMillis() every loop.

Anything I'm missing? I'm really keen to learn about how to do these things as efficiently as possible.

James

Have you looked at the way Arduino tackles this problem?

Why didn't used DS1307. You get accurate.problem need to use external crystal oscillator. I think it should work with library. Instead of conversion.

GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks library avialble here, Let me know if any problem in output. share your code and snapshot of the output window

So something like this:

	ms += 1
	fracms += 1
	if (fracms == 42) {
		fracms = 0
		ms -= 1 }
	if (ms == 1000) {
		ms = 0 }

Every 42 beats it corrects itself.

Seems fair enough.

AMPS-N:
Why didn't used DS1307. You get accurate.problem need to use external crystal oscillator. I think it should work with library. Instead of conversion.

GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks library avialble here, Let me know if any problem in output. share your code and snapshot of the output window

I think you've misunderstood the question.
The DS1307 doesn't have a 1.024kHz output, only 4.096, 8.192 and 32.768kHz and 1Hz.

JamesCarruthers:
I need accurate time for my project — specifically milliseconds.
...SNIP....

Sorry, but I can't understand your post - even after having a quick look at the DS3231 datahsheet.
I have no idea what you want to achieve or even how you think you might do it.

What is your project?
What amount of error in timing is acceptable? ( +/- how many microseconds or parts-per-million )

...R

I read it as "I want to be able to timestamp to (roughly) millisecond accuracy"

AWOL:
I read it as "I want to be able to timestamp to (roughly) millisecond accuracy"

Yup, exactly.

I'm trying to work out if using a higher hz would give me a more accurate millisecond by using the same technique.

I guess it's a case of how accurate do I need...

JamesCarruthers:

AWOL:
I read it as "I want to be able to timestamp to (roughly) millisecond accuracy"

Yup, exactly.

You are still not expressing your requirement clearly.

It sounds like you are content with an accuracy of +/- 1 msec, but you don't say how long that must be maintained for.

If you want accuracy over a longer period than the Arduino can naturally maintain I would read millis() and the RTC periodically and keep a correction factor variable that represents how much has to be added or subtracted to/from the value for millis() to stay in step with the RTC. In other words if I wanted to record the time of an event I would save it as

eventTime = millis() + correction;

Would it not be easier just to read the RTC when the event occurs and ignore millis() altogether?

...R

In the same way I can get seconds() I'd like to be able to get milliseconds()

Accurate as possible within reason.

I'm using them to drive a strip of LEDs — at the moment I'm using only 60 and slight variances are noticeable. It's surprising how the eye can pick up on tiny millisecond differences. So at the moment I only need 1/60 accuracy.

I'd like a solution that is scalable to a large number of LEDs

JamesCarruthers:
I'm using them to drive a strip of LEDs

If you just want to flash LEDs in a pattern and you are driving all the LEDs from a single Arduino then just use millis().

If they are not staying in sync with each other the way you want that is because you are not using millis() properly - not because of a deficiency in millis().

This demo several things at a time illustrates how to use millis().
...R

Why not implement a bresenham algrithm with a ratio of 125/128?

AMPS-N:
Why didn't used DS1307. You get accurate.problem need to use external crystal oscillator. I think it should work with library. Instead of conversion.

GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks library avialble here, Let me know if any problem in output. share your code and snapshot of the output window

Hi Jack,

I have your library working — many thanks — saved me some time.

I have set my squarewave to 1024Hz — but am unsure how to test it's actually working. I have it attached to a pin and attachInterrupt running a counter function on RISING. But no luck.

JamesCarruthers:
In the same way I can get seconds() I'd like to be able to get milliseconds()

Accurate as possible within reason.

I'm using them to drive a strip of LEDs — at the moment I'm using only 60 and slight variances are noticeable. It's surprising how the eye can pick up on tiny millisecond differences. So at the moment I only need 1/60 accuracy.

I'd like a solution that is scalable to a large number of LEDs

I'm with Robin on this, if your timing errors are noticeable to the human eye, you're probably way beyond millisecond error. What kind of LED strips are these?