Implementing 5V to 12V PWM Boost Converter in the DSPIC33CH Curiosity Development Board
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).
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.
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.
- 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.
- After opening MPLAB, close any projects that are open on the left pane.
MASTER CORE
- Ctrl+Shift+N to open the new project dialog box. Select Standalone Project then Click Next.
- 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.
- Select Compiler XC16 (v1.70) then click next. You may have a newer version installed.
- Name the project as "master". Save it to folder "boost" then click Finish.
SLAVE CORE
- Ctrl+Shift+N to open again the new project dialog box. Select Standalone Project then click Next.
- 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.
- Select compiler XC16 (v1.70) then click next.
- Name the project as "slave" and use the same project location with the master core. Click Finish.
CODE CONFIGURATION
MASTER CORE
- On the Projects Pane, if the slave project is highlighted in bold, right click on master project and select "Set as Main Project".
- 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.
- In the device resources pane, click the + on the SLAVE CORE.
After clicking the + icon, wait until the SLAVE CORE appears on the project resources pane. - In the Slave Core Pane, name the Slave as "slave". Enable Protocol A and Protocol B. Set Protocol A Direction as M->S.
- 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.
- In the System Module Pane, set clock output pin as "OSC2 is general purpose digital I/O pin".
- 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.
- On the Project Resources pane, click the Generate button on the Project Resources pane. If there are warnings, review first the Notifications [MCC] pane.
- 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
- On the Projects Pane, right click on the slave project and select Set as Main Project.
- Click the MCC button on the toolbar. This will generate a file named slave.mc3 to the root of the slave project folder.
- Go to the Master Core Pane then click Load Slave Settings from Master Configuration button.
- In the Load Master Settings dialog box, browse to the master core folder and open the master_config.mc3 file.
- In the Device Resources Pane, click the + on PWM. The PWM will appear on the Project Resources Pane afterwards.
- 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.
- Change the Requested Frequency to 40KHz.
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.
Below table defines these SFRs and abbreviations according to my understanding.
The initialization of these SFRs will be automatically defined later on a pwm.c file generated by the MCC.Abbreviation Definition PGxPER PWM 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).PGxDC PWM 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.PGxPHASE PWM 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.PWMxH PWM 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.PGxTRIGB PWM 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.PGxTRIGA PWM 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.PWMxL PWM 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. - Change the ADC Trigger 1 to Trigger A Compare and Trigger A Compare time to 1ns.
- Change the Data Update Trigger to Duty Cycle.
This setting means that the duty cycle of the generator (both H side and L side) will be updated every time PG7DC is updated. - In the Device Resources pane, click + of the ADC1 to transfer the device to the Project Resources.
- In the ADC1 pane, enable Core1, change Core1 channel to S1ANA1 and Trigger Source to Slave PWM1 Trigger1. Tick the Interrupt Box.
- In the Pin Module Pane, change the name of RC1 to RC1_VOUTFB.
- Review the MCC Notifications. Usually I get the warnings like below.
- 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.
- Close the code configurator by clicking the MCC toolbar button.
SET THE SLAVE CORE AS SECONDARY PROJECT OF MASTER CORE
- Set master project as the main project.
- Right click on the Secondaries folder of master project and click Add Secondary Project...
- In the Add Secondary Project dialog window, select slave.X and click Add button.
- 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
- Open the main.c file on the master project's "Sources Files" folder.
- 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" - 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. - Below should be how the master core main.c would look like:
- Close the main.c file of the master core. Click Save when asked.
SLAVE CORE SOURCE CODE
- Open the main.c file on the slave project's Source Files folder.
- 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. - 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); - 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
} - The new main.c of the slave core should now look like below:
RESULT
- Be sure to set the master project as the main project.
- Be sure that the Curiosity board is connected to the PC.
- Click the program button on the toolbar. Wait until the "Programming/Verify complete" is prompted on the Starter Kit on Board Pane.
- Below is my hardware setup to validate the result. I use ADALM2000 as oscilloscope.
- Below is the waveform taken from the ADALM2000 oscilloscope.
Comments