这个是我的程序,采用CAN2口能发送数据,can分析接收的数据,如果用CAN分析仪向控制板发送数据,控制板的接收中断无法进入,这是怎么回事,和别人程序对照没有发现问题呢,请大神指导,
can的配置
如下void Init_CAN2(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//使能GPIOD的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE);
/* Configure CAN2 RX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN2 TX pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_CAN2, ENABLE);
//使能CAN时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2, ENABLE);
//CAN重置所有参数
CAN_DeInit(CAN2);
//CAN结构体重置
CAN_StructInit(&CAN_InitStructure);
//CAN 初始化
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_NART = DISABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;//不使用接收FIFO锁定功能,这样报文溢出时,新报文将覆盖最后接收到的旧报文
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
//CAN波特率 =1MBps CAN (在时钟30Mhz)
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;//重新同步跳跃宽度1个时间单位
CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq;//时间段1为8个时间单位
CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;//时间段2为7个时间单位
CAN_InitStructure.CAN_Prescaler =9;//(pclk1/((1+13+2)*9)) = 36Mhz/16/9 = 250Kbits设定了一个时间单位的长度9
CAN_Init(CAN2, &CAN_InitStructure);
//CAN2的过滤器初始化
CAN_FilterInitStructure.CAN_FilterNumber =14;
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;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN2_FilterInit(&CAN_FilterInitStructure);
//传输
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x01;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 1;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = CAN2_TX_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = CAN2_SCE_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*在操作系统下注册中断函数*/
// BSP_IntVectSet(BSP_INT_ID_CAN2_TX, BSP_IntHandlerCAN2_TX); /*Assign ISR handler.Interrupt for which vector will be set*/
// BSP_IntEn(BSP_INT_ID_CAN2_TX); /*Interrupt to enable.*/
// BSP_IntVectSet(BSP_INT_ID_CAN2_RX0, BSP_IntHandlerCAN2_RX0); /*Assign ISR handler.Interrupt for which vector will be set*/
// BSP_IntEn(BSP_INT_ID_CAN2_RX0); /*Interrupt to enable.*/
// BSP_IntVectSet(BSP_INT_ID_CAN2_SCE, BSP_IntHandlerCAN2_SCE); /*Assign ISR handler.Interrupt for which vector will be set*/
// BSP_IntEn(BSP_INT_ID_CAN2_SCE); /*Interrupt to enable.*/
CAN_ClearITPendingBit(CAN2, CAN_IT_FMP0);//清接收中断标志
CAN_ClearITPendingBit(CAN2, CAN_IT_TME);//清发送中断标志
// CAN_ITConfig(CAN2, CAN_IT_FMP0 |/*!< FIFO 0 message pending Interrupt*/
// CAN_IT_FMP1 |/*!< FIFO 1 message pending Interrupt*/
// CAN_IT_FF0 |/*!< FIFO 0 full Interrupt*/
// CAN_IT_FF1 |/*!< FIFO 1 full Interrupt*/
// CAN_IT_TME |/*!< Transmit mailbox empty Interrupt*/
// CAN_IT_BOF |/*!< Bus-off Interrupt*/
// CAN_IT_EPV |/*!< Error passive Interrupt*/
// CAN_IT_EWG |/*!< Error warning Interrupt*/
// CAN_IT_ERR /*!< Error Interrupt*/
// , ENABLE);
CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
// CAN_ITConfig(CAN2,CAN2_RX0_IRQn,ENABLE);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>