User Guide Topics

The MPLAB Device Blocks for Simulink requires a Microchip compiler to generate and build embedded C code from your Simulink model. This guide explains which compilers are supported, how to download and install them, and how to configure MATLAB to use them.

Supported Compilers

The blockset supports three compiler families from Microchip, each targeting different device architectures:

CompilerTarget DevicesArchitectureFree License Features
XC16dsPIC30F, dsPIC33F, dsPIC33E, PIC24F/H/E16-bitOptimization up to -O2
XC-DSCdsPIC33C, dsPIC33A, PIC32A16-bit / 32-bit DSCOptimization up to -O2
XC32PIC32MK/MZ/MM, SAM (ARM Cortex-M)32-bitOptimization up to -O2
πŸ’‘ Free Compiler License: All Microchip compilers are free to download and use with no code size or time limits. The free license supports optimization levels -O0, -O1, and -O2. Advanced optimization levels (-O3, -Os) require a paid PRO license.

Compiler Selection by Device Family

Device FamilyCompilerMinimum VersionRecommended
dsPIC30FXC16v1.26+v2.10+
dsPIC33FXC16v1.26+v2.10+
dsPIC33EXC16v1.26+v2.10+
dsPIC33CXC-DSCv3.20+v3.30+
dsPIC33AXC-DSCv3.30+v3.39+
PIC32AXC-DSCv3.30+v3.39+
PIC24F/H/EXC16v1.26+v2.10+
PIC32MK/MZ/MMXC32v2.40+v4.45+
SAM (ARM Cortex-M)XC32v2.40+v4.45+
Note: XC32 is based on ARM GCC and supports both PIC32 (MIPS-based) and SAM (ARM Cortex-M) devices. The compiler automatically adapts to the target architecture based on the selected device.

Downloading Compilers

Download compilers from the official Microchip website:

πŸ”— microchip.com/compilers

Direct download links:

⚠️ Installation Recommendation: Install compilers to the default installation paths. MATLAB auto-detection works most reliably with default paths. If you must use custom paths, you will need to configure MATLAB manually (see Manual Configuration section below).

Default Installation Paths

Windows:

  • XC16: C:\Program Files\Microchip\xc16\v<version>\
  • XC-DSC: C:\Program Files\Microchip\xc-dsc\v<version>\
  • XC32: C:\Program Files\Microchip\xc32\v<version>\

Linux:

  • XC16: /opt/microchip/xc16/v<version>/
  • XC-DSC: /opt/microchip/xc-dsc/v<version>/
  • XC32: /opt/microchip/xc32/v<version>/

MATLAB Compiler Auto-Detection

MATLAB automatically detects installed Microchip compilers at these default locations. To verify compiler detection:

Step 1: Check Installed Compilers

Run picInfo('check') in the MATLAB Command Window:

>> picInfo('check')

--------------------------------------------
   Microchip Blockset 3.xx Installed:
--------------------------------------------

  2 Compiler(s) found for dsPIC chip:
   - C:/Program Files/Microchip/xc16/v2.10  (XC16  v2.10)
   - C:/Program Files/Microchip/xc-dsc/v3.30  (XC-DSC  v3.30)

  1 Compiler(s) found for 32 bits chip:
   - C:/Program Files/Microchip/xc32/v4.45  (XC32  v4.45)

Step 2: Refresh Compiler Cache (if needed)

If you just installed a new compiler, refresh MATLAB’s toolbox cache:

>> RTW.TargetRegistry.getInstance('reset')
>> rehash toolboxcache

Device Family Packs (DFP)

What is a DFP?

A Device Family Pack (DFP) contains device-specific information required for code generation:

  • Register definitions and memory maps
  • Peripheral specifications
  • Startup code and linker scripts
  • Programming and debugging support files

DFP Installation

DFPs are automatically installed with MPLAB X IDE. They can also be installed standalone:

  1. Via MPLAB X IDE (Recommended):

    • Tools β†’ Packs β†’ Device Family Pack Management
    • Select required device family
    • Click “Install Latest”
  2. Standalone Installation:

DFP Location:

  • Windows: C:\Program Files\Microchip\MPLABX\<version>\packs\Microchip\
  • Linux: /opt/microchip/mplabx/<version>/packs/Microchip/

Configuring Compiler Options

