Hi All,
Any one help me in doing this. I need to continously print the current date and time in DD/MM/YY and HH:MM:SS format. Iam using ARM LPC2378. I am not sure whether my RTC code is correct. Please confirm or suggest if there is something else I need to do. My code which is creating the problem goes as follows:
void RTCInit(void) { PCONP |= 0x0200; /* RTC Power Enabled */ RTC_CCR = 0x11; /* RTC Osci.Clock Enabled */ RTC_ILR |= 0x01; /* RTC Interrupt Enabled */ RTC_AMR |= 0xFF; /* Alarm Mask Register Disabled */ RTC_CIIR = 0x01; /* Every Second Interrupt Enabled */ RTC_CISS = 0x87; /* Sub Second Interrupt Disabled */ return; } void RTCSetDateTime(RTCDateTime DateTime) { RTC_SEC = DateTime.RTCSec; RTC_MIN = DateTime.RTCMin; RTC_HOUR = DateTime.RTCHour; RTC_DOM = DateTime.RTCMday; RTC_MONTH = DateTime.RTCMon; RTC_YEAR = DateTime.RTCYear; return; } RTCDateTime RTCGetDateTime(void) { RTCDateTime LocalDateTime; /* Store RTC date and time in st_LocalDateTime structure */ LocalDateTime.RTCSec = RTC_SEC; LocalDateTime.RTCMin = RTC_MIN; LocalDateTime.RTCHour = RTC_HOUR; LocalDateTime.RTCMday = RTC_DOM; LocalDateTime.RTCMon = RTC_MONTH; LocalDateTime.RTCYear = RTC_YEAR; return (LocalDateTime); }
In some other part of the code I am updating the structure variables to set the time initially. And when I do this I call RTCSetDateTime(initiallocaltime); as follows
{ ... ... /* Reset the counter which is resposible for Sec Timer increment in RTC */ RTC_CCR |= 0x03; /* CTC Reset in CCR */ RTC_ILR |= 0x05; /* RTC Sec Timer Counter Reset*/ /* Updation of RTC with Date & Time initially*/ RTCSetDateTime(initiallocaltime); RTC_CCR = RTC_OSC_SELECT; ... ... }
and finally Iam printing the time. Date and time is printed correctly for some time and later the it jumps above or below (some times in seconds/minutes and mostly in hours) and starts running from there.
Please let me know how can I avoid this problems with RTC and print the correct date and time continously eoth out any disturbances.
Thanks and Regards, Sravan Kumar M.
sir, I have one more doubt. what if RTC_SEC the register itself is giving a wrong value due to some reason. how can I correct RTC_SEC value?
Thanks and Regards, Sravan
"I have one more doubt. what if RTC_SEC the register itself is giving a wrong value due to some reason. how can I correct RTC_SEC value?"
What reason?
Is the RTC_SEC any more likely to return a wrong value than any other register?
Sometimes you have to trust what your given - Otherwise you'll have to check every register on every access?!
But ... On a restart, you might want to check all date/time registers for validity and reset them if the value is obviously wrong; e.g., the RTC might be battery backed and the battery might be flat.
Why do you think RTC_SEC would give the wrong value, if you have made sure that you have set the RTC to a valid time and then made sure that you have had valid supply voltages to the RTC all the time?
After having read the question one more time, I think I can guess the reason for the question.
The RTC_SEC is safe to use to verify that the time haven't changed during the readout of the other registers for the simple reason that none of the other registers will change value unless RTC_SEC also changes its value.
You can only get a change of the minute register if the second register changes 59->0. You can only get a change of the hour register if the second register changes 59->0 and the minute register changes 59->0. You can only get a change of the day-of-month if ... ...
So checking the RTC_SEC register for changes is enough to know if any change may have happened for the minutes, hours, day-of-month, month, year, day-of-year or weekday registers.
Hi,
I Have made the following changes to my code, but still I found the problem in Hours field, lagging an hour to the original time. Viz: The current time is 10:52:26 but My RTC is showing the time as 09:52:26 (only hours is getting disturbed).
Changes in the code:
void RTCInit(void) { PCONP |= 0x0200; /* RTC Power Enabled */ RTC_CCR = 0x12; /* RTC Osci.Clock Selected */ RTC_ILR |= 0x07; /* Clear all pending interrupts */ RTC_AMR |= 0xFF; /* Alarm Mask Register Disabled */ RTC_CIIR = 0x00; /* Disable all interrupts */ RTC_CISS = 0x00; /* Sub Second Interrupt Disabled */ RTC_CCR = 0x11; /* RTC Osci.Clock Enabled */ return; } void RTCSetDateTime(RTCDateTime DateTime) { RTC_CCR &= FE; RTC_SEC = DateTime.RTCSec; RTC_MIN = DateTime.RTCMin; RTC_HOUR = DateTime.RTCHour; RTC_DOM = DateTime.RTCMday; RTC_MONTH = DateTime.RTCMon; RTC_YEAR = DateTime.RTCYear; RTC_CCR |= 01; return; } RTCDateTime RTCGetDateTime(void) { RTCDateTime LocalDateTime; /* Store RTC date and time in st_LocalDateTime structure */ LocalDateTime.RTCSec = RTC_SEC; LocalDateTime.RTCMin = RTC_MIN; LocalDateTime.RTCHour = RTC_HOUR; LocalDateTime.RTCMday = RTC_DOM; LocalDateTime.RTCMon = RTC_MONTH; LocalDateTime.RTCYear = RTC_YEAR; return (LocalDateTime); }
Iam not disturbing the registers anywhere else as I did in previous code.. above.. But still the problem exists. Please let me know what else care can be taken to get the correct timestamps in HH:MM:SS. Do I need any Interrupt kind of things???
Strange that your new post doesn't contain the fixes that was suggested earlier, to handle a turnover of the seconds field - did you not think you needed it? And still lots of copying of full structures.
You haven't showed us how you verify the contents of the hour field - how do you print the data? Can you really do one call to RTCSetDateTime() and directly after call RTCGetDateTime() and the hour field has ticked one step?
Do you possibly involve a PC that is making use of any time zone so one of your printouts shows UTC time and one printout shows local time?
Sir, I think that the fixes you mentioned previously are for rollover. Am I right? I believe that the problem is not with rollover.. Some how only the hours field is changing its value and continues time from there.. if it is rollover as you mentioned.. if the time is 12:59:59 it should show 13:59:59 and has to continue with 13:00:00 isn't it? But my time is continued with 13:59:59.. means from the next second it runs from 14:00:00.
I am trying print the time continously in while(1) loop in the main().
So you think that if you have never seen the problem with rollover, you will not need to implement such protection? How clever do would you say such a decision is?
You still didn't respond to my other comments. How did you print the value you set the clock with?
Have you also verified that you test with a minimum of code so you don't have other code that accidentally touches your data? And have you verified that your supply voltages are always peachy? Including the voltage on the VBAT pin?