The Numerically Controlled Oscillator (NCOx) module is a timer that uses the overflow of an accumulator to create an output signal. The accumulator overflow is controlled by an adjustable increment value rather than just a single clock pulse or postscaler increment. This offers an advantage over a simple timer driven counter in that the resolution of division does not vary with the somewhat limited prescaler/postscaler divider value. The NCOx is most useful for applications that require frequency accuracy and fine resolution at a fixed duty cycle.
Features of the NCOx include:
• 16-bit increment function
• Fixed Duty Cycle (FDC) mode
• Pulse Frequency (PF) mode
• Output pulse width control
• Multiple clock input sources
• Output polarity control
• Interrupt capability
The NCOx operates by repeatedly adding a fixed value to an accumulator. Additions occur at the input clock rate. The accumulator will overflow with a carry periodically, which is the raw NCOx output. This effectively reduces the input clock by the ratio of the addition value to the maximum accumulator value.
The NCOx output can be further modified by stretching the pulse or toggling a flip-flop. The modified NCOx output is then distributed internally to other peripherals and optionally output to an I/O pin. The accumulator overflow can also generate an interrupt. The NCOx period changes in discrete steps to create an average frequency. This output depends on the ability of the receiving circuit (i.e., CWG or external resonant converter circuitry) to average the NCOx output to reduce uncertainty.
The overflow of the NCO module is based on the formula below:
NCO Video Tutorial
This video will introduce the Numerically Controlled Oscillator (NCO) for Microchip 8-bit MCU devices and show how to use it.
The NCO module can output a signal in one of two modes.
The NCOCON register controls the mode setting for the NCO.
Fixed Duty Cycle
The Fixed Duty Cycle mode toggles the output on every accumulator overflow. As long as the adder value and clock don't change this will result in a 50% duty cycle output.
Pulse Frequency Modulation
The Pulse Frequency Modulation mode will trigger a pulse on every accumulator overflow for a period set by three bits in the NCOCON register.
The accumulator is a 20-bit register with a maximum value of 1,048,575. Read and write access to the Accumulator is available through three registers:
When the Accumulator overflows, the output of the NCO module will change state.
The NCOx Adder is a full adder, which operates independently from the system clock. It adds the value of the increment value to the accumulator on each NCO clock pulse. The adder takes the value in the accumulator and then adds the increment value. The result is placed back in the accumulator. The accumulator value will rollover and any value beyond the 1,048,575 will get placed as the starting value in the accumulator.
This can be reset if desired by writing a zero to the accumulator. This is typically done within the interrupt service routine if that is enabled on the NCO module.
The Increment value is stored in two 8-bit registers making up a 16-bit increment value. The lower 8-bits are in the NCOxINCL register and the upper 8-bits are in the NCOxINCH register.
Both of the registers are readable and writeable. The Increment registers are double-buffered to allow for value changes to be made without first disabling the NCOx module. The buffer loads are immediate when the module is disabled. Writing to the NCOxINCH register first is necessary because then the buffer is loaded synchronously with the NCOx operation after the write is executed on the NCOxINCL register.
Clock sources available to the NCOx include:
• CLKIN pin
The NCOx clock source is selected by configuring the NxCKS<2:0> bits in the NCOxCLK register.
The HFINTOSC selection will continue to run even if the device is put into sleep mode.
The output of the NCO has several options that can be set in the NCOCON register.
The output can be enabled or disabled (NxOE bit) and also inverted (NxPOL bit) by setting or clearing bits in the NCOCON register.
The output can also be monitored in software by reading the state of the NxOUT bit in the NCOCON register.
The NxEN bit can disable the whole NCO module. A one setting enables the module and a zero setting disables it.
The NCO output can trigger an internal interrupt when the accumulator overflows. This is handled by three bits (GIE, PEIE, NCOxIE) in the set of registers shown below. This will allow the NCO to control software actions through the interrupt service routine in the application code while also outputting a signal to an I/O pin.
The GIE is the Global Interrupt Enable bit.
The PEIE is the Peripheral Interrupt Enable bit.
The NCOxIE is the NCO Interrupt Enable bit. There can be multiple NCO modules so the "x" represents the NCO number.
The NCOxIF bit is the interrupt indicator flag. This can be monitored in software to see if an interrupt has occurred. This needs to be cleared in the interrupt service routine or in the software routine that read the bit.