STM32里的CAN 支撑2.0A,2.0B, 带有FIFO,中止等, 这儿首要提一下内部的时钟运用.
bxCAN挂接在APB1总线上,选用总线时钟,所以咱们需求知道APB1的总线时钟是多少. 咱们先看看下图,看看APB1总线时钟:
APB1时钟取自AHB的分频, 而AHB又取自体系时钟的分频, 体系时钟可选HSI,HSE, PLLCLK, 这个在例程的RC设置里都有的,
然后再看看有了APB1的时钟后,怎么算CAN的总线速率, 先看下图:
有了上边的这个图,基本就清楚了.
总线时钟MHz (3+TS1+TS2)*(BRP+1)
===================================================
下面是我的核算:
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
留意//#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
关于分频系数 检查 system_stm32f10x.c下面的
static void SetSysClockTo72(void) 函数
/* HCLK = SYSCLK */
/* PCLK2 = HCLK */
/* PCLK1 = HCLK/2 */
所以can时钟 72MHZ/2/4=9 Mhz
tq=1/36Mhz
波特率为 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);
}
留意//#define CAN_BS1_3tq ((uint8_t)0x02) /*!< 3 time quantum */
拨特率10K,公式:72MHZ/2/200/(1+9+8)=0.01,即10K,和SJA1000测试通过
================================================
120欧姆电阻要加上!!!
哦 确实是
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);
总结一下
Fpclk=36M 时 can波特率为250k 的装备为
/* 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_LoopBack;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=9;
CAN_Init(&CAN_InitStructure); 250k
======================================
的:将can总线波特率设置为250k
在官方的can例程上 给出了100k 查询 和500k 中止方法的比如 别离设置如下:
CAN_Polling:
/* 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_LoopBack;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=5;
CAN_Init(&CAN_InitStructure); 100k
/* CAN cell init */ CAN_Interrupt
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_LoopBack;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=1;
CAN_Init(&CAN_InitStructure); //500k
can时钟是RCC_APB1PeriphClock,你要留意CAN时钟频率
CAN波特率 = RCC_APB1PeriphClock/CAN_SJW+CAN_BS1+CAN_BS2/CAN_Prescaler;
假如CAN时钟为8M, CAN_SJW = 1,CAN_BS1 = 8,CAN_BS2 = 7,CAN_Prescaler = 2
那么波特率便是=8M/(1+8+7)/2=250K
=========================================
得到500Kb/s的波特率
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq; 每一位的Tq数目 = 1 (固定SYNC_SEG) + 8 (BS1) + 7 (BS2) = 16 假如CAN时钟是 8 MHz : (8M / 1 ) / 16 = 500K 其间: 为了设置为 100K, 把分频系数改为5即可, BS1 BS2 不变 每一位的Tq数目 = 1 (固定) + 8 (BS1) + 7 (BS2) = 16 假如想得到 1M 的波特率, CAN时钟仍然是 8 MHz的情况下, 分频系数不变 应该改动 BS1 BS2 CAN_InitStructure.CAN_BS1=CAN_BS1_5tq; 每一位的Tq数目 = 1 (固定) + 5 (BS1) + 2 (BS2) = 8 别的尽可能的把采样点设置为 CiA 引荐的值: 75% when 波特率 > 800K CAN_InitStructure.CAN_Prescaler=5; (1+13) / (1+13+2) = 87.5% 所以关于 500K 的波特率(假定运用 8MHz 时钟) CAN_InitStructure.CAN_Prescaler=1; (1+13) / (1+13+2) = 87.5% 所以关于 1000K 的波特率(假定运用 8MHz 时钟) CAN_InitStructure.CAN_Prescaler=1; (1+5) / (1+5+2) = 75% 个人见解, 仅供参考。 |
上边这个公式算出来的便是CAN的速率了.