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 target for your device family:
MCHP_dsPIC_PIC24.tlc - dsPIC and PIC24 devicesMCHP_PIC32.tlc - PIC32 devicesMCHP_SAM.tlc - SAM (ARM) devices
Code Generation Settings
| Parameter | Recommended Value | Description |
|---|
| Target selection | ert.tlc | Embedded Coder target |
| 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 - 0 (debug) to 3 (max speed)
- 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
Blockset includes assembly code replacements for:
- Math Functions - sin, cos, atan2, sqrt
- Matrix Operations - multiply, transpose
- DSP Blocks - FIR, IIR filters, FFT
- Saturation - Optimized saturation arithmetic
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