stm32的can总线的配置如下:
can_initstructure.can_ttcm=disable;//禁止时间触发通信模式
can_initstructure.can_abom=disable;
can_initstructure.can_awum=disable;
can_initstructure.can_nart=disable;//can报文只被发送1次,不管发送的结果如何(成功、出错或仲裁丢失)
can_initstructure.can_rflm=disable;
can_initstructure.can_txfp=disable;
can_initstructure.can_mode=can_mode_normal;
//can_mode_loopback
//can_mode_normal
can_initstructure.can_sjw=can_sjw_1tq;
can_initstructure.can_bs1=can_bs1_5tq;//1--16
can_initstructure.can_bs2=can_bs2_2tq;//1--8
can_initstructure.can_prescaler=2;
can_init(&can_initstructure);
/* can filter init
*/
can_filterinitstructure.can_filternumber=0;//选择过滤器0
can_filterinitstructure.can_filtermode=can_filtermode_idmask;//指定过滤器被设置为标识符屏蔽模式
can_filterinitstructure.can_filterscale=can_filterscale_32bit;//给出过滤器位宽为32位
下面根据设置的参数不同来决定can总线can总线的配置情况:
1、对扩展数据帧进行过滤:(只接收扩展数据帧)
can_filterinitstructure.can_filteridhigh =
(((u32)slave_id<<3)&0xffff0000)>>16;
can_filterinitstructure.can_filteridlow =
(((u32)slave_id<<3)|can_id_ext|can_rtr_data)&0xffff;
can_filterinitstructure.can_filtermaskidhigh =
0xffff;
can_filterinitstructure.can_filtermaskidlow =
(注:标准帧数据帧、标准远程帧和扩展远程帧均被过滤)
2、对扩展远程帧过滤:(只接收扩展远程帧)
(((u32)slave_id<<3)|can_id_ext|can_rtr_remote)&0xffff;
can_filterinitstructure.can_filtermaskidlow = 0xffff;
3、对标准远程帧过滤:(只接收标准远程帧)
(((u32)slave_id<<21)&0xffff0000)>>16;
(((u32)slave_id<<21)|can_id_std|can_rtr_remote)&0xffff;
4、对标准数据帧过滤:(只接收标准数据帧)
(((u32)slave_id<<21)|can_id_std|can_rtr_data)&0xffff;
5、对扩展帧进行过滤:(扩展帧不会被过滤掉)
(((u32)slave_id<<3)|can_id_ext)&0xffff;
can_filterinitstructure.can_filtermaskidlow = 0xfffc;
6、对标准帧进行过滤:(标准帧不会被过滤掉)
(((u32)slave_id<<21)|can_id_std)&0xffff;
注:slave_id为要过滤的id号。