Navigation
User Guide Topics
The MCHP Blockset automatically generates optimized C code from Simulink models for deployment on Microchip microcontrollers.
Code Generation Workflow
- Model Design - Create algorithm in Simulink
- Configuration - Set up target hardware and compiler
- Build - Generate and compile code
- Deploy - Program device with generated firmware
Model Configuration Parameters
System Target File
Select the appropriate MCHP system target file for your device family:
MCHP_dsPIC_stf.tlc / MCHP_dsPIC_PIC24.tlc / MCHP_dsPIC_TC_stf.tlc - classic dsPIC (30F / 33F / 33E / 33C)MCHP_dsPIC33A_stf.tlc / MCHP_dsPIC33A_noTMF_stf.tlc - dsPIC33A / PIC32AMCHP_pic32_stf.tlc - PIC32 (MIPS)MCHP_ARM0p_stf.tlc / MCHP_ARM4F_stf.tlc / MCHP_ARM7_stf.tlc - SAM (ARM Cortex-M)
The correct target is selected automatically for the device chosen in the Master block.
Code Generation Settings
| Parameter | Recommended Value | Description |
|---|
| System target file | one of the MCHP *.tlc above | Selected automatically from the Master block device |
| Language | C | Generate C code (default) |
| Code interface packaging | Reusable function | For standalone deployment |
| Generate code only | Unchecked | Enable compilation |
MCHP Target Settings
Configure in Code Generation โ MCHP Target section:
- Device - Select target microcontroller
- Compiler Version - Select installed compiler
- Optimization Level -
-O0 (debug) to -O2 (free license); -O3 / -Os require a paid PRO license - Build Action - Build, Build and Download, etc.
Optimization Settings
Compiler Optimization Levels
| Level | Flag | Description | Use Case |
|---|
| 0 | -O0 | No optimization | Debug, code analysis |
| 1 | -O1 | Basic optimization | Development testing |
| 2 | -O2 | Moderate optimization | Balanced speed/size |
| 3 | -O3 | Aggressive optimization | Maximum performance |
| s | -Os | Size optimization | Memory-constrained devices |
Model Optimization
- Solver - Fixed-step, Discrete (no continuous time)
- Eliminate dead branches - Enable for code size reduction
- Inline parameters - Enable for performance
- Reusable functions - Enable for modular code
Custom Code Integration
Adding Custom C Code
Use Code Generation โ Custom Code section:
- Include directories - Add header file paths
- Source files - Add .c files to compilation
- Additional include files - Specify headers to include
- Custom code - Add code snippets (initialization, etc.)
Legacy Code Integration
Use Legacy Code Tool to wrap existing C functions:
- Create legacy code specification structure
- Register function interface with Simulink
- Generate S-Function wrapper
- Use S-Function block in model
Code Generation Process
Build Steps
- TLC Processing - Model converted to Target Language Compiler format
- Code Generation - C code generated from TLC
- Compilation - XC compiler builds object files
- Linking - Object files linked to create executable
- Post-processing - Generate .hex, .elf, .lst files
Generated Files
| File Type | Extension | Description |
|---|
| Source Code | .c, .h | Generated C source and headers |
| Executable | .elf | ELF format with debug symbols |
| Programming File | .hex | Intel HEX for device programming |
| Listing | .lst | Assembly listing with addresses |
| Map File | .map | Memory map and symbol table |
Fixed-Point Code Generation
For DSP and motor control applications:
- Use
fi() objects for fixed-point signals - Configure word lengths: 8, 16, 32 bits
- Set fraction lengths for precision
- Enable fixed-point code generation in model settings
dsPIC Assembly Optimization
The blockset includes hardware-optimized assembly code replacements (classic dsPIC 30F/33F/33E/33C):
- Trigonometric - sin, cos, sincos, atan2
- Square root - 16-bit and 32-bit fixed-point
- Saturation - optimized type conversions with overflow handling
- Arithmetic - absolute value, mixed signed/unsigned multiply
dsPIC33A / PIC32A additionally use the hardware FPU and single-precision math replacements (see release notes).
Note: Assembly replacements are automatically used when available for the target device family.
Memory Management
RAM Allocation
- Stack - Configured in linker script
- Heap - Avoid dynamic allocation if possible
- Global Variables - Minimize for better performance
- DMA Buffers - Use aligned memory sections
Flash Optimization
- Use
const for constant data - Enable compiler size optimization (-Os)
- Minimize duplicate code with reusable functions
- Use lookup tables for complex math
Build Diagnostics
Common Build Errors
| Error | Cause | Solution |
|---|
| Compiler not found | Path not configured | Set compiler path in model settings |
| Symbol not defined | Missing library/header | Add to custom code includes |
| Memory overflow | Code/data too large | Enable optimization, reduce model size |
| Unsupported block | Block not compatible | Replace with supported alternative |
Build Warnings
Address these warnings for robust code:
- Unused variables - Remove or comment out
- Type mismatches - Use explicit casts
- Uninitialized variables - Add initialization
- Deprecated functions - Update to current API
Code Review and Analysis
Generated Code Inspection
Review generated code for:
- Efficiency - Check loop structure, function calls
- Safety - Verify array bounds, pointer usage
- Readability - Enable code generation comments
- Compliance - MISRA-C, coding standards
Code Metrics
Use Code Generation Report to analyze:
- Lines of code generated
- ROM/RAM usage
- Function complexity
- Stack depth estimates
Best Practices
- Use discrete solver with fixed time step
- Avoid dynamic memory allocation
- Minimize global variable usage
- Use appropriate data types (int16, uint32, etc.)
- Enable code generation report for documentation
- Test with PIL before final deployment
- Use version control for model files
Home
| User Guide
| External Mode
| PIL Testing
| Compiler Setup
| Video Demos
| Block Reference