完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
芯片:STM32 F103E芯片,一个电脑上安装周立功CAN卡,总线上连接12个设备CAN。调试时出现总线冲突。大家帮忙看看代码是否有问题。
int InitFilterCan(u8 canAddr, u32 filterMaskCode) { CAN_FilterInitTypeDef CAN_FilterStructure; CAN_ITConfig((&CANDevList[canAddr - 1])->ADDR, CAN_IT_FMP0, DISABLE); CAN_FilterStructure.CAN_FilterNumber = 0; CAN_FilterStructure.CAN_FilterMode = CAN_FilterMode_IdMask; CAN_FilterStructure.CAN_FilterScale = CAN_FilterScale_32bit; CAN_FilterStructure.CAN_FilterIdHigh = (u16)(filterMaskCode& 0xE0)>>5; CAN_FilterStructure.CAN_FilterIdLow = (filterMaskCode << 11);//6.20 CAN_FilterStructure.CAN_FilterMaskIdHigh = 0x0007; CAN_FilterStructure.CAN_FilterMaskIdLow = (u16)((u16)0xFF << 11);//6.20 CAN_FilterStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; CAN_FilterStructure.CAN_FilterActivation = ENABLE; CAN_FilterInit(&CAN_FilterStructure); #define CAN_FILTER_2 #ifdef CAN_FILTER_2 CAN_FilterStructure.CAN_FilterNumber = 2; CAN_FilterStructure.CAN_FilterMode = CAN_FilterMode_IdMask; CAN_FilterStructure.CAN_FilterScale = CAN_FilterScale_32bit; CAN_FilterStructure.CAN_FilterIdHigh = 0x0007; CAN_FilterStructure.CAN_FilterIdLow = (u16)((u16)0x00FF << 11); CAN_FilterStructure.CAN_FilterMaskIdHigh = 0x0007; CAN_FilterStructure.CAN_FilterMaskIdLow = (u16)((u16)0x00FF << 11); CAN_FilterStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; CAN_FilterStructure.CAN_FilterActivation = ENABLE; CAN_FilterInit(&CAN_FilterStructure); #endif CAN_ITConfig((&CANDevList[canAddr - 1])->ADDR, CAN_IT_FMP0, ENABLE); return 1; } int InitCan(pCANDev pCanDevice, u32 filterMaskCode, u32 baudType) { GPIO_InitTypeDef GPIO_InitStructure; CAN_InitTypeDef CAN_InitStructure; //CAN_FilterInitTypeDef CAN_FilterStructure; NVIC_InitTypeDef NVIC_InitStructure; //CanTxMsg CAN_TxStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = pCanDevice->RxIRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pCanDevice->PreemptionPriority; NVIC_InitStructure.NVIC_IRQChannelSubPriority = pCanDevice->SubPriority; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = pCanDevice->TxIRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pCanDevice->PreemptionPriority; NVIC_InitStructure.NVIC_IRQChannelSubPriority = pCanDevice->SubPriority; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); BSP_IntVectSet(pCanDevice->IntId,pCanDevice->RxIRQHandler); APBxPeriphClockCmd(GPIO_BUS,pCanDevice->APBPeriphGpio , ENABLE);//³õʼ»¯CANʹÓõÄGPIOʱÖÓ APBxPeriphClockCmd(CAN_BUS,pCanDevice->APBPeriphCan, ENABLE);//³õʼ»¯CANʱÖÓ /* Configure CAN RX and TX pins */ GPIO_InitStructure.GPIO_Pin = pCanDevice->CAN_RX; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(pCanDevice->CPOIx, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = pCanDevice->CAN_TX; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(pCanDevice->CPOIx, &GPIO_InitStructure); /* CAN register init */ CAN_DeInit(pCanDevice->ADDR); /* CAN cell init */ CAN_InitStructure.CAN_TTCM = DISABLE; CAN_InitStructure.CAN_ABOM = ENABLE;//ENABLE;//; CAN_InitStructure.CAN_AWUM = ENABLE; 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_3tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq; CAN_InitStructure.CAN_Prescaler = 36000/baudType/6; //OK //36M/(36000/baudType/6)/(2+3+1) CAN_Init(pCanDevice->ADDR, &CAN_InitStructure); return 1; } int CanOpen(u8 canAddr, u32 filterMaskCode, u32 baudType) { if(canAddr == 1 ) { InitCan(&CANDevList[canAddr - 1], filterMaskCode, baudType); InitFilterCan(canAddr, filterMaskCode); } return 1; } uint32_t CreateExtId(u8 PF) { uint32_t extId = 0; extId |= PF; extId <<= 8; extId |= 0; extId <<= 8; extId |= _g_DC_Id; return extId; } CanTxMsg __createCanTxMsgHeader(u8 PF) { CanTxMsg canMsg; memset(&canMsg, 0, sizeof(CanTxMsg)); canMsg.StdId = 0; canMsg.ExtId = CreateExtId(PF); canMsg.IDE = CAN_ID_EXT; //À©Õ¹Ö¡ canMsg.RTR = CAN_RTR_DATA; //Êý¾ÝÖ¡ return canMsg; } int CanSend(u8 canAddr, CanTxMsg CAN_TxStructure) { u8 state; // CAN_Transmit(CANDevList[canAddr - 1].ADDR, &CAN_TxStructure); // return 1; u8 TransmitMailbox = 0; u32 t=0; TransmitMailbox = CAN_Transmit(CANDevList[canAddr - 1].ADDR, &CAN_TxStructure); /* µÈ´ý·¢ËÍÍê³É */ state = CAN_TransmitStatus(CANDevList[canAddr - 1].ADDR, TransmitMailbox); while((state != CANTXOK) && (t < 0xffff)) { if(state == CANTXFAILED) return _Can_Transmit_Fail; else if(state == CAN_NO_MB) return _Can_No_MailBox; t++; state = CAN_TransmitStatus(CANDevList[canAddr - 1].ADDR, TransmitMailbox); } if(t == 0XFFFF) return _Can_Transmit_timeout; return _Can_Success; } |
|
相关推荐
2个回答
|
|
12个设备同时上电,用示波器抓出来显示,似乎有电平叠加在一起了。高高低低,很乱,请高手帮忙解决!!!
|
|
|
|
正在学习中。。。。。。。。
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
使用stm32l451片子,对ad7606进行3通道100k采样值跳动问题?
1113 浏览 1 评论
STM32进行DMA数据转运,为什么只有A组最后一位数据被转运到B组去?
1602 浏览 2 评论
1902 浏览 3 评论
1492 浏览 0 评论
嵌入式开发 Win11安装ST-Link 提示 设备描述符请求失败 怎么处理?
1631 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-8-26 22:21 , Processed in 0.598271 second(s), Total 75, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191