Go Down

Topic: Time stamps (Read 624 times) previous topic - next topic

legoEngineer2011

I have to send a 2 byte time stamp via serial to my computer.
This will consist of how many milliseconds since the arduino started.
This time stamp needs will roll over every 65 seconds and that's fine. However I don't know how to generate it.

Should I use millis() ?
I don't know how to truncate the least significant 2 bytes and separate them into individual bytes.
Also, is this even possible with millis() accuracy?

Should I just count time0 ?
How could I keep track of every roll over while still running the rest of my code?

Thank you, any comments are greatly appreciated.

Coding Badly

Quote
Should I use millis() ?


Yes.

Quote
I don't know how to truncate the least significant 2 bytes and separate them into individual bytes.


You don't need to.  The compiler will do it for you.

Quote
Also, is this even possible with millis() accuracy?


Yes.

Quote
How could I keep track of every roll over while still running the rest of my code?


No need.

Just cast the return value from millis to "unsigned short"...

Quote
void loop( void )
{
  Serial.print( (unsigned short) millis(), DEC );
}

legoEngineer2011

Thank you that was extremely helpful.

I added the & 0xFFFF to get the least significant 16 bits.
Serial.print( (((unsigned short) millis()) & 0xFFFF), HEX );

Is there a way to get ride of the least significant 8 bits, so I am only printing 1 byte?

Coding Badly

I added the & 0xFFFF to get the least significant 16 bits.


That wasn't necessary but a bitwise-and will help with the next request...

Quote
Is there a way to get ride of the least significant 8 bits, so I am only printing 1 byte?


(millis() >> 8) & 0xFF

Go Up