只接收标准ID=0X80的数据帧,标准数据帧={0x29, 0xF2, 0x01, 0x00, 0x01},匹配好,PB14/15置 低;
一,过滤器配置
void CAN_Filter_Init(void) { CAN_FilterTypeDef sFilterConfig; sFilterConfig.FilterBank = 0; / 过滤器组0 / sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST; / 屏蔽位模�? / sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT; / 32位�??/ sFilterConfig.FilterIdHigh =0x80; / 要过滤的ID高位 / sFilterConfig.FilterIdLow = 0xFFFF; sFilterConfig.FilterMaskIdHigh = 0xFFFF; / 过滤器高16位每位必须匹�? / sFilterConfig.FilterMaskIdLow = 0xFFFF; / 过滤器低16位每位必须匹�? / sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0; / 过滤器被关联到FIFO 0 / sFilterConfig.FilterActivation = CAN_FILTER_ENABLE; / 使能过滤�? / sFilterConfig.SlaveStartFilterBank = 14; if( HAL_CAN_ConfigFilter(&hcan, &sFilterConfig)!=HAL_OK) {Error_Handler(); } if( HAL_CAN_Start(&hcan)!=HAL_OK) { Error_Handler(); } if( HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING)!=HAL_OK) { Error_Handler(); } }
二,中断回调函数
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) { uint32_t i; uint8_t aData[5],RxData[5]; if(hcan->Instance==CAN1) { if( HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &RxHeader, aData)==HAL_OK ) { for(i = 0; i<5; i++) { RxData[i] = aData[i]; } } if((RxData[0]==0x29) && (RxData[1]=0xF2) && (RxData[2]==0x01) && (RxData[3]==0x00) && (RxData[4]==0x01)) { HAL_GPIO_WritePin(GPIOB,GPIO_PIN_14,RESET); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,RESET); } } }