can baud rate calculation

Simple calculation of CAN baud rate

Suppose we do not consider the SJW bit in BTR0 and the SAM bit in BTR1 first. Then, BTR0 and BTR1 are two frequency division coefficient registers; their product is an extended frequency division coefficient. which is:

BTR0 & TImes; BTR1 = F_BASE / Fbps (1)

Among them: the internal frequency reference source F_BASE = Fclk / 2, that is, the external crystal frequency Fclk divided by 2. Note that in any application, when an external crystal is used as the reference source, it is first shaped by frequency divider 2.

In the formula, when the crystal oscillator is 16M, F_BASE = 8000K; when the crystal oscillator is 12M, F_BASE = 6000K, Fbps is the CAN bus frequency we want to get. The unit is K.

In the formula, BTR0 = m, BTR1 = n, external crystal oscillator 16M, then: m • n = 8000 / Fbps

In this way, when Fbps takes the value we want, we will get a combined value of m * n. When n is selected, the value of m is also unique. The n-value CAN specification specifies 8 to 25. (That is, the value of BTR1) The basic principle is: the higher the Fbps value, the greater the value of n (by setting BTR1). The reason is not difficult to understand.

I assume that n = 10 is selected in general applications, that is: synchronization segment + phase buffer segment 1 + phase buffer segment 2 = 1 + 5 + 4, then (2) is simplified to m = 800 / Fbps; the maximum setting value of m Is 64, the maximum frequency division coefficient of SJA1000 m * n = 64x25 = 1600. So the standard algorithm usually takes 16M crystal as an example. In fact, with formula (1), any crystal value (6M ~ 24M) is easy to calculate.

Determination of SAM: When low frequency, select SAM = 1, that is, sample 3 times. When the high frequency is above 100K, take SAM = 0, that is, sample once. SJA resynchronization jump selection: related to digital phase-locked loop technology. When the value of n is selected large, SJA can be selected large, that is, multiple pulse shares Tscl can be corrected at one time. When n value is small or frequency is low, select SJA = 1. That is, both BTR0.7 and BTR0.6 are set to 0.

STM32 CAN baud rate calculation

CAN in STM32 supports 2.0A, 2.0B, with FIFO, interrupts, etc.Here we mainly mention the internal clock application.

bxCAN is connected to the APB1 bus and uses the bus clock, so we need to know what the APB1 bus clock is. Let ’s take a look at the following figure to see the APB1 bus clock:

The APB1 clock is taken from the AHB frequency division, and the AHB is taken from the system clock frequency division.

Then look at how to calculate the CAN bus rate after having the APB1 clock, first look at the following figure:

With the above picture, it is basically clear.

Bus clock MHz (3 + TS1 + TS2) * (BRP + 1)

================================================== =

Here is my calculation:

CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;

Note // # define CAN_BS1_3tq ((uint8_t) 0x02) / *! <3 TIme quantum * /

CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;

CAN_InitStructure.CAN_Prescaler = 4; // 2

nominal bit TIme (3 + 5 + 1) tq = 9tq

About the frequency division coefficient, see the following under system_stm32f10x.c

staTIc void SetSysClockTo72 (void) function

/ * HCLK = SYSCLK * /

/ * PCLK2 = HCLK * /

/ * PCLK1 = HCLK / 2 * /

So can clock 72MHZ / 2/4 = 9 Mhz

tq = 1 / 36Mhz

Baud rate is 1 / nominal bit time = 9/9 = 1MHZ

=============================================

-----------------------------------------------

================================================== ==

void CAN_Configuration (void)

{

CAN_InitTypeDef CAN_InitStructure;

CAN_FilterInitTypeDef CAN_FilterInitStructure;

/ * CAN register init * /

CAN_DeInit ();

CAN_StructInit (& CAN_InitStructure);

/ * CAN cell init * /

CAN_InitStructure.CAN_TTCM = DISABLE;

CAN_InitStructure.CAN_ABOM = DISABLE;

CAN_InitStructure.CAN_AWUM = DISABLE;

CAN_InitStructure.CAN_NART = DISABLE;

CAN_InitStructure.CAN_RFLM = DISABLE;

CAN_InitStructure.CAN_TXFP = DISABLE;

CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;

CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;

CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;

CAN_InitStructure.CAN_Prescaler = 200;

CAN_Init (& CAN_InitStructure);

/ * CAN filter init * /

CAN_FilterInitStructure.CAN_FilterNumber = 0;

CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;

CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;

CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;

CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;

CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;

CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;

CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;

CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;

CAN_FilterInit (& CAN_FilterInitStructure);

}

Note // # define CAN_BS1_3tq ((uint8_t) 0x02) / *! <3 time quantum * /

The dialing rate is 10K, the formula: 72MHZ / 2/200 / (1 + 9 + 8) = 0.01, that is, 10K, and passed the SJA1000 test

================================================

120 ohm resistor should be added !!!

CAN-> BTR = (u32) ((u32) CAN_InitStruct-> CAN_Mode << 30) | ((u32) CAN_InitStruct-> CAN_SJW << 24) |

((u32) CAN_InitStruct-> CAN_BS1 << 16) | ((u32) CAN_InitStruct-> CAN_BS2 << 20) |

((u32) CAN_InitStruct-> CAN_Prescaler-1);

Dog Fence

Dog Fence,Wire Fence,Petsafe Fence,Best Wireless Dog Fence

Elite-tek Electronics Ltd , https://www.aetertek.ca