about xmega128a1 rtc overflow period calculate?

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

I read AVR application note code(attachement.)

I don't understand the RTC overflow calculation.

 

for internal 32k  oscillator 

register period=1023

            count=0

            div=1

 

why RTC overflow period=1S?

 

#include "avr_compiler.h"
#include "rtc_driver.h"

#define LED_PORT          PORTD
#define RTC_CYCLES_1S     1024

/*! \brief Struct to hold the BCD second values. */
typedef struct RTC_BCD_struct{
    uint8_t sec_ones;    /*!< Holds the BCD value of the ones. */
    uint8_t sec_tens;    /*!< Holds the BCD value of the tens. */
} RTC_BCD_t;

/*! \brief The main RTC example.
 *
 *  This function demonstrates how the RTC can be used to implement a simple
 *  second counter. The RTC is configured to produce an interrupt once every
 *  second. The RTC overflow interrupt keeps track of time by increasing a
 *  counter each time it is run. The time is shown in binary coded decimal,
 *  using 7 LEDs. Four LEDs connected to pins 0-3 shows single seconds, while
 *  three LEDs connected to pins 4-6 shows tens of seconds. The second counter
 *  wraps at 60, so it starts at 0 once every minute.
 *
 *  Hardware setup:
 *    - Connect LEDs to pins 0-6 on PORTD to watch the clock.
 */
int main(void)
{
    /* 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;

    /* Configure LED port as output. */
    LED_PORT.DIR = 0xFF;

    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();

    do {
        /* Wait while the interrupt executes. */
        nop();
    } while (1);
}

/*! \brief RTC overflow interrupt service routine.
 *
 *  This ISR keeps track of the seconds and displays the current second count
 *  on LEDs connected to PORTD.
 */
ISR(RTC_OVF_vect)
{
    static RTC_BCD_t rtcTime;

    /*  Increase and check if 1's reach top value. Reset 1's and increase
     *  10's if top is reached.
     */
    if ( ++rtcTime.sec_ones > 9 ) {
        rtcTime.sec_ones = 0;
        rtcTime.sec_tens++;
    }

    /* Check if 10's reach top value and reset to zero if it does. */
    if ( rtcTime.sec_tens > 5 ) {
        rtcTime.sec_tens = 0;
    }

    /* Display the inverted counters on active low LEDs. */
    LED_PORT.OUT = ~( ( rtcTime.sec_tens << 4 ) | rtcTime.sec_ones );
}

 

 

Attachment(s): 

This topic has a solution.
Last Edited: Sun. May 16, 2021 - 10:23 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Because you have selected 1.024kHz as the clock source for the RTC.

 

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

Thank you very much! I see.