这是我第一次编程微控制器,我想知道如何使用中断通过CAN接收msg。我初始化了我的micro,STM32F105,因此只允许0x0106和0x010A ID。 这是主部分:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_CAN1_Init();
MX_IWDG_Init();
MX_RTC_Init();
MX_SPI1_Init();
MX_TIM1_Init();
MX_TIM2_Init();
MX_UART4_Init();
MX_UART5_Init();
/* USER CODE BEGIN 2 */
/* Initialize CAN Filter */
CAN_FilterConfTypeDef sFilterConfig;
sFilterConfig.FilterNumber=0;
sFilterConfig.FilterMode=CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale=CAN_FILTERSCALE_16BIT;
sFilterConfig.FilterIdHigh=0x10A<<5;
sFilterConfig.FilterIdLow=0x106<<5;
sFilterConfig.FilterMaskIdHigh=0x7FF<<5;
sFilterConfig.FilterMaskIdLow=0x7FF<<5;
sFilterConfig.FilterFIFOAssignment=0;
sFilterConfig.FilterActivation=ENABLE;
sFilterConfig.BankNumber=0;
HAL_CAN_ConfigFilter(&hcan1,&sFilterConfig);
if (HAL_CAN_ConfigFilter(&hcan1,&sFilterConfig) != HAL_OK)
{
Error_Handler();
}
HAL_CAN_Receive_IT(&hcan1,CAN_FIFO0);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
//some code here
}
/* USER CODE END 3 */
这是CAN初始化部分:
/* CAN1 init function */
static void MX_CAN1_Init(void)
{
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 4;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SJW = CAN_SJW_1TQ;
hcan1.Init.BS1 = CAN_BS1_6TQ;
hcan1.Init.BS2 = CAN_BS2_1TQ;
hcan1.Init.TTCM = DISABLE;
hcan1.Init.ABOM = DISABLE;
hcan1.Init.AWUM = DISABLE;
hcan1.Init.NART = DISABLE;
hcan1.Init.RFLM = DISABLE;
hcan1.Init.TXFP = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
Error_Handler();
}
}
这是中断部分:
void CAN1_RX0_IRQHandler(void)
{
/* USER CODE BEGIN CAN1_RX0_IRQn 0 */
/* USER CODE END CAN1_RX0_IRQn 0 */
HAL_CAN_IRQHandler(&hcan1);
/* USER CODE BEGIN CAN1_RX0_IRQn 1 */
// other code here
}
基本上,当我测试我的电路板时,似乎永远不会执行中断例程。你知道问题出在哪里吗?