《STM32F103芯片CAN总线应用程序.docx》由会员分享,可在线阅读,更多相关《STM32F103芯片CAN总线应用程序.docx(5页珍藏版)》请在优知文库上搜索。
1、*Copyright(a)日期:修改记录:描述:STM32共有14组过滤器,用以对接收到的帧进展过滤。每组过滤器包括了2个可配置的32位存放器:CAN_FxR0和CAN_FxRl.对于过滤器组,可以将其配置成屏蔽位模式,这样CAN_FxR0中保存的就是标识符匹配值,CAN-FxRl中保存的是屏蔽码,即CAN_FxRl中如果某一位为1,那么CAN,FR0中相应的位必须与收到的帧的标志符中的相应位吻合才能通过过滤器;CAN-FxRl中为0的位表示CAN_FxR0中的相应位可不必与收到的帧进展匹配。过滤器组还可以被配置成标识符列表模式,此时CAN.FR0和CAN_FxRl中的都是要匹配的标识符,收到
2、的帧的标识符必须与其中的一个吻合才能通过过滤。例如:CAN-FiIterInitStructure.CAN-FiIterNumber=O;设置第一组过滤器CAN_FilterlnitStructure.CAN_FilterMode=CAN_FilterMode_ldMask;屏蔽位模式CAN_FilterlnitStructure.CAN_FilterScale=CAN_FilterScale_32bit;/32位CAN-FiIterInitStructure.CAN-FiIterIdHigh=(OXO6355);接收ID为635的消息11位帧ID标准帧因此左移5CAN_FilterlnitS
3、tructure.CAN_FilterldLow=OxOOOO;CAN_FilterlnitStructure.CAN_FilterMaskldHigh=Oxffff;/CAN-FiIterInitStructure.CAN-FiIterMaskIdLow=Oxffff;CAN_FilterlnitStructure.CAN_FilterFIFOAssignment=CAN_FIFOO;第一组过滤器指向FIFOOCAN-FiIterInitStructure.CAN-FiIterActivation=ENABLE;CAN_Filterlnit(&CAN_FilterlnitStructure)
4、;滤波器配置详细如下:1、对扩展数据帧进展过滤:(只接收扩展数据帧)CAN-FilterlnitStructure-CAN-FiIterIdHigh=(u32)slave_id3)&0xFFFF0000)16;CAN_FilterlnitStructure.CAN_FilterldLo=(u32)slave_id3)|CANJD_EXT|CAN_RTR_DA7A)&0xFFFF;CAN_FilterlnitStructure.CAN_FilterMaskldHigh=OxFFFF;CAN_FilterlnitStructure.CAN_FilterMaskldLow=OxFFFF;(注:标准帧
5、数据帧、标准远程帧和扩展远程帧均被过滤)2、对扩展远程帧过滤:(只接收扩展远程帧)CAN-FilterlnitStructure-CAN-FiIterIdHigh=(u32)slave_id3)&0xFFFF0000)16;CAN_FilterlnitStructure.CAN_FilterldLow=(u32)slave_id3)|CAN_ID_EXT|CAN_RTR_REMOTE)&OxFFFF;CAN_FilterlnitStructure.CAN_FilterMaskldHigh=OxFFFF;CAN_FilterlnitStructure.CAN_FilterMaskldLow=Ox
6、FFFF;3、对标准远程帧过滤:(只接收标准远程帧)CAN-FiIterInitStructure.CAN-FiIterIdHigh=(u32)slave_id21)&0xffff0000)16;CAN_FilterlnitStructure.CAN_FilterldLow=(u32)slavejd21)CAN_ID_STD|CAN_RTR_REMOTE)&Oxffff;CAN_FilterlnitStructure.CAN_FilterMaskldHigh=OxFFFF;CAN_FilterlnitStructure.CAN_FilterMaskldLow=OxFFFF;4、对标准数据帧过滤
7、:(只接收标准数据帧)CAN_FilterlnitStructure.CAN_FilterldHigh=(u32)slave_id21)&0xffff0000)16;CAN_FilterlnitStructure.CAN_FilterldLow=(u32)slavejd21)CAN_ID_STDCAN_RTR_DA7A)&0xffff;CAN_FilterlnitStructure.CAN_FilterMaskldHigh=OxFFFF;CAN_FilterlnitStructure.CAN_FilterMaskldLow=OxFFFF;5、对扩展帧进展过滤:(扩展帧不会被过滤掉)CAN-Fi
8、IterInitStrUCtUre.CAN-FiIterIdHigh=(u32)slave_id3)&0xFFFF0000)16;CAN_FilterlnitStructure.CAN_FilterldLow=(u32)slave_id3)|CAN_ID_EXT)&0xFFFF;CAN_FilterlnitStructure.CAN_FilterMaskldHigh=OxFFFF;CAN_FilterlnitStructure.CAN_FilterMaskldLow=OxFFFC;6、对标准帧进展过滤:(标准帧不会被过滤掉)CAN_FilterlnitStructure.CAN_Filterl
9、dHigh=(u32)slave_id21)&0xffff0000)16;CAN_FilterlnitStructure.CAN_FilterldLow=(u32)slavejd21)CAN_ID_STD)&Oxffff;CAN-FilteiInitStructure.CAN-FiIterMaskIdHigh=OxFFFF;CAN_FilterlnitStructure.CAN_FilterMaskldLow=OxFFFC;注:SlaVeJd为要过滤的id号。*/ftinclude1,CAN.hn/*/全局变量声明/引用处externvolatileunsignedcharCan_Flag;/
10、CAN发送数据完成flag标志/*描述:CAN初始化、CAN波特率=PCLKl/(BRP+l)*(TSl+TS2+3)*/voidlnit_CAN(void)(NVICJnitTypeDefNviCJnitStructure;GPIOJnitTypeDefGPIO_lnitStructure;CAN_lnitTypeDefCANJnitStructure;CAN_FilterlnitTypeDefCAN_FilterlnitStructure;RCjAPBlPeriPhClOCkCmd(RCJAPBlPeriph_CANl,ENABLE);/CAN时钟使能RCC,APB2PeriphClock
11、Cmd(RCC,APB2Peph,GPIOAzENABLE);/I/O口时钟使能*ConfigureCANpin:RXPAll*/GPIO_lnitStructure.GPIO_Pin=GPIO_Pin_ll;GPIO_lnitStructure.GPIO_Mode=GPIO_Mode_IPU;GPIO_lnit(GPIOA,&GPIO_lnitStructure);*ConfigureCANpin:TXPA12*/GPIO_lnitStructure.GPIO_Pin=GPIO_Pin_12;GPIO_lnitStructure.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_
12、lnit(GPIOA,&GPIO_lnitStructure);*CANregisterinit*/CAN-DeInit(CANl);CAN_Structlnit(&CAN_lnitStructure);*CANcellinit*/CANjnitstructureCANJTCM=DISABLE;制止时间触发通信模式CAN_lnitStructure.CAN_ABOM=DISABLE;CAN_lnitStructure.CAN_AWUM=DISABLE;CAN_lnitStructure.CAN_NART=DISABLE;CAN_lnitStructure.CAN_RFLM=DISABLE;CA
13、N_lnitStructure.CAN_TXFP=DISABLE;睡眠模式通过去除sleep位来唤醒 ENABLE;报文自动重传接收溢出时,FIFO未锁定发送的优先级由标示符的大小决定CAN_lnitStructure.CAN_Mode=CAN_Mode_Normal; /CAN_Mode_LoopBack;/CAN_lnitStructure.CAN_SJW=CAN_SJW_ltq;/SJW=O波特率计算,PCLKl=36MHZ,CAN 波特率=36MHZ/(2+l)*(15+6+3)=500KbSCAN_lnitStructure.CAN_BSl=CAN_BSl_16tq;/TS1=15波
14、特率计算,PCLK1=36MHZ,CAN 波特率=PCLKi/(BRP+l)*(TSl+TS2+3)CAN_lnitStructure.CAN_BS2=CAN_BS2_7tq; /TS2=6CAN_lnitStructure.CAN_Prescaler=3;/BRP=29/ 45=50KCAN_lnit(CANlz&CAN_lnitStructure);* CAN filter init */CAN_FilterlnitStructure.CANeFiIterNumber=O;过滤器CAN_FilterlnitStructure.CAN_FilterMode=CAN_FilterMode_ldMask; 模式.CAN_FilterlnitStructure.CAN_FilterScale=CAN_FilterScale_32bit;CAN_FilterlnitStructure.CAN_FilterldHigh=OxOOOO;CAN_FilterlnitStructure.CAN_FilterldLow=OxOOOO;CAN_Filt