Xmega Application Note


rtc_example.c File Reference


Detailed Description

XMEGA RTC driver example source.

This file contains an example application that demonstrates the RTC driver.

Application note:
AVR1314: Using the XMEGA Real Time Counter
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
1569
Date
2008-04-22 13:03:43 +0200 (ti, 22 apr 2008)

Copyright (c) 2008, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file rtc_example.c.

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

Include dependency graph for rtc_example.c:

Go to the source code of this file.

Data Structures

struct  RTC_BCD_struct
 Struct to hold the BCD second values. More...

Defines

#define LED_PORT   PORTD
#define RTC_CYCLES_1S   1024

Typedefs

typedef struct RTC_BCD_struct RTC_BCD_t

Functions

 ISR (RTC_OVF_vect)
 RTC overflow interrupt service routine.
int main (void)
 The main RTC example.


Define Documentation

#define LED_PORT   PORTD

Definition at line 54 of file rtc_example.c.

Referenced by ISR(), and main().

#define RTC_CYCLES_1S   1024

Definition at line 55 of file rtc_example.c.

Referenced by main().


Typedef Documentation

typedef struct RTC_BCD_struct RTC_BCD_t


Function Documentation

ISR ( RTC_OVF_vect   ) 

RTC overflow interrupt service routine.

This ISR keeps track of the seconds and displays the current second count on LEDs connected to PORTD.

Definition at line 120 of file rtc_example.c.

References LED_PORT, RTC_BCD_struct::sec_ones, and RTC_BCD_struct::sec_tens.

00121 {
00122         static RTC_BCD_t rtcTime;
00123 
00124         /*  Increase and check if 1's reach top value. Reset 1's and increase
00125          *  10's if top is reached.
00126          */
00127         if ( ++rtcTime.sec_ones > 9 ) {
00128                 rtcTime.sec_ones = 0;
00129                 rtcTime.sec_tens++;
00130         }
00131 
00132         /* Check if 10's reach top value and reset to zero if it does. */
00133         if ( rtcTime.sec_tens > 5 ) {
00134                 rtcTime.sec_tens = 0;
00135         }
00136 
00137         /* Display the inverted counters on active low LEDs. */
00138         LED_PORT.OUT = ~( ( rtcTime.sec_tens << 4 ) | rtcTime.sec_ones );
00139 }

int main ( void   ) 

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.

Definition at line 78 of file rtc_example.c.

References LED_PORT, RTC_Busy, RTC_CYCLES_1S, RTC_Initialize(), and RTC_SetIntLevels().

00079 {
00080         /* Turn on internal 32kHz. */
00081         OSC.CTRL |= OSC_RC32KEN_bm;
00082 
00083         do {
00084                 /* Wait for the 32kHz oscillator to stabilize. */
00085         } while ( ( OSC.STATUS & OSC_RC32KRDY_bm ) == 0);
00086 
00087 
00088         /* Set internal 32kHz oscillator as clock source for RTC. */
00089         CLK.RTCCTRL = CLK_RTCSRC_RCOSC_gc | CLK_RTCEN_bm;
00090 
00091         /* Configure LED port as output. */
00092         LED_PORT.DIR = 0xFF;
00093 
00094         do {
00095                 /* Wait until RTC is not busy. */
00096         } while ( RTC_Busy() );
00097 
00098         /* Configure RTC period to 1 second. */
00099         RTC_Initialize( RTC_CYCLES_1S, 0, 0, RTC_PRESCALER_DIV1_gc );
00100 
00101         /* Enable overflow interrupt. */
00102         RTC_SetIntLevels( RTC_OVFINTLVL_LO_gc, RTC_COMPINTLVL_OFF_gc );
00103 
00104         /* Enable interrupts. */
00105         PMIC.CTRL |= PMIC_LOLVLEN_bm;
00106         sei();
00107 
00108         do {
00109                 /* Wait while the interrupt executes. */
00110                 nop();
00111         } while (1);
00112 }

Here is the call graph for this function:

@DOC_TITLE@
Generated on Wed Apr 23 08:25:24 2008 for AVR1314 Using the Xmega Real Time Counter by doxygen 1.5.5