Last Updated:

Implementing 5V to 12V PWM Boost Converter in the DSPIC33CH Curiosity Development Board

Andrew Mosqueda
Andrew Mosqueda

Introduction

 The dsPIC33CH Curiosity Development Board (DM330028) has a built in buck-boost circuit that we can experiment with. Unfortunately in the time of this writing, the code for the buck-boost converter that is provided by Microchip is only written in assembly language which I found hard to comprehend. So I made my own program that is written in C language and using PID for the close loop control. Only voltage loop control is covered by this topic.

Buck-Boost Converter Circuit

Figure 1 below is the schematic of the buck boost circuit of the DSPIC33CH Curiosity Board. The DSPIC IC can control the boost transistor (Q2) switching through the RC15_S1PWM7L connection. The DSPIC IC should set low the RC14_S1PWM7H so that the Q6 P-MOSFET is always on (switch closed). 

DM330038 DSPIC33CH Curiosity Development Board Buck Boost Circuit
Figure 1 DM330028 DSPIC33CH Curiosity Development Board Buck-Boost Converter Corcuit

The output of the converter (Vout) is sensed by the DSPIC IC through RC1_VOUTFB connection as shown in Figure 2. For a 12V output, the voltage that is sensed on RC1_VOUTFB is 12V*1kΩ/(4.7kΩ+1kΩ) = 2.105V. Make sure that J13 is not shorted on the board. Shorting J13 will increase the gain and make the sense voltage clip to 3.3V. Shorting J13 is useful for buck operation. I'll just let the circuit containing R70, R57, R65, R66 and Q7 as load. In my computation, the load will have a resistance of 3.7kΩ or a power consumption of 39mW.

Figure 2 DM330028 DSPIC33CH Curiosity Development Board Buck-Boost Output Voltage Sense Circuit
Figure 2 DM330028 DSPIC33CH Curiosity Development Board Buck-Boost Output Voltage Sense Circuit

CORE SETUP

