Cornell University ECE4760
Time Measurement and Control

plus clock generation
PIC32MX250F128B

The larger goal is to use parallel hardware to make timing completely independent of software, including independent of interrupts.

Time measurement and control
There are eight hardware timers on the PIC32MX250. Each of them is an independent counter which is toggled by one of several clock sources and which may trigger interrupts or other hardware events such as DMA bursts, ADC conversions, or pulse trains. The timers are not equivalent. Some have specific hardware abilities that the others do not. See also the Hardware Manual. There are two other functions which are closely related to timers: Input Capture which uses the timers to measure precise intervals, and Output Compare which use a timer to produce precise output pulse trains such as PWM and variable frequency square waves. There are five input capture units and five output compare units, each of which may be linked to either timer 2 or timer 3. This page also talks a little about the available time-base oscillators the PIC32 can generate.

An example in lab 1 uses one timer to drive two output compare units to produce two pulse trains, and a second timer to act a source for input capture of the period of either pulse train.

Timers

Input Capture Units
Timer 2 or 3 can be used as a time source for time capture based on an external event, such as a rising or falling edge on an ICx pin.. This allows very accurate (1-cycle) timing of events independent of software. Our cpu has five input capture units. All if the IC event-inputs are on PPS, and therefore may be moved between 8 different external pins.

Output Compare Units
Timer 2 or 3 can be used as a time source for generating an external event, such as a rising or falling edge or PWM signal. This allows very accurate (1-cycle) generation of events independent of software. Our cpu has five output compare units. All if the OCx pulse-outputs are on PPS, and therefore may be moved between 8 different external pins. There is Hardware-based PWM Fault detection and automatic output disable (OCFA or OCFB inputs).

Oscillators
The PIC32 oscillator system has the following modules and features:
• Four external and internal oscillator options as clock sources. Primary osc, secondary osc, fast RC osc, low power RC osc.
• On-chip Phase-Locked Loop (PLL) with a user-selectable input divider and multiplier, as well as an output divider, to boost operating frequency on select internal and external oscillator sources
• On-chip user-selectable divisor postscaler on select oscillator sources

Generally in this class the oscillator will default to the fast RC, FRC, 8 MHz +/-0.9%, clock, as specified in the Protothreads config file.
In the file there are three pragma lines:
#pragma config FNOSC = FRCPLL, POSCMOD = OFF
#pragma config FPLLIDIV = DIV_2, FPLLMUL = MUL_20, FPLLODIV = DIV_2 //40 MHz
#pragma config FPBDIV = DIV_1 // PB 40 MHz

--The first line sets the Oscillator Selection Bits (FNOSC) to use the Fast RC Osc with PLL. The PLL allows the system to multiply the 8 MHz FRC to a higher rate. It also disables the primary oscillator by setting POSCMOD off.
--The second line divides the FRC by two because the PLL input must be 4 MHz. Then it sets the PLL to multiply by 20 and divide by two to get 40 MHz cpu clock. Range is 15≤FPLLMUL≤24 and 1≤FPLLODIV≤256. But check the config page for details.
--The third line sets the peripherial bus speed to be the same as the cpu speed. FPBDIV can be set to 1,2,4,8.

From the diagram below, you can see that there are many ways of setting up the oscillator system to minimize power, or maximize performance, or run USB. To run the USB PLL you must use the primary oscillator with an external 12 MHz resonator!


Copyright Cornell University August 29, 2018