Mcp2515 Baud Rate Calculator

MCP2515 CAN Baud Rate Calculator

Configuration Parameters

Common: 8, 16, or 20 MHz
Range: 0 to 63 (Actual divisor is 2*(BRP+1))
1 TQ 2 TQ 3 TQ 4 TQ 5 TQ 6 TQ 7 TQ 8 TQ
1 TQ 2 TQ 3 TQ 4 TQ 5 TQ 6 TQ 7 TQ 8 TQ
2 TQ 3 TQ 4 TQ 5 TQ 6 TQ 7 TQ 8 TQ
1 TQ 2 TQ 3 TQ 4 TQ

Calculation Results

Calculated Baud Rate
500,000 bps
(500 kbps)
Time Quantum (TQ)
125 ns
Total TQ / Bit
16 TQ
Sample Point
75%
Status
Valid

Register Values (Hex)

CNF1: 0x00
CNF2: 0x90
CNF3: 0x02

Understanding MCP2515 CAN Bus Timing

The Microchip MCP2515 is a standalone CAN controller used to provide CAN bus communication to microcontrollers like Arduino or ESP32. Setting the correct baud rate is critical because every node on a CAN network must operate at the exact same bit timing to ensure message integrity and prevent bus errors.

Key Timing Components

  • Fosc: This is the frequency of the crystal or clock source connected to the MCP2515. Typical values are 8MHz and 16MHz.
  • Time Quantum (TQ): The basic unit of time for the CAN controller. It is derived from the oscillator and the Baud Rate Prescaler (BRP).
  • Bit Segments: A single CAN bit is divided into four segments:
    • Sync Segment: Always 1 TQ. Used to synchronize nodes on the bus.
    • Propagation Segment (PropSeg): Compensates for physical delays in the network.
    • Phase Segment 1 (PS1): Compensates for edge phase errors.
    • Phase Segment 2 (PS2): Used to adjust the sample point.

How the Calculation Works

The total number of Time Quanta per bit is the sum: 1 (Sync) + PropSeg + PS1 + PS2. The MCP2515 requires this sum to be between 8 and 25 TQ for reliable operation.

The sample point is the moment when the CAN controller reads the state of the bus. It is calculated as: (1 + PropSeg + PS1) / Total TQ. For high-speed CAN (ISO 11898), a sample point around 75% to 87.5% is typically recommended.

Register Descriptions

Register Function
CNF1 Controls the Synchronization Jump Width (SJW) and the Baud Rate Prescaler (BRP).
CNF2 Configures the Propagation Segment, PS1, and determines if PS2 is programmable.
CNF3 Defines Phase Segment 2 and Wake-up filters.

Example: Setting 500kbps with 16MHz Crystal

To achieve 500kbps with a 16MHz oscillator:

  1. Set BRP to 0. TQ = 2 * (0 + 1) / 16,000,000 = 125ns.
  2. Set Total TQ per bit to 16. (16 * 125ns = 2000ns = 1/500,000).
  3. Distribute 16 TQ: Sync=1, PropSeg=7, PS1=4, PS2=4.
  4. This results in a sample point of 75%.
function calculateCAN() { // Get Input Values var foscMHz = parseFloat(document.getElementById("fosc").value); var brpVal = parseInt(document.getElementById("brp").value); var pSeg = parseInt(document.getElementById("propseg").value); var ph1 = parseInt(document.getElementById("ps1").value); var ph2 = parseInt(document.getElementById("ps2").value); var sjw = parseInt(document.getElementById("sjw").value); // Validation if (isNaN(foscMHz) || isNaN(brpVal) || foscMHz <= 0) { alert("Please enter valid numeric values."); return; } // TQ Calculation // TQ = 2 * (BRP + 1) / Fosc var tqSec = (2 * (brpVal + 1)) / (foscMHz * 1000000); var tqNs = tqSec * 1000000000; // Total TQ per bit // BitTime = SyncSeg(1) + PropSeg + Phase1 + Phase2 var totalTq = 1 + pSeg + ph1 + ph2; // Baud Rate Calculation var baudRate = 1 / (tqSec * totalTq); var kbps = baudRate / 1000; // Sample Point var samplePoint = ((1 + pSeg + ph1) / totalTq) * 100; // Update UI Results document.getElementById("res_baud").innerText = Math.round(baudRate).toLocaleString() + " bps"; document.getElementById("res_kbps").innerText = "(" + kbps.toFixed(2) + " kbps)"; document.getElementById("res_tq").innerText = tqNs.toFixed(2) + " ns"; document.getElementById("res_total_tq").innerText = totalTq + " TQ"; document.getElementById("res_sample").innerText = samplePoint.toFixed(1) + "%"; // Status logic var statusEl = document.getElementById("res_status"); if (totalTq 25) { statusEl.innerText = "Invalid TQ"; statusEl.style.color = "#dc3545"; } else if (ph2 < 2) { statusEl.innerText = "PS2 Too Low"; statusEl.style.color = "#dc3545"; } else { statusEl.innerText = "Valid"; statusEl.style.color = "#28a745"; } // Register Values Calculation // CNF1: [SJW1][SJW0][BRP5][BRP4][BRP3][BRP2][BRP1][BRP0] var cnf1 = ((sjw – 1) << 6) | (brpVal & 0x3F); // CNF2: [BTLMODE][SAM][PHSEG1.2][PHSEG1.1][PHSEG1.0][PRSEG2][PRSEG1][PRSEG0] // BTLMODE = 1 (Phase Segment 2 is determined by CNF3) // SAM = 0 (Bus sampled once) var cnf2 = (1 << 7) | ((ph1 – 1) << 3) | (pSeg – 1); // CNF3: [SOF][WAKFIL][0][0][0][PHSEG2.2][PHSEG2.1][PHSEG2.0] var cnf3 = (ph2 – 1); document.getElementById("reg_cnf1").innerText = "0x" + cnf1.toString(16).toUpperCase().padStart(2, '0'); document.getElementById("reg_cnf2").innerText = "0x" + cnf2.toString(16).toUpperCase().padStart(2, '0'); document.getElementById("reg_cnf3").innerText = "0x" + cnf3.toString(16).toUpperCase().padStart(2, '0'); } // Run initial calculation on load window.onload = function() { calculateCAN(); };

Leave a Comment