新手入门
- 积分
- 13
- 金钱
- 13
- 注册时间
- 2024-7-23
- 在线时间
- 2 小时
|
发表于 2024-7-30 19:36:59
|
显示全部楼层
大佬你好,我想问下如果我要配置过滤只接收拓展帧ID的bit28~25为0x01的数据,CAN_FilterInitStructure.CAN_FilterIdHigh、CAN_FilterInitStructure.CAN_FilterIdLow、CAN_FilterInitStructure.CAN_FilterMaskIdHigh、CAN_FilterInitStructure.CAN_FilterMaskIdLow这四个寄存器该如何配置?如下代码,我分别配置为0x0200、0x0000、0x1e00、0x0000无效,全配置为0x0000代码可以跑通。
void CANInit()
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* CAN1初始化 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_CAN1);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
CAN_DeInit(CAN1);
CAN_InitStructure.CAN_TTCM = DISABLE; /* 禁止时间触发模式(不生成时间戳), T */
CAN_InitStructure.CAN_ABOM = ENABLE; /* 自动总线关闭管理 */
CAN_InitStructure.CAN_AWUM = ENABLE; /* 自动唤醒模式 */
CAN_InitStructure.CAN_NART = DISABLE; /* 禁止仲裁丢失或出错后的自动重传功能 */
CAN_InitStructure.CAN_RFLM = DISABLE; /* 禁止接收FIFO加锁模式 */
CAN_InitStructure.CAN_TXFP = DISABLE; /* 禁止传输FIFO优先级 */
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; /* 设置CAN为正常工作模式 */
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
CAN_InitStructure.CAN_Prescaler = 14;
CAN_Init(CAN1, &CAN_InitStructure);
/* 设置CAN滤波器0 */
CAN_FilterInitStructure.CAN_FilterNumber = 0; /* 滤波器序号,0-13,共14个滤波器 */
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; /* 滤波器模式,设置ID掩码模式 */
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; /* 32位滤波 */
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0002; /* 掩码后ID的高16bit */
CAN_FilterInitStructure.CAN_FilterIdLow = 0xB7A4; /* 掩码后ID的低16bit */
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0007; /* ID掩码值高16bit */
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFE; /* ID掩码值低16bit */
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; /* 滤波器绑定FIFO 0 */
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; /* 使能滤波器 */
CAN_FilterInit(&CAN_FilterInitStructure);
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* CAN2初始化 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_CAN2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_CAN2);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOB, &GPIO_InitStructure);
CAN_DeInit(CAN2);
CAN_InitStructure.CAN_TTCM = DISABLE; /* 禁止时间触发模式(不生成时间戳), T */
CAN_InitStructure.CAN_ABOM = ENABLE; /* 自动总线关闭管理 */
CAN_InitStructure.CAN_AWUM = ENABLE; /* 自动唤醒模式 */
CAN_InitStructure.CAN_NART = DISABLE; /* 禁止仲裁丢失或出错后的自动重传功能 */
CAN_InitStructure.CAN_RFLM = DISABLE; /* 禁止接收FIFO加锁模式 */
CAN_InitStructure.CAN_TXFP = DISABLE; /* 禁止传输FIFO优先级 */
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; /* 设置CAN为正常工作模式 */
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
CAN_InitStructure.CAN_Prescaler = 28;
CAN_Init(CAN2, &CAN_InitStructure);
/* 设置CAN滤波器*/
if(device_param_get(CONTROL_CMD_BOARD_TYPE) == 1)
{
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0004;
}
else
{
CAN_FilterInitStructure.CAN_FilterIdLow = 0x4004;
}
CAN_FilterInitStructure.CAN_FilterNumber = 14;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; /* 滤波器模式,设置ID掩码模式 */
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; /* 32位滤波 */
#if 1//20240730滤一下协议号,bit28~25为0x01
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0200;//拓展帧ID的高16位,这里设置为0x01的高8位
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x1e00;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
#else
//240718先不滤波
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000; /* 掩码后ID的高16bit */
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000; /* 掩码后ID的低16bit */
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000; /* ID掩码值高16bit */
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000; /* ID掩码值低16bit */
#endif
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO1; /* 滤波器绑定FIFO 0 */
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; /* 使能滤波器 */
CAN_FilterInit(&CAN_FilterInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX1_IRQn;;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
CAN_ITConfig(CAN2, CAN_IT_FMP1, ENABLE);
}
|
|