Timer Config Block Icon
TIMER Config - Timer Configuration Block Timer Block Icon The MCHP_TIMER_Config block provides a high-level interface for configuring hardware timers on Microchip microcontrollers. This block creates and manages named timer resources that can be shared across multiple peripherals in the model, providing automatic timer allocation and optimal prescaler selection. Unlike direct timer register access, this block manages the complete timer lifecycle including resource allocation, conflict resolution, and automatic prescaler calculation based on desired resolution and period requirements.

Block Inputs/Outputs

Inputs: None (configuration block only)

Outputs: None (configuration block only)

This is a configuration block that sets up hardware timer resources. It does not have signal ports—other blocks (Input Capture, Output Compare, etc.) reference the timer by name.

Supported Device Families

FamilySupport LevelAvailable TimersPrescaler Options
dsPIC30F/33F/33E/33C/33AFullTimer1-Timer9 (device dependent)1:1, 1:8, 1:64, 1:256
PIC24FFullTimer1-Timer5 (device dependent)1:1, 1:8, 1:64, 1:256
PIC32 (MIPS)FullTimer1-Timer51:1, 1:2, 1:4, 1:8, 1:16, 1:32, 1:64, 1:256
PIC32AFullTimer1-Timer9 (device dependent)1:1, 1:8, 1:64, 1:256
SAMx7 (ARM M7)FullTC0-TC11 (3 channels each)1:8, 1:32, 1:128
SAMx2/x5 (ARM M0+/M4)FullTC0-TC71:1, 1:2, 1:4, 1:8, 1:16, 1:64, 1:256, 1:1024

Configuration Parameters

Timer Identification

ParameterDescriptionOptions/RangeDefault
TIMER_NameNamed timer resource identifierTimerA-TimerF, Custom NameTimerA
TIMER_CustomNameUser-defined timer name (when Custom Name selected)Valid MATLAB identifierTimerG

Timer Resource Selection

ParameterDescriptionOptions
TIMER_RessourceHardware timer allocation constraintNo constraints [Preferred]
TIMER_Ressource_CustomCustom timer list (e.g., [1 3 5])Array of timer indices

Timer Resolution and Period

ParameterDescriptionNotes
TIMER_ResolutionTimer tick resolution and maximum periodDropdown shows: “Resol: X.XXµs - MaxPer: Y.YYms” for each prescaler option
TIMER_PeriodChkUse maximum period (65535 ticks)When ON: PR register = 65535
TIMER_PeriodDesired timer period (seconds)Active when TIMER_PeriodChk is OFF. Can be MATLAB variable or numeric value

Timer Sharing Configuration

ParameterDescriptionOptions
TIMER_ShareChkEnable timer sharing between blocksON = Shared, OFF = Exclusive use
TIMER_UseSharing permission levelRead only
DoNotSetupChkReserve timer for custom C codeWhen ON: No timer initialization, manual setup required

Implementation Details

Register Configuration

The block configures the following timer registers based on device family:

dsPIC/PIC24/PIC32A (TxCON register)

  • TON - Timer ON bit (controlled by startup mode)
  • TCKPS - Timer Input Clock Prescale Select (1:1, 1:8, 1:64, 1:256)
  • TCS - Timer Clock Source (internal peripheral clock)
  • TGATE - Timer Gated Time Accumulation Enable (disabled)
  • PRx - Period register (calculated from desired period)

PIC32 MIPS (TxCON register)

  • ON - Timer Enable bit
  • TCKPS - Clock Prescale (3 bits: 1:1 to 1:256)
  • TCS - Clock Source (PBCLK)
  • PRx - Period register

SAMx (ARM) - TC Module

  • TCCLKSx - Clock Selection (MCK/8, MCK/32, MCK/128)
  • WAVE - Waveform mode (enabled for timer operation)
  • RC - Register C (period value)
  • CMR - Channel Mode Register configuration

Timer Type Classification

The block manages timers using an internal type system:

TypeDescriptionUse Case
0Ticks only (no register access)Time base reference only
1Read with PR = 65535Maximum period, read-only
2Read with fixed PR ≠ 65535Specific period, read-only
3Read/Write with PR modificationShared timer with PR updates
4Full control (Read/Write/Start/Stop)Exclusive timer ownership

