STM32里的CAN 支持2.0A,2.0B, 带有FIFO,中断等, 这里主要提一下内部的时钟应用.
bxCAN挂接在APB1总线上,采用总线时钟,所以我们需要知道APB1的总线时钟是多少. 我们先看看下图,看看APB1总线时钟:
APB1时钟取自AHB的分频, 而AHB又取自系统时钟的分频, 系统时钟可选HSI,HSE, PLLCLK, 这个在例程的RC设置里都有的,
总线时钟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的速率了
上一篇:stm32HAL库之can操作
下一篇:STM32 CAN总线通信学习笔记(一)
推荐阅读
史海拾趣
本人大四,技术并不是特别好,大赛的时候关注过咱论坛,感觉有些高职院校的朋友,技术功底很扎实, 那究竟什么学历,最适合搞单片机呢? 坛子里的人,又都是什么学历?… 查看全部问答∨ |
1-8-3.全桥式变压器开关电源 全桥式变压器开关电源也属于双激式变压器开关电源。它同时具有推挽式变压器开关电源电压利用率高,又具有半桥式变压器开关电源耐压高的特点。因此,全桥式变压器开关电源经常用于工作电压高,输出大功率大的场合。 1 ...… 查看全部问答∨ |
这年头,找个有眼光的老板真难 大公司,不思进取,ceo完成任务就ok,至于未来,关他鸟事; 小公司,没有资本,更没有胆量,得过且过。 结论--自己干最爽,可惜没钱,还欠地主家一屁股债。 只好跟着这些笨蛋老板混日子,公司不是家,能 ...… 查看全部问答∨ |
|
大家好,半路出家,基础问题询问。 由于驱动调试不便,所以使用应用程序直接编写。需要使用到中断: 首先初始化IO,使得某个IO脚为中断输入脚。 然后建立EVENT, 通过KernelIoControl申请中断,InterruptInitialize创建 IRQ 与事件的关联, 建 ...… 查看全部问答∨ |
安装ADOCE问题,Microsoft Windows Platform SDK for Pocket哪里有下载? 安装ADOCE时候,前提之一是安装 -Microsoft Windows Platform SDK for Pocket, 他和Microsoft Pocket PC 2003 SDK不一样吗?要在哪里能下载?… 查看全部问答∨ |