Xmega Application Note


clksys_example.c File Reference

XMEGA Clock System driver example source. More...

#include "avr_compiler.h"
#include "clksys_driver.h"

Include dependency graph for clksys_example.c:

Go to the source code of this file.

Defines

#define LEDMASK   0xFF
#define LEDPORT   PORTD
#define SWITCHMASK   0xFF
#define SWITCHPORT   PORTC

Functions

 ISR (TCC0_OVF_vect)
int main (void)
 Example code that switch between different clocks.
void WaitForSwitches (void)
 This function waits for a button push and release before proceeding.


Detailed Description

XMEGA Clock System driver example source.

This file contains an example application that demonstrates the Clock System driver. The recommended setup for this demonstration is to connect an external 2-9MHz crystal to the XTAL1/XTAL2 pins with proper decoupling.

Application note:
AVR1003: Using the XMEGA Clock System
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 clksys_example.c.


Define Documentation

#define LEDMASK   0xFF

Definition at line 57 of file clksys_example.c.

Referenced by ISR(), and main().

#define LEDPORT   PORTD

Definition at line 56 of file clksys_example.c.

Referenced by ISR(), and main().

#define SWITCHMASK   0xFF

Definition at line 61 of file clksys_example.c.

Referenced by main(), and WaitForSwitches().

#define SWITCHPORT   PORTC

Definition at line 60 of file clksys_example.c.

Referenced by main(), and WaitForSwitches().


Function Documentation

ISR ( TCC0_OVF_vect   ) 

Just toggle LED(s) when interrupt occurs.

Definition at line 189 of file clksys_example.c.

References LEDMASK, and LEDPORT.

00190 {
00191         LEDPORT.OUTTGL = LEDMASK;
00192 }

int main ( void   ) 

Example code that switch between different clocks.

This example code shows how to change between 5 different system clocks and how to prescale and divide the clocks to make use of dynamic clocking for the different parts on the device. When changes are made to the clocks, blocking functions are used to make sure the clocks are stable before they can be used.

Definition at line 76 of file clksys_example.c.

References CLKSYS_Disable(), CLKSYS_Enable, CLKSYS_IsReady, CLKSYS_Main_ClockSource_Select(), CLKSYS_PLL_Config(), CLKSYS_Prescalers_Config(), CLKSYS_XOSC_Config(), LEDMASK, LEDPORT, SWITCHMASK, SWITCHPORT, and WaitForSwitches().

00077 {
00078         /* Set up user interface. */
00079         LEDPORT.DIRSET = LEDMASK;
00080         LEDPORT.OUTSET = LEDMASK;
00081         SWITCHPORT.DIRCLR = SWITCHMASK;
00082 
00083         /* Set up Timer/Counter 0 to work from CPUCLK/64, with period 10000 and
00084          * enable overflow interrupt.
00085          */
00086         TCC0.PER = 10000;
00087         TCC0.CTRLA = ( TCC0.CTRLA & ~TC0_CLKSEL_gm ) | TC_CLKSEL_DIV64_gc;
00088         TCC0.INTCTRLA = ( TCC0.INTCTRLA & ~TC0_OVFINTLVL_gm ) |
00089                         TC_OVFINTLVL_MED_gc;
00090 
00091         /* Enable low interrupt level in PMIC and enable global interrupts. */
00092         PMIC.CTRL |= PMIC_MEDLVLEN_bm;
00093         sei();
00094 
00095 
00096         /*  This while loop run through and switch between the different
00097          *  system clock sources available.
00098          */
00099         while(1) {
00100 
00101                 /* Wait for user input while the LEDs toggle. */
00102                 WaitForSwitches();
00103 
00104 
00105                 /*  Enable internal 32 MHz ring oscillator and wait until it's
00106                  *  stable. Divide clock by two with the prescaler C and set the
00107                  *  32 MHz ring oscillator as the main clock source. Wait for
00108                  *  user input while the LEDs toggle.
00109                  */
00110                 CLKSYS_Enable( OSC_RC32MEN_bm );
00111                 CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_1_2_gc );
00112                 do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 );
00113                 CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32M_gc );
00114                 WaitForSwitches( );
00115 
00116 
00117                 /* Enable for external 2-9 MHz crystal with quick startup time
00118                  * (256CLK). Check if it's stable and set the external
00119                  * oscillator as the main clock source. Wait for user input
00120                  * while the LEDs toggle.
00121                  */
00122                 CLKSYS_XOSC_Config( OSC_FRQRANGE_2TO9_gc,
00123                                     false,
00124                                     OSC_XOSCSEL_EXTCLK_gc );
00125                 CLKSYS_Enable( OSC_XOSCEN_bm );
00126                 do {} while ( CLKSYS_IsReady( OSC_XOSCRDY_bm ) == 0 );
00127                 CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_XOSC_gc );
00128                 CLKSYS_Disable( OSC_RC32MEN_bm );
00129                 WaitForSwitches();
00130 
00131 
00132                 /*  Divide Prescaler C by two and Prescaler C by two, and wait
00133                  *  for user input.
00134                  */
00135                 CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_2_2_gc );
00136                 WaitForSwitches();
00137 
00138 
00139                 /*  Enable internal 32 kHz calibrated oscillator and check for
00140                  *  it to be stable and set prescaler A, B and C to none. Set
00141                  *  the 32 kHz oscillator as the main clock source. Wait for
00142                  *  user input while the LEDs toggle.
00143                  */
00144                 CLKSYS_Enable( OSC_RC32KEN_bm );
00145                 CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc );
00146                 do {} while ( CLKSYS_IsReady( OSC_RC32KRDY_bm ) == 0 );
00147                 CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32K_gc );
00148                 CLKSYS_Disable( OSC_XOSCEN_bm );
00149                 WaitForSwitches();
00150 
00151 
00152                 /*  Configure PLL with the 2 MHz RC oscillator as source and
00153                  *  multiply by 30 to get 60 MHz PLL clock and enable it. Wait
00154                  *  for it to be stable and set prescaler C to divide by two
00155                  *  to set the CPU clock to 30 MHz. Disable unused clock and
00156                  *  wait for user input.
00157                  */
00158                 CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 30 );
00159                 CLKSYS_Enable( OSC_PLLEN_bm );
00160                 CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_1_2_gc );
00161                 do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 );
00162                 CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc );
00163                 CLKSYS_Disable( OSC_XOSCEN_bm );
00164                 WaitForSwitches( );
00165 
00166 
00167                 /*  Select 2 MHz RC oscillator as main clock source and diable
00168                  *  unused clock.
00169                  */
00170                 do {} while ( CLKSYS_IsReady( OSC_RC2MRDY_bm ) == 0 );
00171                 CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC2M_gc );
00172                 CLKSYS_Disable( OSC_PLLEN_bm );
00173 
00174         }
00175 }

Here is the call graph for this function:

void WaitForSwitches ( void   ) 

This function waits for a button push and release before proceeding.

Definition at line 179 of file clksys_example.c.

References SWITCHMASK, and SWITCHPORT.

Referenced by main().

00180 {
00181         do {} while ( ( SWITCHPORT.IN & SWITCHMASK ) == SWITCHMASK );
00182         delay_us( 1000 );
00183         do {} while ( ( SWITCHPORT.IN & SWITCHMASK ) != SWITCHMASK );
00184         delay_us( 1000 );
00185 }

@DOC_TITLE@
Generated on Mon Sep 14 09:48:10 2009 for AVR1003 Using the XMEGA Clock System by doxygen 1.5.8