请教STM32F407 CAN2进不了接收中断问题

2019-08-17 06:07发布

如题,我的CAN1和CAN2用的相同方式配置,CAN1接收、发送没问题,CAN2只能发送,进不了接收中断,CAN2接收使用的是FIFO0,请教是什么问题?
void CAN_HardwareInit(u8 canChan,float bps,u8 mode,u8 rxIntType)
{
GPIO_InitTypeDef GPIO_InitStructure; 
CAN_InitTypeDef  CAN_InitStructure;
CAN_FilterInitTypeDef  CAN_FilterInitStructure;
u8 i = 0;
switch (canChan)
{
case CAN1_CHANNEL:
#if CAN1_USE_GPIOA_EN
//使能相关时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能PORTA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//使能CAN1时钟
//初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11| GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化PA11,PA12
//引脚复用映射配置
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_CAN1); //GPIOA11复用为CAN1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_CAN1); //GPIOA12复用为CAN1
#elif CAN1_USE_GPIOB_EN
//使能相关时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能PORTB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//使能CAN1时钟
//初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化PB8,PB9
//引脚复用映射配置
GPIO_PinAFConfig(GPIOB,GPIO_PinSource8,GPIO_AF_CAN1); //GPIOB8复用为CAN1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource9,GPIO_AF_CAN1); //GPIOB9复用为CAN1
#elif CAN1_USE_GPIOD_EN
//使能相关时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);//使能PORTD时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//使能CAN1时钟
//初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化PD0,PD1
//引脚复用映射配置
GPIO_PinAFConfig(GPIOD,GPIO_PinSource0,GPIO_AF_CAN1); //GPIOD0复用为CAN1
GPIO_PinAFConfig(GPIOD,GPIO_PinSource1,GPIO_AF_CAN1); //GPIOD1复用为CAN1
#endif
for (i = 0;i < MAX_BPS_SIZE;i++)
{
if (CANBprInit.bps == bps)
{
CAN_InitStructure.CAN_TTCM=DISABLE; //非时间触发通信模式   
CAN_InitStructure.CAN_ABOM=DISABLE; //软件自动离线管理  
CAN_InitStructure.CAN_AWUM=ENABLE;//睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
CAN_InitStructure.CAN_NART=DISABLE; //禁止报文自动传送 
CAN_InitStructure.CAN_RFLM=DISABLE; //报文不锁定,新的覆盖旧的  
CAN_InitStructure.CAN_TXFP=DISABLE; //优先级由报文标识符决定 
CAN_InitStructure.CAN_Mode = mode; //模式设置 
CAN_InitStructure.CAN_SJW = CANBprInit.tsjw; //重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位 CAN_SJW_1tq~CAN_SJW_4tq
CAN_InitStructure.CAN_BS1 = CANBprInit.tbs1; //Tbs1范围CAN_BS1_1tq ~CAN_BS1_16tq
CAN_InitStructure.CAN_BS2 = CANBprInit.tbs2;//Tbs2范围CAN_BS2_1tq ~ CAN_BS2_8tq
CAN_InitStructure.CAN_Prescaler = CANBprInit.brp;  //分频系数(Fdiv)为brp+1
i = MAX_BPS_SIZE;
}
}
//配置过滤器
if (rxIntType & CAN1_RX0_INT_ENABLE)
{
CAN_FilterInitStructure.CAN_FilterNumber = 0;  //过滤器0
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; 
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; //32位 
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;////32位ID
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;//32位MASK
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;//过滤器0关联到FIFO0
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //激活过滤器0
CAN_Mode_Init(CAN1,&CAN_InitStructure,&CAN_FilterInitStructure,rxIntType);
}
if (rxIntType & CAN1_RX1_INT_ENABLE)
{
CAN_FilterInitStructure.CAN_FilterNumber = 1;  //过滤器0
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; 
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; //32位 
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;////32位ID
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;//32位MASK
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO1;//过滤器0关联到FIFO0
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //激活过滤器0
CAN_Mode_Init(CAN1,&CAN_InitStructure,&CAN_FilterInitStructure,rxIntType);
}
break;
case CAN2_CHANNEL:
//使能相关时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能PORTB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);//使能CAN2时钟
//初始化GPIO
#if CAN2_USE_GPIOB5_6_EN
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
#elif CAN2_USE_GPIOB12_13_EN
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
#endif
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化PB12,PB13或PB5、PB6
//引脚复用映射配置
#if CAN2_USE_GPIOB5_6_EN
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_CAN2); //GPIOB5复用为CAN2
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_CAN2); //GPIOB6复用为CAN2
#elif CAN2_USE_GPIOB12_13_EN
GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_CAN2); //GPIOB12复用为CAN2
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_CAN2); //GPIOB13复用为CAN2
#endif
for (i = 0;i < MAX_BPS_SIZE;i++)
{
if (CANBprInit.bps == bps)
{
CAN_InitStructure.CAN_TTCM=DISABLE; //非时间触发通信模式   
CAN_InitStructure.CAN_ABOM=DISABLE; //软件自动离线管理  
CAN_InitStructure.CAN_AWUM= ENABLE;//睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
CAN_InitStructure.CAN_NART=DISABLE; //禁止报文自动传送 
CAN_InitStructure.CAN_RFLM=DISABLE; //报文不锁定,新的覆盖旧的  
CAN_InitStructure.CAN_TXFP=DISABLE; //优先级由报文标识符决定 
CAN_InitStructure.CAN_Mode = mode; //模式设置 
CAN_InitStructure.CAN_SJW = CANBprInit.tsjw; //重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位 CAN_SJW_1tq~CAN_SJW_4tq
CAN_InitStructure.CAN_BS1 = CANBprInit.tbs1; //Tbs1范围CAN_BS1_1tq ~CAN_BS1_16tq
CAN_InitStructure.CAN_BS2 = CANBprInit.tbs2;//Tbs2范围CAN_BS2_1tq ~ CAN_BS2_8tq
CAN_InitStructure.CAN_Prescaler = CANBprInit.brp;  //分频系数(Fdiv)为brp+1
i = MAX_BPS_SIZE;
}
}
if (rxIntType & CAN2_RX0_INT_ENABLE)
{
CAN_FilterInitStructure.CAN_FilterNumber = 14;  //过滤器14
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; 
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; //32位 
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;////32位ID
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;//32位MASK
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;//过滤器2关联到FIFO0
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //激活过滤器1
CAN_Mode_Init(CAN2,&CAN_InitStructure,&CAN_FilterInitStructure,rxIntType);
}
if (rxIntType & CAN2_RX1_INT_ENABLE)
{
CAN_FilterInitStructure.CAN_FilterNumber = 3;  //过滤器2
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; 
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; //32位 
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;////32位ID
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;//32位MASK
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO1;//过滤器2关联到FIFO0
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //激活过滤器1
CAN_Mode_Init(CAN2,&CAN_InitStructure,&CAN_FilterInitStructure,rxIntType);
}
break;
default:
break;
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
5条回答
正点原子
1楼-- · 2019-08-17 09:43
 精彩回答 2  元偷偷看……
tjb83981242
2楼-- · 2019-08-17 13:32
很经典的问题,怎么觉得的楼主 ,原子哥 如何解决 我也是这样设置的 can1 好的  can2 就是不行
Morgan
3楼-- · 2019-08-17 14:55
除了帮顶....有没别的答案
合肥-文盲
4楼-- · 2019-08-17 18:10
我也是的,CAN1收发正常,CAN只能发不能收,CAN2和CAN1同样配置
合肥-文盲
5楼-- · 2019-08-17 21:08
我也是的,CAN1收发正常,CAN只能发不能收,CAN2和CAN1同样配置

一周热门 更多>