Input Capture Block Icon
The MCHP_IC block captures hardware timer values when external events occur on input pins. It enables precise measurement of pulse widths, periods, frequencies, and event timestamps using dedicated Input Capture peripheral modules. Each IC channel can detect rising edges, falling edges, or both, and automatically capture the associated timer value. This is ideal for encoder interfaces, frequency measurement, and event timing applications.

Block Inputs/Outputs

Inputs: None (captures from hardware pins)

Outputs: Dynamic based on configuration

Output PortConditionDescription
IC1_Up, IC1_DownMode 3Pulse width measurements (rising and falling edge times)
IC1_Up, IC1_PeriodMode 5High pulse width and signal period
IC1_Down, IC1_PeriodMode 10Low pulse width and signal period

Block Output Datatype

The block supports 1 output datatype representation:

Output TypeRangeDescription
uint160 to ICxmaxRaw timer counts at capture event

Uint Raw Output (1 option)

Integer values transferred directly from ICxBUF capture registers—no runtime computation. Users handle conversion to physical time units.

Workspace Variables

The block creates workspace variables for scaling calculations:

VariableDescription
IC1max, IC2max, …Maximum timer count per IC channel (based on associated timer)
ICxmaxVector of all channel max values (e.g., [65535 65535])

Scaling Formula

Scaling formula: Time_seconds = Timer_Ticks × (Max_Period_s / ICxmax)

Where Max_Period_s is the configured timer period in seconds.

Example: With IC1max = 65535 and timer max period = 0.25s:

Time_Resolution = 0.25 / 65535;  % ≈ 3.8 µs per tick
Pulse_Width_s = IC1_Up × Time_Resolution;

Raw counts preserve full resolution—users apply application-specific scaling (frequency, RPM, duty cycle) in their model.

Hardware Considerations: PIC32 devices have hardware FPU—float scaling is efficient. dsPIC30F/33F/33E/33C lack FPU—use fixed-point arithmetic in time-critical loops. dsPIC33A has hardware FPU.

Block Parameters

Channel Selection

ParameterDescriptionExample
ChannelIC channel numbers to use[1 2 3] for IC1, IC2, IC3

Capture Mode

Mode ValueDescriptionCaptures
1 (0x001)Edge detect, every edgeEvery rising or falling edge
2 (0x002)Edge detect, every 4th edgeEvery 4th rising or falling edge
3 (0x003)Edge detect, every 16th edgeEvery 16th rising or falling edge
4 (0x004)Capture rising edge onlyTimer value on rising edge
5 (0x005)Capture falling edge onlyTimer value on falling edge
6 (0x006)Capture every 4th rising edgeEvery 4th rising edge
7 (0x007)Capture every 16th rising edgeEvery 16th rising edge

Measurement Configuration

ParameterDescriptionOptions
Channel_UP_Down_PeriodeMeasurement type per channel3: Pulse width (up & down), 5: Pulse up + period, 10: Pulse down + period
ChangeDetectOutput only when value changesON/OFF per channel
Timer_RefAssociated timer for each channelTimer index or -2 for system timer

Pin Mapping (for remappable devices)

ParameterDescription
PIN_IC1 … PIN_IC16Remappable pin selection for each IC channel (dsPIC33E/C/A, PIC32)

Device Support

FamilyIC ChannelsEdge DetectionTimer Association
dsPIC33F/33EIC1-IC8Rising, Falling, BothTimer2/Timer3
dsPIC33C/33AIC1-IC16 (device dependent)Rising, Falling, BothTimer1-Timer5
PIC24FIC1-IC9Rising, Falling, BothTimer2/Timer3
PIC32MKIC1-IC9Rising, Falling, BothTimer2/Timer3

Implementation Details

Register Configuration (dsPIC)

  • ICxCON1 register:

  • ICSIDL = 0 (continue in idle mode)

  • ICTMR = timer selection (0=Timer3, 1=Timer2, others device-specific)

  • ICI = interrupt prescaler

  • ICM = capture mode (0x1-0x7)

  • ICxBUF - Capture buffer (read-only, contains timer value)

  • ICxCON2 (33C/33A) - Additional control bits, trigger synchronization