The PWM control pins in Figure 1 are connected to the Slave Core of the IC so we need to make the source code of the PWM control in the Slave Core project. We also need to make a simple source code for the Master Core project just to start the Slave Core.

  1. Run MPLAB X IDE (I'm using version v5.50 at the time of this writing). Connect J20 of the Curiosity board to the PC so that the MPLAB application detects what board is connected.
  2. After opening MPLAB, close any projects that are open on the left pane.

MASTER CORE

  1. Ctrl+Shift+N to open the new project dialog box. Select Standalone Project then Click Next.
    MPLAB New Project Choose Project dialog box
  2. Select Device dsPIC33CH128MP508 and Tool Starter Kits (PKOB)-SN:BUR182571251 then click Next. Note, there is a DM330028-2 version of the curiousity board so the device and tool will be different. Please refer to its user guide or check the actual part number of the dsPIC IC that is mounted on the board.
    MPLAB New Project Select Device Dialog
  3. Select Compiler XC16 (v1.70) then click next. You may have a newer version installed.
    MPLAB New Project Select Compiler Dialog
  4. Name the project as "master". Save it to folder "boost" then click Finish.
    MPLAB New Project Select Project Name and Folder Dialog Box

SLAVE CORE

  1. Ctrl+Shift+N to open again the new project dialog box. Select Standalone Project then click Next.
  2. Again, your curiosity may have a different device name but take note of the last two characters ("S1") for the device which means that it is the slave core. Select device dsPIC33CH128MO508S1 and Starter Kits (PKOB)-SN:BUR182571251 then click Next.
    MPLAB New Project Select Device dialog box
  3. Select compiler XC16 (v1.70) then click next.
  4. Name the project as "slave" and use the same project location with the master core. Click Finish.

CODE CONFIGURATION

MASTER CORE

  1. On the Projects Pane, if the slave project is highlighted in bold, right click on master project and select "Set as Main Project".
    MPLAB X IDE Set as Main Project menu
  2. Click the MCC button on the toolbox. a master.mc3 file will be created in the root of the master project folder after MCC has finished loading.
  3. In the device resources pane, click the + on the SLAVE CORE.
    MPLAB Device Resources Slave Core

    After clicking the + icon, wait until the SLAVE CORE appears on the project resources pane.
    MPLAB Project Resources Slave Core
  4. In the Slave Core Pane, name the Slave as "slave". Enable Protocol A and Protocol B. Set Protocol A Direction as M->S.
    MPLAB Master Settings Slave Name and Protocol Settings
  5. In the Pin Manager Pane, set RC1, RC14 and RC15 as owned by the Slave Core. RC1 is the voltage sense input. RC14 and RC15 are the PWM pins for controlling the MOSFETS of the buck-boost circuit.
    MPLAB MCC Pin Manager Slave Core Ownership
  6. In the System Module Pane, set clock output pin as "OSC2 is general purpose digital I/O pin".
    MPLAB MCC Master Clock Output Pin Configuration
  7. Go back to the Slave Core Pane and click Save Master Settings button. A file named master_config.mc3 will be generated on the root of the master folder after you click the Save Master Settings button.
  8. On the Project Resources pane, click the Generate button on the Project Resources pane. If there are warnings, review first the Notifications [MCC] pane.
  9. After confirmation on the generate configuration, you should see a Generation complete on the text output. After that, close the configurator by clicking the MCC button on the toolbar.

SLAVE CORE

  1. On the Projects Pane, right click on the slave project and select Set as Main Project.
  2. Click the MCC button on the toolbar. This will generate a file named slave.mc3 to the root of the slave project folder.
  3. Go to the Master Core Pane then click Load Slave Settings from Master Configuration button.
    MPLAB MCC Load Master Settings
  4. In the Load Master Settings dialog box, browse to the master core folder and open the master_config.mc3 file.
  5. In the Device Resources Pane, click the + on PWM. The PWM will appear on the Project Resources Pane afterwards.
    MPLAB MCC Device Resources Pane
  6. In the PWM Pane, select PWM Generator 7, rename the Custom Name to PWM_BOOST, enable PWM Generator, change the PWM Operation Mode to Independent Edge, dual output and change PWM Output Mode to Independent. Note that we can only use Generator 7 because it is the one connected to RC14 and RC15 PWM output.
    MPLAB MCC PWM Generator Configuration
  7. Change the Requested Frequency to 40KHz.
    MPLAB MCC PWM Generator Frequency configuration

    Below is the relationship of the Special Function Registers (SFR) to the Independent Edge dual output PWM generator from the dsPIC33/PIC24 FRM High-Resolution PWM with Fine Edge Placement document.
    dsPIC33/PIC24 dual independent output PWM SFR

    Below table defines these SFRs and abbreviations according to my understanding.
    AbbreviationDefinition
    PGxPERPWM Generator x (generator number) Period . The PWM
    counter counts from 0 up to the value of this SFR. This value is
    calculated as the value on the "PWM Input Clock Selection"
    (8MHz) divided by the value on "Requested Frequency"
    (40kHz) less 1 and is equal to 199 (0xC7).
    PGxDCPWM Generator x (generator number) high side output Duty
    Cycle. The PWM generator high side output changes to 0
    when the timer value is equal to the value of this SFR.
    PGxPHASEPWM Generator x (generator number) high side output Phase
    or deadtime. The PWM generator high side output  starts
    from 0 when timer value is 0 and changes to 1 when the timer
    value is equal to the value of this SFR. We actually don't need
    to use this.
    PWMxHPWM Generator x (generator number) high side output. For
    the dsPIC33 IC that we are using, this output is connected to
    the RC14 pin (control for the buck MOSFET). PWMxH = 1 if
    Timer>= PGxPHASE & Counter <= PGxDC else 0.
    PGxTRIGBPWM Generator x (generator number) low side output Duty
    Cycle. The PWM generator low side output changes to 0 when
    the timer value is equal to the value of this SFR.
    PGxTRIGAPWM Generator x (generator number) low side output phase
    or deadtime. The PWM generator low side output starts from
    0 when timer value is 0 and changes to 1 when the timer value
    is equal to the value of this SFR.
    PWMxLPWM Generator x (generator number) low side output. For the
    dsPIC33 IC that we are using, this output is connected to the
    RC15 pin (control for the boost MOSFET). PWMxL = 1 if Timer
    >= PGxTRIGA & Counter <= PGxTRIGB else 0.
     The initialization of these SFRs will be automatically defined later on a pwm.c file generated by the MCC.
  8. Change the ADC Trigger 1 to Trigger A Compare and Trigger A Compare time to 1ns.
    MPLAB MCC PWM ADC Trigger Configuration
  9. Change the Data Update Trigger to Duty Cycle.
    MPLAB MCC PWM Update Trigger Configuration

    This setting means that the duty cycle of the generator (both H side and L side) will be updated every time PG7DC is updated.
  10. In the Device Resources pane, click + of the ADC1 to transfer the device to the Project Resources.
    MPLAB MCC Device Resources ADC1
  11. In the ADC1 pane, enable Core1, change Core1 channel to S1ANA1 and Trigger Source to Slave PWM1 Trigger1. Tick the Interrupt Box.
    MPLAB MCC ADC Configuration
  12. In the Pin Module Pane, change the name of RC1 to RC1_VOUTFB.
    MPLAB MCC Pin Module Configuration
  13. Review the MCC Notifications. Usually I get the warnings like below.
    MPLAB MCC Slave notifications
  14. In the Project Resources pane, click Generate button. Click Yes to disregard the warnings and proceed generating the source files. Wait until a "Generation complete" text appears on the MPLAB Code Configurator prompt window.
  15. Close the code configurator by clicking the MCC toolbar button.

SET THE SLAVE CORE AS SECONDARY PROJECT OF MASTER CORE

  1. Set master project as the main project.
  2. Right click on the Secondaries folder of master project and click Add Secondary Project...
    MPLAB Add Secondary Project
  3. In the Add Secondary Project dialog window, select slave.X and click Add button.
    MPLAB Add Secondary Project dialog
  4. Right-click again on the Secondaries Folder and select Properties. Tick the Build box of the properties dialog window then click OK.

ACTUAL CODING

MASTER CORE SOURCE CODE

  1. Open the main.c file on the master project's "Sources Files" folder.
  2. Below the #include "mcc_generated_files/system.h", add the following codes:
    #include "mcc_generated_files/slave1.h"
    #include "mcc_generated_files/pin_manager.h"
  3. Below the SYSTEM_Initialize();, add the following codes:
    SLAVE1_Program();
    SLAVE1_Start();

    At the time of this writing, SLAVE1_Program() and SLAVE1_Start() functions are deprecated so I would expect I will need a replacement for these functions in the future.
  4. Below should be how the master core main.c would look like:
    MPLAB Master Core Source Code
  5. Close the main.c file of the master core. Click Save when asked.

SLAVE CORE SOURCE CODE

  1. Open the main.c file on the slave project's Source Files folder.
  2. Below the #include "mcc_generated_files/system.h", add the following codes:
    #include "mcc_generated_files/pin_manager.h"
    #include "mcc_generated_files/pwm.h"
    #include "mcc_generated_files/adc1.h"

    /*PID function and variable definitions*/
    void PID(void);
    int16_t n16_integrator_memory;
    int16_t n16_reference;
    int16_t n16_error;
    int16_t n16_proportional;
    int16_t n16_sum;
    int16_t n16_limit; 

    #define target_voltage 12 
    #define reference 200*target_voltage + 230 
    #define p_gain 0.1
    #define i_gain 0.05

    The p_gain and i_gain values are the parameters that you can adjust to fine tune the gain loop response of the boost converter. You can also fine tune the offset of the reference to get an exact 12V output.
  3. Inside the int main(void), add the following codes after SYSTEM_Initialize();
    // Initialize PWM to 0% duty cycle (The boost Q2 is NMOS, 1=on)
    PG7TRIGB = 0;
    // Duty cycle limit (ensure boost MOSFET doesn't short)
    n16_limit = (int16_t)(PG7PER-10); 
    //Initialize PID variables
    n16_integrator_memory = 0;
    n16_reference = (int16_t) reference;
    //Assign PID Handler
    ADC1_SetRC1_VOUTFBInterruptHandler(PID);
  4. After the int_main(void) function, add the following code:
    void PID(void)
    {
      n16_error = n16_reference - ADCBUF1;
      n16_integrator_memory = n16_integrator_memory + i_gain*n16_error;
      n16_proportional = p_gain*n16_error;
      n16_sum = n16_integrator_memory + n16_proportional;
      if (n16_sum > n16_limit) {n16_sum = n16_limit;} 
      if (n16_sum < 0) {n16_sum = 0;}
      PG7TRIGB = n16_sum;
      PG7DC = 0; // Need this to update PG7TRIGB
    }
  5. The new main.c of the slave core should now look like below:
    MPLAB DSPIC33CH Boost Converter Source Code

RESULT

  1. Be sure to set the master project as the main project.
  2. Be sure that the Curiosity board is connected to the PC.
  3. Click the program button on the toolbar
    MPLAB program button
    . Wait until the "Programming/Verify complete" is prompted on the Starter Kit on Board Pane.
    MPLAB Starter Kit on Board
  4. Below is my hardware setup to validate the result. I use ADALM2000 as oscilloscope.
    DM330028 dsPIC33CH Curiosity with ADALM2000
  5. Below is the waveform taken from the ADALM2000 oscilloscope.
    ADALM2000 oscilloscope

 

 

Comments