This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

RTC example for XC167

Hi,

Does anyone have C example code on how to use the RTC module?
Thanks

  • This is an interesting question. We use an external serial calendar chip (with its own battery) to keep track of the date and time. In the Infineon manual, it states that it can be used as a "System clock to determine the current time and date". I imagine a battery source would need be necessary to keep track of time when the controller is powered down.

  • Hi,

    We do not need a battery backed up RTC, it only needs to continue running while powered.
    It would be great if someone could provide some C code on how to use the RTC using the main oscillator since we cannot get that to work.

  • I haven't written anything for it. Post the code you couldn't get to work and so we can see if there are any obvious problems with it.

    Shouldn't this just involve setting up a couple registers?

  • First off you may need write access to the SYSCON2 register. This access can only be gotten by using the UNLOCK sequence detailed on page 21-22 of the C167CS Derivatives Manual. Acually you may be able to run with the defaults, you can read the register after startup to see if anything needs to be changed. A description of the register is given on page 21-12 of the C167CS Derivatives Manual.

    Assuming that the SYSCON2 register is set correctly, all you have to do is set your RTCL, RTCH, T14REL and T14 registers to zero. I don't believe that you need to enable any interrupts and you can basically just look at the RTCH, RTCL and T14 to get the current count.

    To be honest, I am not exacly sure which register is used to enable the RTC. There isn't a T01CON type register which enables it. The closest thing I saw was the PDCON bits of the SYSCON2 register.

  • 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.