Quadrature Decoder Using TC Modules - SAM Device Encoder Interface
Overview
The MCHP_QDEC_SAMx block implements quadrature encoder decoding using the Timer/Counter (TC) modules on SAM microcontrollers (E5x, E7x, C2x). Unlike dedicated QEI peripherals, this implementation uses TC modules in quadrature mode to decode encoder signals for position and speed measurement.
Key Features:
- Quadrature position decoding using TC modules
- 32-bit position counter with revolution counter
- Index pulse support for position reset
- Direction detection
- Speed measurement using time base
- Phase error detection (quadrature integrity check)
- Missing pulse error detection
- Configurable digital filtering
- Rotation change flag
Device Support
| Family | TC Modules | Max QDEC Instances | Features |
|---|
| SAM E54/E53 | TC0-TC7 | 2-4 | Position, speed, index, error detection |
| SAM E70/S70/V71 | TC0-TC11 | 4 | Position, speed, index, error detection |
| SAM C21 | TC0-TC7 | 2-4 | Position, speed, index, error detection |
Important: Each QDEC instance requires 3 TC channels (one complete TC module). For example, TC0 uses channels 0, 1, and 2 for phase A/B counting, index counting, and speed time base respectively.
Block Parameters
Module Selection
| Parameter | Options | Description |
|---|
| QDEC Module | TC0, TC3, TC6, TC9… | Select TC module (only modules with 3 channels shown) |
Operating Mode
| Parameter | Options | Description |
|---|
| QDEC Mode | โข Position | โข Speed |
Position Mode Configuration
| Parameter | Options | Description |
|---|
| Position Output | โข 16-bit unsigned | โข 16-bit signed |
| Position Reset Mode | โข never reset position | โข at user defined value |
| Reset Value | Integer | Value to reset position counter to |
| Reset Edge | โข rising edge | โข falling edge |
| Reset Input | on/off | Enable block input for counter reset |
Speed Mode Configuration
| Parameter | Options | Description |
|---|
| Speed Output | โข 16-bit unsigned | โข 32-bit unsigned |
| Speed Time Base | Multiple options with resolution/max period | Time base for speed calculation (selects TC prescaler) |
Index Counter (Revolution Counter)
| Parameter | Options | Description |
|---|
| Index Output | โข is not a block output | โข 16-bit unsigned/signed |
Signal Configuration
| Parameter | Options | Description |
|---|
| PHA Pin | Pin selection (e.g., PA12, PB16) | Phase A input pin (TIOA signal) |
| PHB Pin | Pin selection | Phase B input pin (TIOB signal) |
| IDX Pin | Pin selection or “not used” | Index pulse input pin (TIOB of adjacent channel) |
| Invert PHA | on/off | Invert phase A polarity |
| Invert PHB | on/off | Invert phase B polarity |
| Invert IDX | on/off | Invert index polarity |
| Swap PHA/PHB | on/off | Reverse encoder direction |
Advanced Features
| Parameter | Options | Description |
|---|
| Auto Correction | on/off | Automatic phase error correction |
| Max Period Filter | disabled or time period | Digital filter for input signals |
| Max Consecutive Missing Pulses | disabled or count | Threshold for missing pulse error detection |
Error Detection Outputs
| Output | Type | Description |
|---|
| Direction Status | Boolean | Current rotation direction (0=CCW, 1=CW) |
| Rotation Change Flag | Boolean | Direction changed since last sample |
| Missing Pulse Error | Boolean | Consecutive missing pulse threshold exceeded |
| Quadrature Error | Boolean | Phase error detected (invalid state transition) |
TC Module Configuration
Quadrature Decoding Mode
The TC module is configured in quadrature decoder mode (QDEC):
// Channel 0: Phase A/B counter (position) TC0->COUNT16.CTRLA.bit.MODE = 0x01; // 16-bit counter TC0->COUNT16.CTRLA.bit.ENABLE = 1; TC0->COUNT16.CTRLBSET.bit.DIR = 0; // Count up (default)// QDEC Configuration TC0->COUNT16.DRVCTRL.bit.INVEN0 = invert_pha; TC0->COUNT16.DRVCTRL.bit.INVEN1 = invert_phb; // Channel 1: Index counter (revolutions) TC1->COUNT16.CTRLA.bit.MODE = 0x01; TC1->COUNT16.EVCTRL.bit.EVACT = 0x01; // Count on event// Channel 2: Speed time base TC2->COUNT16.CTRLA.bit.MODE = 0x00; // 16-bit mode TC2->COUNT16.CTRLA.bit.PRESCALER = speed_prescaler;
SAM E70/S70/V71 TC Registers
// TC Channel 0 (Position Counter) TC0->TC_CHANNEL[0].TC_CMR = 0x00000006; // QDEC mode, QDPHASEL=0, QDPHBSEL=1 TC0->TC_CHANNEL[0].TC_BMR = …; // Block mode config// Position counter value position = TC0->TC_CHANNEL[0].TC_CV; // Direction dir = (TC0->TC_CHANNEL[0].TC_SR & TC_SR_DIR) ? 1 : 0; // Channel 1 (Index Counter) TC0->TC_CHANNEL[1].TC_CMR = …; // Capture mode for index index_count = TC0->TC_CHANNEL[1].TC_CV; // Channel 2 (Speed Time Base) TC0->TC_CHANNEL[2].TC_CMR = …; // Timer for speed measurement
Examples
Example 1: Optical Encoder Position Tracking
Application: Robotic Joint Position Feedback
Hardware: 1024 PPR optical encoder
Configuration:
- QDEC Module: TC0
- Mode: Position
- Position Output: 32-bit signed
- Index Output: 16-bit signed (revolution counter)
- Reset Mode: on each IDX signal
- IDX Pin: Enabled, rising edge
// Quadrature mode provides 4ร resolution Counts_per_rev = 1024 * 4 = 4096; // Absolute position calculation Position_within_rev = Position_Output; // 0 to 4095 Total_revolutions = Index_Counter; Absolute_position = (Total_revolutions * 4096) + Position_within_rev; // Convert to angle Angle_deg = (Position_within_rev / 4096) * 360; Angle_rad = (Position_within_rev / 4096) * 2 * pi; Tip: Use 32-bit signed position counter for bidirectional motion. The revolution counter (index output) tracks full rotations in both directions.
Example 2: Velocity Estimation Using Position Difference
Application: Motor Speed Control
Configuration:
- Mode: Position
- Position Output: 32-bit signed
- Sample Rate: 1 kHz
// In Simulink: Position โ [z^-1] โ Subtract โ Speed (counts/sample)// โโโโโโโโโโโโโโโโโโโโโโ// Convert to RPM (4096 counts/rev, 1 kHz sample) Speed_counts_per_sample = Position_current - Position_previous; Speed_RPM = Speed_counts_per_sample * (1000 Hz) * (60 s/min) / (4096 counts/rev); Speed_RPM = Speed_counts_per_sample * 14.65; // Low-pass filter for noise reduction Speed_filtered = 0.9 * Speed_filtered_prev + 0.1 * Speed_RPM;
Example 3: Position-Triggered ADC Sampling
Application: Synchronous Motor Current Sampling
Goal: Trigger ADC at specific rotor positions
Configuration:
- Position Output: 16-bit unsigned
- Reset Mode: at user defined value (e.g., 4095 for modulo)
- Use comparison match events (via TC compare registers)
// Set TC compare value for trigger position// Example: Trigger at 60ยฐ electrical (for 6-step commutation) Electrical_angle_deg = 60; Pole_pairs = 4; Mechanical_counts = (Electrical_angle_deg / 360) * (4096 / Pole_pairs); TC0->COUNT16.CC[0].reg = Mechanical_counts; // Enable event output on compare match TC0->COUNT16.EVCTRL.bit.MCEO0 = 1; // Match/Compare Event Output// Connect to ADC via EVSYS// ADC trigger = TC0 Match Compare 0
Example 4: Speed Measurement Mode
Application: Low-Speed Motor Velocity Feedback
Configuration:
- Mode: Speed
- Speed Output: 32-bit unsigned
- Speed Time Base: Resol: 125 ยตs - MaxPer: 8.19 s
// Speed output is time-based (period measurement)// Time = (Speed_Output) * Time_Base_Resolution Time_between_counts = Speed_Output * 125e-6; // seconds// Convert to frequency Frequency_Hz = 1 / Time_between_counts; // Convert to RPM (for 4096 counts/rev encoder) RPM = (Frequency_Hz * 60) / 4096; // Example: Speed_Output = 20000// Time = 20000 * 125ยตs = 2.5 seconds per count// Freq = 1/2.5 = 0.4 Hz// RPM = (0.4 * 60) / 4096 = 0.0058 RPM (very slow!)Speed Mode Best For: Very low speeds where position-based velocity calculation has poor resolution. At high speeds, position difference method is more accurate.
Error Detection and Handling
Quadrature Error Detection
The QDEC monitors phase transitions for validity. Invalid transitions indicate:
Valid Quadrature Sequence: 00 โ 01 โ 11 โ 10 โ 00 (CCW) 00 โ 10 โ 11 โ 01 โ 00 (CW) Invalid Transitions (Quadrature Error): 00 โ 11 (both changed simultaneously) 01 โ 10 (both changed simultaneously)
Quadrature Error Causes:
- Encoder mechanical misalignment
- Electrical noise causing glitches
- Loose encoder connections
- Encoder speed exceeds maximum input frequency
Solution: Enable digital filter (Max Period Filter) and/or enable Auto Correction to automatically fix minor phase errors.
Missing Pulse Error
Detects when expected encoder transitions don’t occur within a time window:
Direction Change Detection
Troubleshooting
Common Issues
Issue: Position Counter Drifts
Symptoms: Position slowly increases/decreases at standstill
Causes & Solutions:
- Electrical noise โ Enable digital filter
- Mechanical vibration โ Increase filter period
- Encoder alignment โ Check 90ยฐ phase relationship
- Enable Auto Correction for minor phase errors
Issue: Index Counter Not Incrementing
Check:
- IDX pin configuration (not “not used”)
- Index pulse polarity (Invert IDX if needed)
- Index pulse width (must be wider than input filter period)
- Physical encoder rotation through index position
Issue: Quadrature Error Flag Always Set
Solutions:
- Enable Auto Correction (handles minor glitches)
- Increase digital filter period (filters noise)
- Check PHA/PHB wiring (may need to swap or invert)
- Verify encoder signals have 90ยฐ phase shift (use oscilloscope)
// Maximum encoder frequency depends on MCU clock and filter// SAM E54 @ 120 MHz: MCU_Clock = 120e6; Filter_Period = 1e-6; // 1 ยตs filter Max_Encoder_Freq = 1 / (Filter_Period * 4); // 4 edges in quadrature Max_Encoder_Freq = 250 kHz; // For 4096 counts/rev encoder: Max_RPM = (250e3 * 60) / 4096 = 3662 RPM; // Disable filter for higher speeds (if noise is low)// Filter = disabled โ Max freq limited only by TC clock
- MCHP_QEI
- Quadrature Encoder Interface for dsPIC/PIC32
- MCHP_PDEC
- Position Decoder for SAM E5x/E7x (Hall sensors, stepper)
- MCHP_TC - Timer/Counter block (underlying peripheral used by QDEC)
- MCHP_EVSYS - Event System (for position-triggered events)
References
- [SAM E54 Family Datasheet (DS60001507)] - Timer/Counter (TC) peripheral section
- [SAM E70/S70/V71 Family Datasheet (DS60001527)] - TC in QDEC mode
- [SAM C21 Family Datasheet (DS60001479)] - TC quadrature decoder
- Atmel Application Note: Using TC for Quadrature Decoding (search [Microchip Documentation Portal])
- [SAME54 Xplained Pro User Guide (70005321)] - Hardware connections for encoder
Last Updated: 2024 | MCHP Blockset for MATLAB/Simulink
See Also
Datasheets
Other