View previous topic :: View next topic |
Author |
Message |
skiffex
Joined: 04 Feb 2005 Posts: 5
|
ms into MIN and SEC algorithm |
Posted: Sat Feb 05, 2005 5:00 pm |
|
|
Hi, i'm looking for extremly fast (read effecient) algorithm of converting the milliseconds into minutes and seconds : something like Code: |
temp=time_ms/1000;
time_min=temp/60;
time_sec=temp%60;
|
but much more optimized.
Can somebody help me? |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Sat Feb 05, 2005 6:48 pm |
|
|
You're not giving us much to go on, but if you are counting the ms yourself, how about just using registers for your seconds and minutes as you count up. Each time the milliseconds hits 1000, clear them and increment the seconds. If the seconds is 60, then clear the seconds and increment the minutes. This adds a little overhead to each cycle, but it is just increment and compares, and you don't need to do much math to display the resulting time. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Feb 05, 2005 7:20 pm |
|
|
Skiffex,
Let me guess: Your 'time_ms' is a 32-bit variable and you found out that 32-bit division is very slow?
Charlie U's suggestion is the same line as I was thinking.
The best implementation I have seen was given by Neutone. Special in this routine is that despite the milliseconds interrupt not being exactly at the millisecond accurate, the seconds have a zero long time drift.
Code: | //RTC variables
#define XTAL_FREQUENCY 19660800
int32 Ticker;
int8 seconds=0;
//optional:
//int8 year=0,Month=0,days=0,hours=0,minutes=0;int1 leapyear=0;
/*********************************************
* Initialize RTC *
*********************************************/
void Initialize_RTC(void)
{ Ticker = XTAL_FREQUENCY;
setup_timer_2(T2_DIV_BY_4, 0xff, 4); // initialize 8-bit Timer2 to interrupt about
// every 1ms (16384 clock cycles)
enable_interrupts( INT_TIMER2 ); // Start RTC
}
// Global Real Time Clock Information
#int_TIMER2 // Clock interrupt adjusted to occurs ~ 1ms
void TIMER2_isr() // -=Process Zero Drift Real Time Clock Information=-
{
Ticker -= 16384; // Decrement ticker by clocks per interrupt
if ( Ticker < 16384 ) // If second has expired
{ Ticker += XTAL_FREQUENCY; // Increment ticker by clocks per second
second++; // Increment number of seconds
}
// Optional:
/*
if(seconds == 60) {minutes++;seconds = 0;
if(minutes == 60) {hours++;minutes = 0;
if(hours == 24) {days++;hours= 0;
if(days == 29 && Month==2 && !leapyear){Month++;days=0;}
if(days == 30 && Month==2){Month++;days=0;}
if(days == 31 && (Month==4 || Month==6 || Month==9 || Month==11 )){Month++;days=0;}
if(days == 32) {Month++;days=0;
if(Month == 13) {year++;Month=0;
if((year & 3) == 0) {leapyear=1}
else{leapyear=0}
}}}}}}
*/
} |
|
|
|
|