Automatic Timer Allocation

The block uses the MCHP_Fun.TimerConfig() function for intelligent timer assignment:

  • Conflict Detection - Scans all Timer Config blocks for duplicate names
  • Resource Pooling - Builds available timer list based on constraints
  • Optimal Selection - Chooses timer that best matches requirements
  • Prescaler Calculation - Automatically selects prescaler for desired resolution
  • Period Register - Calculates PR value to achieve target period

Usage Examples

Example 1: Timer for Periodic Interrupt

% Configure 1ms periodic timer
Timer Name: TimerA
Timer Resource: No constraints [Preferred]
Timer Period: 0.001  % 1ms
Use Maximum Period: OFF
Share Timer: OFF (exclusive use)

Result: Automatically selects optimal prescaler and timer peripheral for 1ms period, exclusive ownership.

Example 2: Shared Timer for Multiple Peripherals

% High-resolution timer shared between PWM and OC
Timer Name: TimerPWM
Timer Resource: Timer 2  % Force use of Timer2
Use Maximum Period: ON  % PR = 65535
Share Timer: ON
Timer Use: Read only  % Allow other blocks to read

Result: Timer2 configured at maximum resolution, other blocks can reference “TimerPWM” for synchronization.

Example 3: Custom C Code Timer Reservation

% Reserve Timer3 for manual configuration
Timer Name: CustomTimer
Timer Resource: Timer 3
Do Not Setup This Timer: ON  % Reserve only

Result: Timer3 reserved but not initialized. User can configure in custom C code using the reserved resource.

Example 4: Variable Period Control

% MATLAB workspace variable control
samplingPeriod = 0.0001;  % 100µs base period

Timer Name: TimerControl
Timer Period: samplingPeriod
Share Timer: ON
Timer Use: Read / Write / Start-Stop / Period update

Result: Timer configured for 100µs, can be modified at runtime by other blocks.

Timing Considerations

Resolution vs. Range Trade-off

Timer resolution and maximum period are inversely related through the prescaler:

PrescalerResolution @ 60MHzMax Period (16-bit)
1:116.67 ns1.09 ms
1:8133 ns8.74 ms
1:641.07 µs69.9 ms
1:2564.27 µs280 ms

Period Calculation Accuracy

  • Period register is integer only (0-65535)
  • Actual period may differ slightly from requested value
  • Block automatically rounds to nearest achievable period
  • Check model compilation output for actual achieved period

Clock Source Dependencies

  • dsPIC/PIC24: Uses instruction cycle clock (Fcy = Fosc/2)
  • PIC32: Uses peripheral bus clock (PBCLK)
  • SAMx: Uses master clock (MCK) or GCLK
  • Ensure clock configuration in Master Block matches hardware setup

Troubleshooting

Common Issues and Solutions

Error: “Timer [Name] already defined”

Cause: Duplicate timer names in model.

Solution: Use unique names (TimerA, TimerB, etc.) or custom names. Block automatically suggests alternative names.

Warning: “Cannot achieve exact period”

Cause: Requested period not achievable with integer PR value.

Solution:

  • Accept nearest achievable period (shown in compilation output)
  • Try different prescaler setting
  • Adjust requested period to align with clock frequency

Timer Conflict with Other Peripherals

Cause: Multiple peripherals requesting same timer with incompatible settings.

Solution:

  • Use “Share Timer: ON” with compatible Timer Use settings
  • Use “No constraints” to allow automatic timer selection
  • Manually assign different timers to each peripheral

No Available Timers

Cause: All timers allocated to exclusive-use blocks.

Solution:

  • Enable timer sharing where possible
  • Reduce number of Timer Config blocks
  • Use single shared timer for multiple peripherals
  • [MCHP_IC - Input Capture] - Uses configured timers for event timestamping
  • [MCHP_OC_HW - Output Compare (Hardware)] - Requires timer timebase
  • [MCHP_OC_SW - Output Compare (Software)] - Uses timer for software compare
  • [MCHP_TIMER_PIL_Profiling] - Uses timers for code execution profiling
  • [MCHP Interrupt Blocks] - Timer interrupts for periodic tasks
  • [MCHP_PWM Blocks] - PWM blocks use timers for frequency control

See Also

MPLAB Blockset for Simulink