<picture>
    <source media="(prefers-color-scheme: dark)" srcset="images/microchip_logo_white_red.png">
  <source media="(prefers-color-scheme: light)" srcset="images/microchip_logo_black_red.png">
    <img alt="Microchip Logo." src="images/microchip_logo_black_red.png">
</picture> 

# MATLAB/Simulink Digital Power LV Totem-Pole PFC Simulation and Code Generation Model

## 1. INTRODUCTION

This document describes the setup and the model which illustrates design methodology of single phase totem pole PFC topology. Two models are provided, a simulation model and a protected code generation model. Both models use the same control block, which makes them fully coherent from the simulation to the execution on the platform.


[3.8kW/7.6kW Totem Pole Demonstration Application (TPPFC)](https://www.microchip.com/en-us/tools-resources/reference-designs/3-8kw-7-6kw-dspic33c-totem-pole-development-application) is a modular carrier platform board for plug-in SiC and dsPIC33 cards. The TPPFC enables development in low voltage and high voltage configurations. In this example, low voltage configuration is used. </p>

The [dsPIC33AK128MC106 Digital Power Plug-In Module (DP PIM)](https://www.microchip.com/en-us/development-tool/ev67k87a) is a demonstration board that in conjunction with different power boards (such as TPPFC) showcases the Microchip dsPIC33AK128MC106 Digital Signal Controller (DSC) features.  It directly plugs-in to the Digital Power Development Board (DM330029) and the Low Power FPC Development Kit (DV330101).

The DP PIM provides access to the dsPIC33AK128MC106 analog inputs, the Digital-to-Analog Converter (DAC) output, the Pulse-Width Modulator (PWM) outputs and the General-Purpose Input and Output (GPIO) ports. 

In this example, the PIM board is used as the control card for the TPPFC carrier power stage board configured in the low voltage mode. 

## 2. SUGGESTED DEMONSTRATION REQUIREMENTS

### 2.1 MATLAB Model Required for the Demonstration

To acquire this MATLAB model: 

- MATLAB model can be cloned or downloaded as zip file

### 2.2 Software Tools Used for Testing the MATLAB/Simulink Model

- MPLAB® X IDE **v6.25** 
- MPLAB® XC-DSC Compiler **v3.19**
-	MATLAB R2024a
-	Required MATLAB add-on packages
    -	Simulink
    -   DSP System Toolbox
    -   Signal Processing Toolbox
    -   Simscape
    -   Simscape Electrical

Furthermore, for the code generation (protected), the following is needed: 
-	Simulink Coder
-	MATLAB Coder
-	Embedded Coder
-	MPLAB Device blocks for Simulink (v3.57)
       
> **_NOTE:_**
>The software tools used for testing the model during release is listed above. It is recommended to use the version listed above or later versions for building the model.

  ### 2.3 Hardware Tools Required for the Demonstration

- [3.8kW/7.6kW dsPIC33C Totem Pole Demonstration Application](https://www.microchip.com/en-us/tools-resources/reference-designs/3-8kw-7-6kw-dspic33c-totem-pole-development-application)
- dsPIC33AK128MC106 Digital Power Plug-In Module (PIM) [(EV67K87A)](https://www.microchip.com/en-us/development-tool/ev67k87a) 

## 3. HARDWARE SETUP
This section describes the hardware setup required for the demonstration.
> **Note:** </br>
>In this document, hereinafter **dsPIC33AK128MC106 Digital Power Plug-In Module (PIM)** is referred as **PIM**.

1. Insert the **dsPIC33AK128MC106 Digital Power Plug-In Module (PIM)** into the PIM Interface of the **TTPFC** board (number 4 in the following picture). 

     <p align="left" >
     <img  src="images/3_1.png"width="500"></p>

2. Connect the **ICD4 programmer** to the **J2** connector of the PIM card, which is used for programming, and running the dsPIC33A DSC in External or Standalone Mode. Connect the host PC to the USB port on the PIM.



The TTPFC reference design board has to be properly equipped and connected in accordance to its  [User's Guide](https://ww1.microchip.com/downloads/aemDocuments/documents/MCU16/ProductDocuments/UserGuides/3.8kW-7.6kW-dsPIC33C-Totem-Pole-Demo-Users-Guide-DS70005569.pdf) and [Operation Mode Guide](https://ww1.microchip.com/downloads/aemDocuments/documents/MCU16/ProductDocuments/UserGuides/3.8kW-7.6kW-dsPIC33C-Totem-Pole-Operation-Mode-Guide-PFC-GTI-DS70005568.pdf), in PFC configuration.

A 10:1 transformer at the input is needed to provide corresponding voltage in the low-voltage setup. A suggested setup can be the following:
 <p align="left"> <img  src="images/3_2a.png"width="500"></p>

These steps are only needed if the protected code generation mode is used.

The simulation model **DP_PFC_on_MC_LVTPPFC_sim.slx** contains the plant model (TPPFC board), as well as the model of the controller based on 2p2z. The same controller is later instantiated in the **DP_PFC_on_MC_LVTPPFC_cg.slx** model for protected code generation to get experimental results.

The focus of the described demonstration is on how to properly run the simulation file **DP_PFC_on_MC_LVTPPFC_sim.slx**.

## 4. BASIC DEMONSTRATION
<p style='text-align: justify;'> Follow the below instructions step-by-step, to set up and run the simulation model:</p>

1. Launch MATLAB (refer the section [“2.2 Software Tools Used for Testing the MATLAB/Simulink Model"](#22-software-tools-used-for-testing-the-matlabsimulink-model)).</p> 

2. Open the folder downloaded from the repository or the download link, in which MATLAB files are saved (refer the section ["2.1 MATLAB Model Required for the Demonstration"](#21-matlab-model-required-for-the-demonstration)).

    <p align="left" >
    <img  src="images/4_2.png"width="500"></p>

3.	Double click on the Simulink model - **DP_PFC_on_MC_LVTPPFC_sim.slx**. This opens the Simulink model as shown below.

    <p align="left">
      <img  src="images/4_3.png"width="500"></p>
    </p>

    > **Note:** </br>
    >This example demonstrates the simulation optimized for 100kHz switching frequency and complementary PWM with a lot of zero crossing events in the simulation. 

4.	<p style='text-align: justify;'>The model contains the plant model (TPPFC), the controller, models of ADC and PWM, as well as the load and the observing blocks.  <p style='text-align: justify;'>

5. <p style='text-align: justify;'>	This plant is modelled in Simscape Electrical, adapted so that the simulation speed is satisfactory. High resolution of the simulation results is achieved by capturing zero crossings, and ensuring accuracy around these events.

6.	<p style='text-align: justify;'>	To simulate and run the code, click Run in the Simulation tab.

    <p align="left">
      <img  src="images/4_6.png"width="500"></p>
    </p>
7.	<p style='text-align: justify;'> The simulation will now run, and the results can be inspected in the Data Inspector (input current, output voltage etc.). The convergence and the dynamics of the control can also be indirectly observed, and so can low level PWM signals. The initial charging transient can be observed, after which the input current is properly shaped and the output voltage converges with expected ripple.

    <p align="left">
      <img  src="images/4_7.png"width="500"></p>



    <p align="left">
      <img  src="images/4_7a.png"width="500"></p>


8. </p><p style='text-align: justify;'> The slow switching leg operates at the grid frequency, whereas the fast switching leg operates at 100kHz. 

    <p align="left">
      <img  src="images/4_8.png"width="500"></p>
    </p>

9. <p style='text-align: justify;'>	The high frequency PWM signals (Q1 and Q2) can be observed, as well as synchronous rectifiers (Q3 and Q4). The reference setpoint Vref which is initially at 50V can be changed to observe settling time and the dynamics of the system.

    <p align="left">
      <img  src="images/4_9.png"width="500"></p>
    </p>

10. <p style='text-align: justify;'>	The model contains simple ADC and PWM models. The plant model can be modified as well, to better understand the deviation from the nominal component values.

    <p align="left">
      <img  src="images/4_10.png"width="500"></p>
    </p>

11. <p style='text-align: justify;'>The load used in the simulation is purely passive, consisting of a Zener diode in series with a resistor. This can also be modified in order to inspect the behaviour with different loads. 

    <p align="left">
      <img  src="images/4_11.png"width="200"></p>
    </p>


## 	REFERENCES:
For more information, refer to the following documents or links.
1. [3.8kW/7.6kW dsPIC33C Totem Pole Demonstration Application](https://www.microchip.com/en-us/tools-resources/reference-designs/3-8kw-7-6kw-dspic33c-totem-pole-development-application)
2. dsPIC33AK128MC106 Digital Power Plug-In Module (PIM) [(EV67K87A)](https://www.microchip.com/en-us/development-tool/ev67k87a) Digital-Power-Development-Board-Users-Guide-DS50002814B.pdf)
3. MPLAB® X IDE User’s Guide [(DS50002027)](https://ww1.microchip.com/downloads/en/DeviceDoc/50002027E.pdf) or [MPLAB® X IDE help](https://microchipdeveloper.com/xwiki/bin/view/software-tools/x/)
4. [MPLAB® X IDE installation](http://microchipdeveloper.com/mplabx:installation)
5. [MPLAB® XC-DSC Compiler installation](https://developerhelp.microchip.com/xwiki/bin/view/software-tools/xc-dsc/install/)
6.  [MPLAB Device Blocks for Simulink :dsPIC, PIC32 and SAM mcu](https://in.mathworks.com/matlabcentral/fileexchange/71892-mplab-device-blocks-for-simulink-dspic-pic32-and-sam-mcu)
7. [External Mode Demo](https://www.microchip.com/en-us/about/media-center/videos/wuzLmORk1M0)
9. [Implementing Power Factor Correction Webinar](https://www.mathworks.com/videos/implementing-power-factor-correction-on-a-microchip-dspic33a-digital-signal-controller-1734709897715.html)