Using the Crystal Clock as System Clock

This example shows how to configure the 32.768 kHz crystal to be the system clock and how the system clock can be set as an output to the CLKOUT pin. Refer to Get Source Code from Atmel | START for a link to the project.

As the 32.768 kHz clock will be clocking the CPU, it is recommended to:

  1. 1.Enable the crystal with Run Standby as one of the first actions performed in main().
  2. 2.If there are any, run other production tests and calibrations of the circuit.
  3. 3.Switch the main clock to the crystal.
  4. 4.Configure the system clock as output on the CLKOUT pin.

This procedure will allow time for the crystal to settle while other production tests are being performed.

In the below example code the crystal is started by the first protected write. Note that the Run Standby bit is written at the same time. This is to ensure that the crystal starts even when no peripheral or the CPU is requesting the clock. The main clock is switched before checking that the crystal is stable as the XOSC32KS bit will not be set unless something is requesting the clock.

    //Deactivate change protection for register Start XOSC32K here
    //start-up time is set to the maximum 64K cycles (~2 seconds)
    _PROTECTED_WRITE(CLKCTRL_XOSC32KCTRLA , CLKCTRL_ENABLE_bm |\
        CLKCTRL_CSUT_64K_gc | CLKCTRL_RUNSTDBY_bm);

    //Deactivate change protection for register, Switch main clock
    //to the 32K oscillator and output the clock to the pin
    _PROTECTED_WRITE(CLKCTRL_MCLKCTRLA , CLKCTRL_MCLKCTRLA =\
        (CLKCTRL_CKSEL1_bm) | CLKCTRL_CLKOUT_bm);

    //Wait for XOSC32K to be reported as stable
    while(!(CLKCTRL_MCLKSTATUS & CLKCTRL_XOSC32KS_bm));

    //Deactivate change protection for register, Make certain the prescaler is disabled.
    _PROTECTED_WRITE(CLKCTRL_MCLKCTRLB, 0);

In the case of outputting the 32.768 kHz clock, it is important to consider the capacitive load on the output pin. A high load can make it difficult to get a good signal out on the test pin. Also, the clock signal must not adversely impact other components connected to the pin at the time of testing. Production testing should be planned early in the design process to make sure that a pin is available to handle this signal.