GH

Chapter 5 - MARV's Heartbeat: Oscillators and Timers

Oscillators

  • Every digital device requires a clock source to synchronize operations.
  • This chapter explores clock sources for PIC18 devices and the functioning of timers/counters.
  • An oscillator consists of an inverting amplifier and a filter (tuned circuit or crystal) in a positive feedback loop.
  • Barkhausen criteria for sustained oscillations:
    • Loop gain must exceed unity at the resonant frequency.
    • Phase shift around the loop must be zero or an integer multiple of 2π radians.
  • The inverting amplifier provides voltage gain and a 180-degree phase shift.
  • The tuned circuit or crystal provides another 180-degree phase shift and determines the oscillator's frequency.

Clock Sources

  • The PIC18F45K22 can use six primary clock sources:
    • Low-frequency (e.g., 31 kHz internal RC oscillator)
    • High-frequency (e.g., up to 64 MHz crystal oscillators)
  • Choose the lowest oscillator speed that meets system requirements to minimize power consumption.
  • FOSC
  • A secondary internal clock source (32.768 kHz) is shared with Timers 1, 3, and 5.
  • System clock select bits (SCS

External RC Oscillator (RC)

  • A resistor-capacitor combination provides a low-cost external oscillator.
  • Microchip Developer Help documentation explains the design process.

External Low-Power Crystal Oscillator (LP)

  • Uses a low-power crystal driven by the internal inverting amplifier at its lowest gain setting.
  • Designed for a low-frequency 32.768 kHz tuning-fork-type crystal.
  • Dividing the frequency by 2^{15} = 32768 yields a 1 Hz signal, commonly used in time-keeping applications (wristwatch crystals).
  • Offers the lowest current consumption among the external crystal oscillator modes.

External Crystal or Resonator Oscillator (XP)

  • Accommodates crystals or resonators with a midrange frequency (0.5 MHz to 4 MHz).
  • The internal inverting amplifier is set at an intermediate gain.
  • Operates at medium power consumption.

External High-Speed Crystal or Resonator (HS)

  • The PIC18F45K22 can operate from a clock source up to 64 MHz.
  • For frequencies above 4 MHz, select the external high-speed oscillator mode.
  • The internal inverting amplifier gain must be set at medium for frequencies between 4 and 16 MHz and at high for frequencies above 16 MHz.
  • Power consumption is high in this mode.

External Clock (EC)

  • Uses a clock source generated by another device, connected to the device's OSC1 pin.
  • Has three power modes:
    • Low power: Clock signals below 0.5 MHz
    • Medium power: Clock signals between 0.5 and 16 MHz
    • High power: Clock signals exceeding 16 MHz

Internal RC Oscillator (INTOSC)

  • The internal RC oscillator module has three independent oscillators:
    • High-frequency oscillator (HFINTOSC): Operates up to 16 MHz
    • Medium-frequency oscillator (MFINTOSC): Operates up to 500 kHz
    • Low-frequency oscillator: Operates at 31.25 kHz
  • Internal oscillator frequency select bits IRCF
  • MFINTOSC and HFINTOSC can be tuned via the OSCTUNE register.
  • The internal oscillator can be output on port pins to drive external digital devices; this is controlled by configuration bits.

Oscillator Start-Up Delay

  • Crystal oscillators take time to stabilize.
  • Executing firmware before stabilization can cause timing errors.
  • PIC18s have an oscillator start-up timer (OST) that, if enabled, counts 1024 oscillations after a power-on reset and keeps the device reset during this period.
  • RC oscillators are stable when switched on.
  • The PIC can be configured to use the internal oscillator until an external crystal or resonator oscillator stabilizes.
  • Two-speed start-up advantages:
    • Faster start-up
    • Reduced power consumption, as the high-speed oscillator might not need to start for small events.
  • Clock-switching can be implemented in firmware to save power by using different clock sources for different tasks.

Timers

  • A timer on a microcontroller is a register or register pair that counts on a regular clock.
  • If the period of a count is known, the duration for a specific number of counts is also known - this can be used to time events.

Introduction to Timers

  • Timers are commonly used to implement delays.
  • PIC18s have 8-bit and 16-bit timers.
    • 8-bit timer: Counts up to 256 (0 to 255).
    • 16-bit timer: Counts up to 65,536. Implemented as two 8-bit registers (high and low byte).
  • Timer overflow: When the timer restarts from zero after reaching its maximum value; this generates an overflow pulse.
  • Timers derive their time base from the instruction clock. If no pre- or postscalers are used, they increment at FOSC/4.
  • If FOSC is 4 MHz, the instruction cycle TCY is 1 µs.
  • An 8-bit timer will take 256 µs to count from zero to zero again.
  • A 16-bit timer will take around 65.5 ms to count from zero to zero again at this clock frequency.
  • If the timing requirement is more than the timer period, a number of timer periods may be implemented back-to-back.

Pre- and Postscalers

  • The timer period can be expanded through pre- and postscalers.

  • The prescaler is a divider that divides the instruction clock. If the prescaler is set at 1:4, its increment period would be four times TCY.

  • The postscaler is another adjustable counter that counts the number of timer overflow pulses.
    Figure 5.2 Pre and post scalers for Timer 2 as shown in the datasheet of the PIC18F45K22.

  • The timer interrupt flag TMRxIF will only be set once the number of timer overflow pulses that was set in the postscaler has been counted.

  • Example:

    • Use an 8-bit timer to implement a 1 s delay for FOSC = 4 MHz.
    • A prescale value of 1:4 will result in a timer period of around 1 ms (4 x 256 µs).
    • 1000 timer periods would be required to create the 1 s delay.
    • Without pre- or postscalers, approximately 4000 timer periods would be required to create 1 s.

Period Registers

  • Some timers have a period register that can be programmed with a specific value.
  • A comparator compares the value of the timer with the value in the period register after each count.
  • When the values match, the timer is reset (restarts from 0).
  • A timer period when using a period register value of 249 will be 4 * 250 µs * 4 * 250 interrupts = 1.000 s.
  • The period register always has the same number of bits as the timer.

Timers as Counters

  • Timers can be incremented from a regular clock source or by an external trigger.
  • If an external irregular trigger is used to increment the timer, it counts events rather than acting as a timer.
  • Timers are frequently referred to as timers/counters.

Timers on the PIC18F45K22

  • The PIC18F45K22 has three 8-bit timers and four 16-bit timers.
  • Timer 0:
    • Can be configured as an 8- or 16-bit timer or counter.
    • To use it as a counter, the trigger must be connected to the T0CKI pin.
    • Has an 8-bit prescaler with values in powers of 2 from 1:1 to 1:256.
  • Timers 1, 3, and 5:
    • 16-bit timers/counters (TMRxH:TMRxL).
    • The edge of the clock input on which the increment should occur can be selected.
    • Have several gate sources.
    • Have a 2-bit prescaler to allow 1:1 to 1:8 instruction clock divisions.
    • Do not have a postscaler.
    • Timer 1 can also be driven from a secondary low-power 32.768 kHz oscillator for time-keeping applications.
    • The Capture/Compare/Pulse Width Modulation (CCP) peripheral uses Timers 1, 3 and 5 for its time-base.
      *Timers and counters register pair in the CCP module thus acts as a 16-bit period register for Timers 1, 3 and 5.
  • Timers 2, 4, and 6:
    • 8-bit timers with pre- and postscalers and a built-in period register.
    • Handy for implementing precise timing applications (e.g., delays).

NIBBLE on Timing and Timers

  • This Chapter's NIBBLE assignments cover timers, counters and oscillators.

The basics you should gain from this NIBBLE

Oscillators

  • An understanding of
    • the various primary clock modes of the PIC18F45K22.
    • the difference between an internal and an external clock source, and the ways in which these sources can be implemented for the PIC18F45K22
    • how to set up the internal oscillator frequency

Timers and counters

  • An understanding of
    • the nature and operation of µC timers.
    • the difference between 8-bit and 16-bit timers.
    • the clock source employed by the timers.
    • the concept and operation of pre- and postscalers.
    • the concept and operation of a period register.
    • how timers can be used to count events, either synchronously or asynchronously.

Code Example: Timer 2 Delay

  • Flashes an LED on RD0 three times and then hangs in an infinite loop.
  • Implements a 1-second delay using Timer 2 with a period register of 199, FOSC = 500 kHz, a prescaler of 1:4, and a postscaler of 1:1.
  • The timer period is 4 * (199+1) * TCY * 1 = 4 * 200 * 8 µs * 1 = 6.4 ms.
  • To implement a delay of 1 second, 1000 ms / 6.4 ms = 156.25 timer interrupts must be allowed to occur, and since only integer values of the timer interrupts can be counted, the value is rounded to the nearest integer to 156.
  • Relevant code sections:
    • Timer 2 control register is loaded with the value for the prescaler, postscaler, and TMR2ON bit.
    • The period register (PR2) is loaded with 199.
    • Interrupts are initialized and enabled (PEIE, GIE, TMR2IE bits are set).
    • The timer is cleared (TMR2 is cleared) and started (TMR2ON is set).
    • A wait loop is implemented until the required number of interrupts has occurred (DelayFlag is set).
    • In the ISR, the Count register is incremented, and the value is compared to 156. When Count reaches 156, DelayFlag is set.
    • Once the delay flag is set, the timer and interrupt are disabled, and the Count register is cleared.

Code Example: Simple Timer 2 Example

  • Illustrates that 10 counts are executed for a period register that is preloaded with a value of 9.
  • Shows the number of counts taken for the branches to and from the ISR.

Additional Resources

  • PICmicro MID-RANGE MCU FAMILY: Chapter 2 Oscillators
  • Intel AP-155 Oscillators for Microcontrollers
  • Microcontroller Oscillator Circuit Design Considerations
  • AN826 Crystal Oscillator Basics and Crystal Selection for rfPIC and PIC Devices
  • AN849 Basic PIC Oscillator Design
  • AN943 Practical PIC Oscillator Analysis and Design
  • AN949 Making Your Oscillator Work