| ??? 12/31/04 19:15 Read: times |
#84184 - Example code Responding to: ???'s previous message |
Following an example code, which was more µC friendly, since it avoid divisions inside loops and use always the smallest possible data format.
It runs with a DS1994 RTC chip to convert 32 bit seconds into time.
/************************************************************************/
/* */
/* Clock / Calendar */
/* */
/* Author: Peter Dannegger */
/* danni@specs.de */
/* */
/************************************************************************/
#pragma pl(30000) cd noco
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned long u32;
typedef signed long s32;
struct time {
u8 second;
u8 minute;
u8 hour;
u8 day;
u8 month;
u16 year;
u8 wday;
};
// 4294967295 sec = 0136 y + 4 m + 16 d + 6 h + 28 min + 15 sec
#define FIRSTYEAR 1990 // start year
#define FIRSTDAY 1 // 0 = Sunday
void gettime( u32 sec, struct time idata *t )
{
u16 day;
u8 year;
u16 dayofyear;
u8 leap400;
u8 month;
u8 code DayOfMonth[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
t->second = sec % 60;
sec /= 60;
t->minute = sec % 60;
sec /= 60;
t->hour = sec % 24;
day = sec / 24;
t->wday = (day + FIRSTDAY) % 7; // weekday
year = FIRSTYEAR % 100; // 0..99
leap400 = 4 - FIRSTYEAR % 400 / 100; // 4, 3, 2, 1
for(;;){
dayofyear = 365;
if( (year & 3) == 0 )
dayofyear = 366; // leap year
if( year == 0 || year == 100 || year == 200 ){ // 100 year exception
if( --leap400 ) // 400 year exception
dayofyear = 365;
}
if( day < dayofyear )
break;
day -= dayofyear;
year++; // 00..136 / 99..235
}
t->year = year + FIRSTYEAR / 100 * 100; // + century
if( dayofyear & 1 && day > 58 ) // no leap year and after 28.2.
day++; // skip 29.2.
for( month = 1; day >= DayOfMonth[month-1]; month++ )
day -= DayOfMonth[month-1];
t->month = month; // 1..12
t->day = day + 1; // 1..31
}
void main (void)
{
u32 sec;
struct time idata current_time;
gettime( 4294967295L, ¤t_time );
for( sec = 0;; sec += 1 ){
gettime( sec, ¤t_time );
}
}
Peter |
| Topic | Author | Date |
| C Code t o Assembly | 01/01/70 00:00 | |
| C to Asm | 01/01/70 00:00 | |
| C Code t o Assembly | 01/01/70 00:00 | |
| only one table | 01/01/70 00:00 | |
| Need some more details | 01/01/70 00:00 | |
| Not even true ! | 01/01/70 00:00 | |
| Answer is Wrong!! | 01/01/70 00:00 | |
| Re : previous 3 replies and Paul | 01/01/70 00:00 | |
| Previously on 8052.com | 01/01/70 00:00 | |
| Overflow | 01/01/70 00:00 | |
| just one array | 01/01/70 00:00 | |
| too limited, Paul | 01/01/70 00:00 | |
| Nothing to do with scope | 01/01/70 00:00 | |
| Agreed | 01/01/70 00:00 | |
| No success ! | 01/01/70 00:00 | |
| Re: Peter | 01/01/70 00:00 | |
| to assembly | 01/01/70 00:00 | |
| Example code | 01/01/70 00:00 | |
| Any unused functions in your program? | 01/01/70 00:00 | |
| query | 01/01/70 00:00 | |
| Off-Topic - start a new thread | 01/01/70 00:00 | |
... and use a _descriptive_ subject! | 01/01/70 00:00 |



