System - Clock

  1. Microcontroller's clock system

    Organization: A microcontroller can have different clock sources, such as an external clock or a crystal oscillator, and serveral components, such as the processor and peripheral devices, of a microcontroller would require clocks. Clock system architecture is Clock multiplexer (select clock source) -> prescaled (software, output system clock clk-sys) ->AVR Clock control unit -> different sub clock source for those components. The following picture is a clock system in the Atmega328p chip.[1]

    * The clki/o and clkcpu have the same clock speed.

    Why independent ADC clock domain? It allows the ADC to work while halting the clocks for CPU and I/O clocks. Halting other clocks reduces the noise and improve ADC accuracy.

    Terms

    • start-up time: a time period before releasing the reset
    • time-out delay: a ...
    • BOD:
    • clock domain: an independent clock source supplied for a component in the microcontroller, such as clk-io and clk-cpu.
  2. Clock domains in Atmega328/p

    The Atmega328p offers 5 clock domains to CPU, I/O, flash, asynchronous timer and ADC, respectively. Independent clock sources allow the microcontroller to halt unnecessary components for saving power.

    Clock domains

    1. clk-cpu: clock AVR core, such as CPU, general purpose register file, PC, SP, the status register, and RAM. Halting the clk-cpu stops performing calculations and other general operations.
    2. clk-flash: clock flash and EEPROM.
    3. clk-io: clock I/O modules, such as SPI, USART, and Timer/Counters.
    4. clk-asy: clock timer/counters. The timer/counters can select clk-io or clk-asy. If the clk-asy is selected, it would be clocked by a real external clock source, different from the system's choices. Then the timer/counter is a real-time counter even the device is in sleep mode.
    5. clk-adc: clock the ADC peripheral device.
  3. System Clock prescale

    The system clock can be divided by configuring the Clock Prescale Register (CLKPR). It allows descrease the system clock frequency and save power. It doesn't affect the clk-asy, which is only determined by the Timer/Counter oscillator clock source.

    The system clock is being changed when the system is running. So the prescaler has to guarantee no glitches occurs. To safely switch clk-sys, a special write procedure shown below:

    1. write CLKPR.CLKPCE = 1 (Clock Prescaler Register's enable bit) and all other bits to 0. CLKPR = 0x80
    2. within 4 cycles, write desired value to CLKPS[3:0] while writing a zero to CLKPCE = 0. CLKPR = 0x0N

    Code

    cli(); // disable interrupt because the following instructions need to be done within 4 clock cycles.
    CLKPR = 0x80;
    CLKPR = 0x08; // set a 256 prescale
    sei(); // may enable interrupt again
    

    * Interrupts must be disabled by software during the modification.

    Clock Prescaler Register

    No.NameMeaningDefault value
    7CLKPCEEnable changing frequency, frequency need to be changed within 4 cycles after set to 10
    3CLKPS30
    2CLKPS20
    1CLKPS10/1
    0CLKPS0CLKPS[3:0] set the prescale of clock source. If CKDIV8 fuse bit is programmed, its initial value is 0011, otherwise 00000/1

    0000 -> 1, 0001 -> 2, 0010 -> 4, 0011 -> 8, ... 1000 -> 256, others are reserved.

  4. Clock sources in Atmega328/p

    Atmega328p can select 6 clock sources

    1. calibrated internal RC OSC [default] 8MHz
    2. external crystal oscillator [frequently used e.g. Arduino Nano 16MHz] Max. 20MHz
    3. external clock
    4. Real timer/counter oscillator
    5. Low-frequency crystal oscillator

    System startup: A successful microcontroller startup needs a stable voltage and a stable clock source. After just powering the system, the Vcc pin's voltage may not be stable thinks to capacitance and other factors. The system would delay a short period in order to give time (time-out delay) to the Vcc pin to reach the valid voltage. Also the clock source may also not reach the stable status. It needs to run a few cycles (start-up time) before claiming it is stable. Different power types and clock sources have distinctive time-out delay and start-up time. They are configured by the Low fuse btyes ([Divided by 8, ck out, SUT (start-up time), CKSELn (clock source selection)]).

    Defined power type

    1. BOD enabled: BOD circuit holds the reset until Vcc reaches the minimum required voltage for a time-out delay time.
    2. Fast rising power
    3. Slowly rising power

    Clock sources

    1. The calibrated internal 8MHz RC oscillator is the default clock source (CKSELn = 0010). Because the CKDIV8 is programmed by default, the clk-sys is 1 MHz.
    2. The low power crystal oscillator can be used with a quartz crystal or ceramic resonate (CSKELn=1111-1000). It is susceptible to noise in noisy environment but save power compared to the external oscillator. The frequency can range from 0.4MHz to 20MHz, CKSEL are distinctive for different range.

      The circuit used by low power crystal oscillator is shown below [2], the capacitance ofc2 and c2 must be same.

  5. Clock sources selection experiments

    Arduino uses a low power crystal oscillator that runs at 16MHz with CKSEL=1111, SUT=11 (16K cycle start-up + 14 cycle + 65ms time-out delay). Design a program that blink a led for one second. Use the same code but setting the fuse bits to use internal RC 8MHz.

  6. References

    [1][2] Atmega328p datasheet. Page 48 and Page 51.