分类: C/C++
2014-02-24 22:02:57
原文地址:stm32f4 can 解析 作者:M甲壳虫M
今天做CAN通信 使用扩展帧 19位扩展帧EXTID是正确的 但是11位标准帧为什么为0 求救 /* CAN cell init */ CAN_InitStructure.CAN_TTCM=ENABLE; //时间触发 CAN_InitStructure.CAN_ABOM=ENABLE; //自动离线管理 CAN_InitStructure.CAN_AWUM=ENABLE; //自动唤醒 CAN_InitStructure.CAN_NART=DISABLE; //自动重传(注意,DISABLE才是自动重传) CAN_InitStructure.CAN_RFLM=DISABLE; //失能FIFO锁定模式 CAN_InitStructure.CAN_TXFP=ENABLE; //失能FIFO优先级 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; //36Mhz/16/9 = 250Kbits CAN_Init(CAN1,&CAN_InitStructure); /* CAN 过滤器设置 */ CAN_FilterInitStructure.CAN_FilterNumber=0; //过滤器编号(0-13) CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //指定了过滤器将被初始化到的模式为标识符列表模式 CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //给出了过滤器位宽1个32位过滤器 CAN_FilterInitStructure.CAN_FilterIdHigh=(u16)(DM1 >> (29-16));//用来设定过滤器标识符(32位位宽时为其高段位,16位位宽时为第一个) CAN_FilterInitStructure.CAN_FilterIdLow=(u16)((DM1 << 3)|0x04);//用来设定过滤器标识符(32位位宽时为其低段位,16位位宽时为第二个 CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0;//用来设定过滤器屏蔽标识符或者过滤器标识符(32位位宽时为其高段位,16位位宽时为第一个 CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x04;//用来设定过滤器屏蔽标识符或者过滤器标识符(32位位宽时为其低段位,16位位宽时为第二个 CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0; //设定了指向过滤器的FIFO0 CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//使能过滤器 CAN_FilterInit(&CAN_FilterInitStructure); /* 允许FMP0中断*/ CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE); void SetMassage(void) { CanTxMsg TxMessage; TxMessage.StdId = (DM1 >> 29-11);//右移 TxMessage.ExtId = DM1&0x3FFFF; TxMessage.RTR = CAN_RTR_DATA;//数据帧 TxMessage.IDE = CAN_ID_EXT; //扩展标识符 TxMessage.DLC = 2; TxMessage.Data[0] = 0xCA; TxMessage.Data[1] = 0xFE; CAN_Transmit(CAN1,&TxMessage); void USB_LP_CAN1_RX0_IRQHandler(void) { CanRxMsg RxMessage; CPU_SR cpu_sr; OS_ENTER_CRITICAL(); /* Tell uC/OS-II that we are starting an ISR */ OSIntNesting++; OS_EXIT_CRITICAL(); RxMessage.ExtId = 0; RxMessage.StdId = 0; CAN_Receive(CAN1,CAN_FIFO0, &RxMessage); printf("%d\n\r",RxMessage.StdId); printf("%d\n\r",RxMessage.ExtId); if(RxMessage.ExtId == (DM1&0x3FFFF) ) { printf("%d\n\r",RxMessage.Data[0]); printf("%d\n\r",RxMessage.Data[1]); } OSIntExit(); } |