Hi, Does anyone have C example code on how to use the RTC module? Thanks
The following code is what I am trying to get to run. The interrupt is only there for testing, in the production code I do not really need it (so RTC_IC_IE = 1; is only there to do some testing now). The interrupt, however, never occurs. And when calling RtcGetTime, it always returns 0. In the debugger T14 is always 0 and is never incremented. During startup, the syscon register is set so the RTC runs synchronously to enable me to read/write the RTC registers. Since I do not have an auxillary oscillator, I cannot try to run with RTC_CON = 0x8003.
interrupt (0x5D) void RTC(void) { if (T14IR == 1) { T14IR = 0; if (P3_7 == 1) { P3_7 = 0; } else { P3_7 = 1; } } } int RtcInit(void) { int nResult = 0; if ((ACCPOS == 0) || (_getbit(SYSCON3, 14) == 1)) { /* No write access possible or RTC HW disabled */ nResult = -1; } else { RTC_CON = 0x8013; RTC_T14REL = 0x0010; RTC_RELL = 0x0020; RTC_RELH = 0x0030; RTC_IC = 0x7F; T14IE = 1; RTC_IC_IE = 1; } return nResult; } void RtcSetTime(tTimeOfDay time) { RTC_T14 = time.msecOfDay & 0x0FFFFFFFL; RTC_RELL = 0; RTC_RELH = time.days << 6; } void RtcGetTime(tTimeOfDay *pTime) { pTime->msecOfDay = RTC_T14 & 0x0FFFFFFFL; pTime->days = RTC_RTCH >> 6; }
Oops, In the previous example, RtcSetTime is incorrect as it needs to write to RTC_RTCH and RTC_RTCL instead of writing to RTC_RELL and RTC_RELH; But even with this improvement it does not work :-(
void RtcSetTime(tTimeOfDay time) { RTC_T14 = time.msecOfDay & 0x0FFFFFFFL; RTC_RTCL = 0; RTC_RTCH = time.days << 6; }
Now I understand better. Unfortunately I use the C167CS which is different than the XC167. As we are using different processors, I don't think I can help you. Are you sure that you are writing to the RTC_CON register? Try writing to the RTC_CON register last, after the write to RTC_IC_IE. In your init routine, call the RtcSetTime routine to set a time. Set a non-zero time. When you call RtcGetTime you should at the very least see the non-zero time. Sorry that I couldn't help. If you figure it out, perhaps you could post the solution here.