通过STLINK硬件调试,程序每次运行到HAL_CAN_AddTxMessage(&hcan,&TxMessage,TData,(uint32_t*)CAN_TX_MAILBOX0);这个函数的
if (pHeader->IDE == CAN_ID_STD)
这一行的时候就会跳转到HardFault_Handler()中。
哪位大神知道为什么啊?
下面是我的代码- 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();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_CAN_Init();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
- Init_CANFilter();
- CAN_SetTxMsg();
- HAL_CAN_Start(&hcan);
- HAL_CAN_ActivateNotification(&hcan,CAN_IT_RX_FIFO0_MSG_PENDING);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- HAL_CAN_AddTxMessage(&hcan,&TxMessage,TData,(uint32_t*)CAN_TX_MAILBOX0);
- HAL_Delay(1000);
- HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);
- HAL_Delay(1000);
- HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- CAN_HandleTypeDef hcan;
- /* CAN init function */
- void MX_CAN_Init(void)
- {
- hcan.Instance = CAN1;
- hcan.Init.Prescaler = 9;
- hcan.Init.Mode = CAN_MODE_NORMAL;
- hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
- hcan.Init.TimeSeg1 = CAN_BS1_5TQ;
- hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
- hcan.Init.TimeTriggeredMode = DISABLE;
- hcan.Init.AutoBusOff = DISABLE;
- hcan.Init.AutoWakeUp = DISABLE;
- hcan.Init.AutoRetransmission = DISABLE;
- hcan.Init.ReceiveFifoLocked = DISABLE;
- hcan.Init.TransmitFifoPriority = DISABLE;
- if (HAL_CAN_Init(&hcan) != HAL_OK)
- {
- Error_Handler();
- }
- }
- void Init_CANFilter(void)
- {
- CAN_FilterTypeDef hFilterConfig;
- hFilterConfig.FilterBank = 0;
- hFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; //工作在标识符屏蔽位模式
- hFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;//过滤器位宽为单个32位。
- hFilterConfig.FilterIdHigh = (((uint32_t)0x1314<<3)&0xFFFF0000)>>16; //要过滤的ID高位
- hFilterConfig.FilterIdLow = (((uint32_t)0x1314<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF; //要过滤的ID低位
- hFilterConfig.FilterMaskIdHigh = 0x0; //过滤器高16位每位必须匹配
- hFilterConfig.FilterMaskIdLow = 0x0; //过滤器低16位每位必须匹配
- hFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0; //过滤器被关联到FIFO 0
- hFilterConfig.SlaveStartFilterBank = 14;
- hFilterConfig.FilterActivation = ENABLE; //使能过滤器
-
- HAL_CAN_ConfigFilter(&hcan, &hFilterConfig);
- //HAL_CAN_Start(&hcan);
- }
- void CAN_SetTxMsg(void)
- {
- TxMessage.StdId = 0;
- TxMessage.ExtId = 0X1314;
- TxMessage.DLC = 2;
- TxMessage.IDE = CAN_ID_EXT;
- TxMessage.RTR = CAN_RTR_DATA;
- TData[0] = 0xAA;
- TData[1] = 0xAA;
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>