Tuesday, April 2, 2013

The Microcontroller Oscillator

This lesson is dedicated to learning about the Microcontroller Oscillator.  I'm going to keep this lesson short in an effort to provide the basic information we need for use on the blog.  Many books dedicated to the microcontroller will elaborate further on the Oscillator, clock, and program flow.

Every microcontroller requires a clock signal, a continuously running fixed-frequency logic square wave, to execute instructions.  This clock signal is generated by an oscillator.  The change between a high and a low is called an Edge (figure 1).
Figure 1

Many essential timing functions in addition to the CPU depend on the clock.  The oscillator frequency is measured in Hertz, or pulses per second.  PIC microcontroller oscillators operate in the tens of thousands to tens of millions of hertz, usually between 32KHz and up to 80MHz for some of the more advanced 32-bit devices.  The oscillator pulses enter the microcontroller, where internal circuitry divides the clock into four even quadrature clocks.  The four clock pulses of the internal circuitry are referred to as Q1, Q2, Q3, and Q4, and all four of them together make up one instruction cycle during which one instruction is executed.

Remember that the instruction set of the PIC microcontrollers are bit-oriented instructions in the Assembly language, not C.  Each instruction requires one instruction cycle to execute with the exception of instructions that branch from normal program flow such as CALL or GOTO.

Each instruction must be fetched from memory, decoded by the processor, executed via some operation, and any data for the current instruction written.  Broadly speaking, Q1 fetches the next instruction, Q2 reads the data for the current instruction, Q3 executes the current instruction, and Q4 writes any data.  All four quadrature clocks together make up one instruction cycle.  A fetch requires one instruction cycle while an execute requires a separate instruction cycle.  Under some microcontroller architectures, this effectively means that each instruction requires two full instruction cycles to process.  PIC microcontrollers however, have separate buses for program memory and data memory, so they implement instruction pipelining where one instruction is being executed while the next instruction is simultaneously being fetched.  The clock/instruction cycle and instruction pipelining are shown in figure 2.
Figure 2
As shown in the upper portion, each instruction cycle is made up of four clock pulses from the oscillator.  The MCU handles a task on each quadrature clock, and the program counter is incremented after one full instruction cycle.

The instruction pipeline flow shows how pipelining works.  When the MCU is powered on, the first instruction cycle fetches the first instruction.  During the second instruction cycle, the first instruction is executed while the second instruction is being fetched, then this instruction is executed while the third instruction is fetched and so on.  When we use an instruction that branches program flow, the CPU forces the address of the instruction onto the stack and then changes the program counter to the address of the branch.  This means that the instruction that was fetched before this change is no longer needed and is flushed from the pipeline, causing an extra instruction cycle to be needed.  As can be seen in figure 2, while instruction 2 is being executed, instruction 3 is being fetched.  Instruction 3 is a CALL, which branches program flow.  Instruction 3 is then executed while instruction 4 is fetched, but instruction 3 changed the program counter to point to the address of SUB_1 so instruction 4 is no longer needed.  Therefore, during the next instruction cycle, instruction 4 is flushed while the instruction at SUB_1 is being fetched.  This pipelining effectively means that each instruction requires only one instruction cycle to complete.

Timing Operations

As stated earlier, timing operations carried out by the MCU operate off the oscillator clock signal.  The oscillator is divided by the MCU into four separate pulses, the quadrature clocks.  Because the MCU operates in such a way that four pulses of the clock oscillator equal one instruction cycle, the clock speed is effectively reduced by a division factor of four (Fosc/4).  Therefore, if we use a 4MHz oscillator, the MCU's instruction frequency is 1MHz, or one million instructions per second.  In other words, one instruction is executed every 0.000001 seconds (1uS).

Using this knowledge, we can now determine how long a particular sequence will take to execute by realizing the instruction frequency time, and counting how many instructions are to be executed (being sure to account for any branching that would require two cycles).  This can be useful in calculating timing delays or other timing functions.  We'll see some examples of timing operations in later lessons.

Oscillator Clock Options

An oscillator clock signal is typically generated by two types of oscillators, a Resistor-Capacitor (RC) oscillator, or a Crystal or Ceramic oscillator.  Some microcontrollers have an internal RC oscillator that can be used to generate the clock signal.  Higher speed clocks require greater power consumption by the MCU while lower speed clocks require less power.  The clock source is selected by the user as part of the configuration word.  Examples of a RC clock and Crystal/Resonator clock are shown in figure 3.
Figure 3
XT - Crystal.  This is the standard crystal configuration.  It is intended for crystals or resonators in the range of 1 to 4MHz.
HS - High Speed.  This is a higher drive version of the XT configuration.  It is intended for crystal frequencies of about 4MHz and greater, and/or ceramic resonators.  It leads to the highest current consumption of all the oscillator modes.
LP - Low Power.  This mode is intended for low-frequency crystal applications and gives the lowest power consumption possible.  It is suitable for crystals of 200kHz or below.
RC - Resistor-Capacitor.  The lowest-cost method of providing an oscillator signal is the resistor-capacitor oscillator.  An RC oscillator is also the least accurate, since the nominal frequency of oscillation can be predicted with only limited accuracy and will even then drift with a change in temperature, supply voltage, and time.  An RC oscillator is a good option when timing operations aren't critical.  Some PIC chips come equipped with an internal RC oscillator eliminating the need for the external circuitry.

13 comments:

  1. thanks for this - it was very useful and you state things that I haven't found on the PIC datasheet.

    ReplyDelete
  2. Thanks for the information.

    I have a PIC16C711 and programmed it. My PIC programmer also has options to set the oscillator modes. WHich I did afterwards but when I do a read Config then it shows me the LP radio button is selected instead of the high speed HS oscillator mode. I tried reprogramming the OSC modes but it always reverts back to the LP selection.

    Q. When programming does one first set and program the oscillator and then program in the rest or program the 1K (in this cae) and then do the oscillator mode selection, The 16C711 seems to be one-time programmable only and I do not want to waste another. They are pricey for me.

    ReplyDelete
  3. Response to Brokenit from May 28, 2014. Regarding your specific device, the "C" designator in 16C711 indicates that chip is a One Time Programmable (OTP) device, meaning that once it is programmed, it cannot be erased or rewritten to.
    Regarding the programming of the configuration word, I have never used the device programmer (in my case the PicKit 2) to program the configuration word. I always leave that task up to the compiler by including the configuration word settings in the C program, as shown in the projects on this blog. This should completely alleviate the need to do anything further with the hardware in order to set the configuration word.
    In other words, the configuration settings should be made by either the software, or the hardware, but not both. I find it is better and easier to complete this task using the software. If you should have further problems with this subject, I recommend seeking help from the community at "forum.allaboutcircuits.com".

    ReplyDelete
  4. There are times when people are having lots of trouble while they are planning to aol desktop gold software in their computer. If you are also one of them, you need first to see whether your system is fulfilling all the requirement which is required for downloading the software.

    ReplyDelete



  5. Just follow these steps given in this article for Belkin Range Extender Setup . N300 Dual-Band

    extender improves Wi-Fi connection speed and signal stability. Check all the steps right now or contact the Belkin Team to do it for you.


    ReplyDelete