“如果你用的CAN引脚是PA11和PA12,接收中断用CAN1_RX0_IRQn。如果CAN引脚用的是PB8和PB9,也就是用重定义的引脚,接收中断用CAN1_RX1_IRQn。由于PA11和PA12也是USB的引脚,所以非互联型且带CAN控制器的微控制器的库文件在起名字时用了USB_LP_CAN1_RX0_IRQn。”具体的定义可以参考stm32F10x.h文件,其中对不同型号的中断有具体的定义。
示例如下:
1. 配置过滤器
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; //关联FIFO0
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
CAN_FilterInitStructure.CAN_FilterNumber = 14;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO1; //关联FIFO1
CAN_FilterInit(&CAN_FilterInitStructure);
2. 开启CAN中断
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
CAN_ITConfig(CAN1, CAN_IT_FMP1, ENABLE);
CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
CAN_ITConfig(CAN2, CAN_IT_FMP1, ENABLE);
3. 中断处理函数
void CAN1_RX0_IRQHandler(void)
{
CAN_Receive(CAN1, CAN_FIFO0, &CAN1_RxMsg);
//
}
void CAN1_RX1_IRQHandler(void)
{
CAN_Receive(CAN1, CAN_FIFO1, &CAN1_RxMsg);
//
}
void CAN2_RX0_IRQHandler(void)
{
CAN_Receive(CAN2, CAN_FIFO0, &CAN2_RxMsg);
//
}
void CAN2_RX1_IRQHandler(void)
{
CAN_Receive(CAN2, CAN_FIFO1, &CAN2_RxMsg);
//
}