??? 05/24/04 12:52 Read: times |
#71049 - RE: To capture real time & date with 8052 uC Responding to: ???'s previous message |
Shahzad Aslam:
Your idea is a good one for producing relative time information. There is still a need to initialize this seconds counter to some value that makes sense to the application at hand. And in any instance, to use the seconds number as a calendar value, it has to be setup such that its value of zero means some particular legitimate date. Lastly there is also the issue of what happens to the seconds counter value when the microcontroller loses its VCC power. Either some battery system and a once per second wake up needs to be put in place maintain the count or the counter must be loaded each time the system powers up. All in all it may just be easier to use an RTC chip with a battery to provide all of the above features. Another comment is that on an 8051 the time spent to convert a 32-bit seconds value to YY/MM/DD and HH/MM/SS could be rather lengthy. Quite a number of divides (multi-byte software routines non the less) are required. I have found it to be far more efficient to maintain six memory locations as: SECOND: DS 1 MINUTE: DS 1 HOUR: DS 1 DATE: DS 1 MONTH: DS 1 YEAR: DS 1 .. and then to simply increment the value and cascade the carry, when seconds rolls over from 59 back to 00, up through the higher order bytes. The number of days per month is easily handled with a look up table of 12 bytes length. Leap year extension of month #2 to 29 days can be handled easily by simply considering the case whenever the lowest two bits of year are '00'b. (The leap year scheme like this works fine as long as the dates being kept track of are in the near epoch of time). Here is a simple 8051 assembly language sample of how to handle a HH:MM:SS increment in a manner that is efficient for embedding inside a 1 second timer interrupt context: ; ; ; RTC variables ; RSEG DATA_SEG RTC_DATA: RTC_SECOND: DS 1 ; seconds value RTC_MINUTE: DS 1 ; minutes value RTC_HOUR: DS 1 ; hour value ; ; ; manage HH:MM:SS time clock counter. This counts minutes, seconds and ; hours in the normal manner. ; RTC_INC: MOV R0, #RTC_DATA ; point at the RTC data block INC @R0 ; increment the seconds value CJNE @R0, #60, $+3 JC RTC_DONE ; exit if sec <= 59 MOV @R0, #0 ; reset seconds to 00 INC R0 INC @R0 ; increment the minutes value CJNE @R0, #60, $+3 JC RTC_DONE ; exit if min <= 59 MOV @R0, #0 ; reset minutes to 00 INC R0 INC @R0 ; increment the hours value CJNE @R0, #24, $+3 JC RTC_DONE ; exit if hour <= 23 MOV @R0, #0 ; reset hours to 00 RTC_DONE: .... Here is a little "C" routine which can manage date/month/and year increment and decrement efficiently. /* define the time structure for binary time variables */ struct rtc_time { unsigned char sec; unsigned char min; unsigned char hour; unsigned char date; unsigned char mon; unsigned char year; }; /* global current time structure */ extern struct rtc_time curr_time; /* table of normal year days per month */ unsigned int rtc_mondays[]={31,28,31,30,31,30,31,31,30,31,30,31}; /* ** ** routine to software increment the date in the rtc_time structure ** pointed to by the entry pointer ** */ void date_inc(struct rtc_time *tp) { tp->date++; if(tp->date > rtc_mondays[tp->mon-1]) { if((tp->mon == 2) && ((tp->year & 0x03) == 0) && (tp->date == 29)) return; /* allow keeping leap year day */ tp->date=1; tp->mon++; if(tp->mon > 12) { tp->mon=1; tp->year++; if(tp->year > 99) { tp->year=0; } } } } /* ** ** routine to software decrement the date in the rtc_time structure ** pointed to by the entry pointer ** */ void date_dec(struct rtc_time *tp) { tp->day--; if(tp->day == 0) tp->day=7; /* reset the day number to saturday */ tp->date--; if(tp->date == 0) { tp->mon--; if(tp->mon == 0) { tp->mon=12; /* reset to december */ if(tp->year == 0) { tp->year=99; } else { tp->year--; } } else { if((tp->mon == 2) && ((tp->year & 0x03) == 0)) { tp->date++; /* extra day for feb */ } } tp->date+=rtc_mondays[tp->mon-1]; } } Michael Karas |
Topic | Author | Date |
To capture real time & date with 8052 uC | 01/01/70 00:00 | |
RE: To capture real time & date with 8052 uC | 01/01/70 00:00 | |
RE: To capture real time & date with 8052 uC | 01/01/70 00:00 | |
RE: To capture real time & date with 8052 uC | 01/01/70 00:00 | |
RE: To capture real time & date with 8052 uC | 01/01/70 00:00 | |
RE: To capture real time & date with 8052 uC![]() | 01/01/70 00:00 |