Interrupt Handling

IC interrupts trigger on each capture event:

  • Edge detected on ICx pin
  • Timer value captured to ICxBUF
  • IC interrupt flag set (IFS)
  • ISR reads ICxBUF and processes measurement
  • Measurement output updated (pulse width, period, etc.)

Timing Calculation

% Pulse width measurement (mode 3)
RisingEdge_Time = IC_RisingCapture / Timer_Frequency;
FallingEdge_Time = IC_FallingCapture / Timer_Frequency;
PulseWidth = FallingEdge_Time - RisingEdge_Time;

% Period measurement (mode 5)
Period = (IC_CurrentCapture - IC_PreviousCapture) / Timer_Frequency;

Usage Examples

Example 1: Quadrature Encoder Interface

% Measure encoder pulses on IC1 and IC2
Channel: [1 2]
Capture Mode: [4 4]  % Rising edge only on both
Channel_UP_Down_Periode: [3 3]  % Pulse measurement
Timer_Ref: [2 2]  % Both use Timer2
Change Detect: [ON ON]  % Output only on changes

Result: Captures encoder A/B channel transitions for position/speed calculation.

Example 2: Frequency Measurement

% Measure signal frequency on IC1
Channel: 1
Capture Mode: 4  % Rising edge
Channel_UP_Down_Periode: 5  % Pulse + period
Timer_Ref: 3  % Timer3 with maximum range

Result: Outputs pulse width and period. Frequency = 1 / (Period_ticks / Timer_Freq)

Example 3: PWM Duty Cycle Measurement

% Measure PWM duty cycle
Channel: 1
Capture Mode: 1  % Every edge (rising and falling)
Channel_UP_Down_Periode: 5  % Up time + period

Result: DutyCycle = (PulseWidth / Period) * 100%

Example 4: Event Timestamping

% Timestamp external events
Channel: [1 2 3]  % Three event sources
Capture Mode: [4 4 4]  % Rising edge trigger
Timer_Ref: [-2 -2 -2]  % Use high-resolution system timer

Result: Precise event timestamps for synchronization or logging.

Timing Considerations

Timer Selection Guidelines

  • High-frequency signals: Use Timer2/3 with 1:1 prescaler for best resolution
  • Low-frequency signals: Use higher prescaler or 32-bit timer mode to avoid overflow
  • Multiple channels: Share same timer when measuring related signals

Maximum Measurable Period

Timer ConfigMax Period @ 60MHz Fcy
16-bit, 1:1 prescaler1.09 ms
16-bit, 1:256 prescaler280 ms
32-bit mode71.6 seconds

Interrupt Latency

  • IC interrupt priority should be high for accurate measurements
  • Typical latency: 10-20 instruction cycles (dsPIC)
  • Use change detect to reduce output update rate if needed

Troubleshooting

No Capture Events

Check:

  • Input signal connected to correct IC pin
  • Pin configured as input (check PPS mapping)
  • Signal levels meet input threshold (typically 0.3VDD to 0.7VDD)
  • Capture mode matches signal characteristics

Incorrect Measurements

Check:

  • Timer frequency configuration matches actual clock
  • Timer not shared with conflicting peripheral
  • ICxmax variable correctly scaled for unit conversion
  • Capture mode appropriate for signal type

Overflow Issues

Solutions:

  • Increase timer prescaler for longer periods
  • Use 32-bit timer mode (if available)
  • Reduce edge prescaler (capture every edge instead of every 4th/16th)
  • [MCHP_TIMER_Config] - Configure timer timebase for IC
  • [MCHP_OC_HW] - Output Compare for PWM generation
  • [MCHP_QEI] - Dedicated quadrature encoder interface
  • [MCHP_Digital_Input] - General digital input (no timing)

See Also