Buck Converter Code Architecture

The code architecture is organized into three areas, Initialization, State Machine, and Analog-to-Digital Converter (ADC) Interrupt Routine. In the next few sections, we will go into the details of these areas.


A number of functions are called to initialize the main resources and peripherals of the chip. Specifically:

  • CPU is initialized
  • I/O ports are initialized
  • Timer used to generate the local tick on which many functions are synchronized is initialized
  • Pulse Width Modulation (PWM) 1 peripheral is initialized
  • ADC pair 0 is initialized
  • Variables needed by the state machine for the buck converter 1 are initialized
  • PWM 2 peripheral is initialized
  • ADC pair 1 is initialized
  • Variables needed by the state machine for the buck converter 2 are initialized

State Machine

In the state machine section, the main loop is an endless loop that continuously calls:

  • The function that tests if any button on the board has been pressed
  • The function that manages the state machine of buck 1
  • The function that manages the state machine of buck 2

ADC Interrupt

All the tasks performed by the routine are shown below. The routine essentially reads the new voltage value and computes the new duty cycle value.


The following slide show demonstrates the details of the ADC interrupt operations. Click on each element for the details of the operation.

Context Save

The W registers used in the local code are pushed into the stack to preserve their value at the output of the Interrupt Service Routine (ISR). Please be careful in using the instruction push.s, as there is only one level of shadow registers that can be used to momentarily store the registers. If a second interrupt uses push.s, then the first instance will be lost.

Init Pointers

Register W8 is used to point to the Proportional-Integral-Derivative (PID) coefficients buffer. W10 is used to point to the input error values buffer. The instructions initialize the two pointers to the first location of the two buffers.

Read New Voltage Value

This routine is executed when the ADC has completed the conversion of the input channel. ADCBUF1 contains the new converted value (output voltage).

Voltage Scaling

The output voltage is multiplied by the reverse of the resistor divider to increase the dynamic range.

Voltage Error Computation

The error is defined as the reference voltage less than the output voltage (from the ADC).

PID Computation

This is the core of the operation of the ISR. The PID output is computed. The result (new active period of the PWM signal) is stored in W0.

Update Voltage Error Buffer

The new error is stored in the first entry of the error buffer.

Check Duty Limits

The newly computed active period must be within meaningful limits. This piece of code tests if they are indeed within the limits, otherwise the MIN or MAX value is used. This piece of code may be considered the digital counterpart of anti-wind-up.

Update Duty Cycle

The new value is stored in the duty cycle register so that it will be used at the next PWM period.

Context Restore

Before leaving the ISR, the original values of the used W registers are popped from the stack and restored.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.