XMega128A1 rtc question

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm trying to get the RTC to work. I'm using code out of the example rtc_example.c and rtc_driver.c The RTC.CNT register never changes and interrupt never occurs. I've tried the ULP clock source and the ROSC sources. I've tried setting the prescaler directly... I'm wondering if anyone else figured this out or had similar problems.

/* Turn on internal 32kHz. */
        OSC.CTRL |= OSC_RC32KEN_bm;

        do {
                /* Wait for the 32kHz oscillator to stabilize. */
        } while ( ( OSC.STATUS & OSC_RC32KRDY_bm ) == 0);


        /* Set internal 32kHz oscillator as clock source for RTC. */
        CLK.RTCCTRL = CLK_RTCSRC_RCOSC_gc | CLK_RTCEN_bm;

        do {
                /* Wait until RTC is not busy. */
        } while ( RTC_Busy() );

        /* Configure RTC period to 1 second. */
        RTC_Initialize( RTC_CYCLES_1S, 0, 0, RTC_PRESCALER_DIV1_gc );

        /* Enable overflow interrupt. */
        RTC_SetIntLevels( RTC_OVFINTLVL_LO_gc, RTC_COMPINTLVL_OFF_gc );

        /* Enable interrupts. */
        PMIC.CTRL |= PMIC_LOLVLEN_bm;
        sei();
        
        while(true) {
             printf_P(PSTR("\r\nrtc: %04x"), RTC.CNT);

        }


ISR(RTC_OVF_vect) {
        rtc_tick=1;
        //PORT_TogglePins( &PORTQ, PIN2_bm );
        PORTQ.OUT ^= (1<<2);
}

	 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have just compiled AVR1314 app note with CodeVision and it compiled fine.

I am running it on an XPLAIN board with the LEDs on PORTE. They are blinking merrily.

I will try with avr-gcc.
Yep. It works fine with -O0 and with -Os

Please could you post your complete code. It does not look as if you are doing much different to the AVR1314 code.

Yippee. I think that I have found a workaround for the "percent" bug on this forum. I noticed that your format string got through. So it appears that a 'percent' followed by a 0 or a non-alphanumeric works ok.

             printf_P(PSTR("\r\nrtc: %04x"), RTC.CNT);

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for trying that out. It gives me hope that I will get this figured out pretty soon. I didn't post the rest of the code because it is a bunch of stuff that isn't relavent to the XPLAIN board. I'm glad to hear the printf format string helped out. I didn't realize there was any problems with that.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is working now, but not sure what the problem was... I tried so many things. The RTC started working first, then I had to change the interrupt level to medium before interrupts worked.

Here is what is working for me.

 /////////////////////////////////////////////
        //  prepare 1hz rtc interrupt
        /////////////////////////////////////////////

        do {
                /* Wait until RTC is not busy. */
        } while ( RTC_Busy() );
        RTC_Initialize( RTC_CYCLES_1S, 0, 0, RTC_PRESCALER_OFF_gc );



        /* Set internal 32kHz oscillator as clock source for RTC. */
        CLK.RTCCTRL = CLK_RTCSRC_RCOSC_gc | CLK_RTCEN_bm;
        //CLK.RTCCTRL = (CLK_RTCSRC_ULP_gc | CLK_RTCEN_bm);

        do {
                /* Wait until RTC is not busy. */
        } while ( RTC_Busy() );

        /* Configure RTC period to 1 second. */
        RTC_Initialize( RTC_CYCLES_1S, 0, 0, RTC_PRESCALER_DIV1_gc );


        /* Turn on internal 32kHz. */
        OSC.CTRL |= OSC_RC32KEN_bm;

        do {
                /* Wait for the 32kHz oscillator to stabilize. */
        } while ( ( OSC.STATUS & OSC_RC32KRDY_bm ) == 0);


        //did this later in code after delay... not sure if that matters

        RTC_SetIntLevels( RTC_OVFINTLVL_MED_gc, RTC_COMPINTLVL_OFF_gc );

        /* Enable all levels of interrupts. */
        PMIC.CTRL = 0x07;

        sei();