The Compiler Options Block provides control over:

  • Optimization level: -O0 (none), -O1, -O2, -O3 (PRO only), -Os (size, PRO only)
  • Debug information: Enable/disable DWARF debug symbols
  • Device Family Pack (DFP) path: Override auto-detected DFP location
  • Additional compiler flags: Custom -D defines, -I include paths, etc.

Configuration Steps:

  1. Add the Compiler Options block to your model
  2. Double-click to open configuration dialog
  3. Set optimization level (typically -O2 for production)
  4. Configure DFP path if using non-standard location
  5. Add any project-specific compiler flags

πŸ’‘ Optimization Levels:

        `-O0`: No optimization (fastest build, largest code, slowest execution)
        `-O1`: Basic optimization (balanced)
        `-O2`: Full optimization (recommended for production, free license)
        `-O3`: Aggressive optimization (requires PRO license)
        `-Os`: Size optimization (requires PRO license)

Manual Configuration

If MATLAB does not auto-detect your compiler (non-standard installation path, custom compiler version, etc.), you can configure it manually:

Method 1: Model Configuration Parameters

  1. Open Model Configuration Parameters (Ctrl+E)
  2. Navigate to: Code Generation β†’ MCHP Target
  3. Under Compiler Version, click Browse
  4. Navigate to the compiler’s bin folder:
    • XC16: <install path>/xc16/v<version>/bin/
    • XC-DSC: <install path>/xc-dsc/v<version>/bin/
    • XC32: <install path>/xc32/v<version>/bin/
  5. Click OK and rebuild the model

Method 2: Environment Variable

Set the compiler path in your system environment variables:

Windows:

setx MICROCHIP_XC16_PATH "C:\custom\path\xc16\v2.10"
setx MICROCHIP_XCDSC_PATH "C:\custom\path\xc-dsc\v3.30"
setx MICROCHIP_XC32_PATH "C:\custom\path\xc32\v4.45"

Linux:

export MICROCHIP_XC16_PATH="/opt/custom/xc16/v2.10"
export MICROCHIP_XCDSC_PATH="/opt/custom/xc-dsc/v3.30"
export MICROCHIP_XC32_PATH="/opt/custom/xc32/v4.45"

After setting environment variables, restart MATLAB and run RTW.TargetRegistry.getInstance('reset').

Troubleshooting

Problem: Compiler Not Detected

Symptom: Build fails with “Compiler not found” or picInfo('check') shows no compilers.

Solutions:

  1. Check installation path: Verify compiler is installed in default location
  2. Check PATH environment variable: Ensure compiler bin folder is in system PATH
  3. Reinstall compiler: Sometimes installer doesn’t complete properly
  4. Refresh MATLAB cache:
    >> RTW.TargetRegistry.getInstance('reset')
    >> rehash toolboxcache
    
  5. Manual configuration: Use Method 1 or 2 from Manual Configuration section above

Problem: Build Fails with “Device not supported”

Symptom: Code generation succeeds but build fails with device-specific error.

Solutions:

  1. Install correct DFP: Ensure Device Family Pack is installed for your target device
  2. Update DFP: Open MPLAB X IDE β†’ Tools β†’ Packs, update to latest version
  3. Verify device selection: Check that device name in Master block matches exactly

Problem: Linker Errors

Symptom: Build fails at linker stage with “undefined reference” or “section overflow” errors.

Common causes:

  • Missing compiler library path
  • Incorrect optimization level causing code size to exceed memory
  • Missing DFP files

Solutions:

  1. Verify compiler version: Ensure minimum version requirements are met (see table above)
  2. Enable optimization: Use -O1 or -O2 to reduce code size
  3. Check memory allocation: Verify model fits in target device memory
  4. Add additional linker flags in Compiler Options block if needed

Problem: Wrong Compiler Used for Device

Symptom: Build uses XC16 for dsPIC33C device (should use XC-DSC), or similar mismatch.

Solution:

  1. Check Master block device selection: Ensure correct device family is selected
  2. Verify compiler installation: Run picInfo('check') to confirm correct compiler is installed
  3. Rebuild model: Sometimes cached settings persistβ€”do clean build (Ctrl+Shift+B)

See Also

Home | User Guide | Code Generation | External Mode | PIL Testing | Video Demos | Block Reference