Xmega Application Note


clksys_example.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00052 #include "avr_compiler.h"
00053 #include "clksys_driver.h"
00054 
00055 /* The LED to use for visual feedback. */
00056 #define LEDPORT PORTD
00057 #define LEDMASK 0xFF
00058 
00059 /* Which switches to listen to */
00060 #define SWITCHPORT PORTC
00061 #define SWITCHMASK 0xFF
00062 
00063 
00064 /* Prototype function. */
00065 void WaitForSwitches( void );
00066 
00067 
00076 int main( void )
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 }
00176 
00179 void WaitForSwitches( void )
00180 {
00181         do {} while ( ( SWITCHPORT.IN & SWITCHMASK ) == SWITCHMASK );
00182         delay_us( 1000 );
00183         do {} while ( ( SWITCHPORT.IN & SWITCHMASK ) != SWITCHMASK );
00184         delay_us( 1000 );
00185 }
00186 
00187 
00189 ISR(TCC0_OVF_vect)
00190 {
00191         LEDPORT.OUTTGL = LEDMASK;
00192 }
@DOC_TITLE@
Generated on Mon Sep 14 09:48:09 2009 for AVR1003 Using the XMEGA Clock System by doxygen 1.5.8