利用查询模式进行CAN的环回模式已经完成,给需要的朋友参考下 int main(void) { static GPIO_InitTypeDef GPIO_InitStruct; static CAN_HandleTypeDef CAN_HandleStruct; static CAN_FilterConfTypeDef CAN_FilterConfStruct; static CanTxMsgTypeDef CanTxMsgStruct; static CanRxMsgTypeDef CanRxMsgStruct; typedef unsigned char u8; typedef unsigned short int u16; typedef unsigned int u32; typedef unsigned __int64 u64; u8 Txbuf[8]; u8 Rxbuf[8]; u8 i; u8 cnt = 0; HAL_Init(); SystemClock_Config(); __HAL_RCC_CAN1_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitStruct.Alternate = GPIO_AF9_CAN; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); CAN_HandleStruct.Init.ABOM = DISABLE; CAN_HandleStruct.Init.AWUM = DISABLE; CAN_HandleStruct.Init.BS1 = CAN_BS1_7TQ; CAN_HandleStruct.Init.BS2 = CAN_BS2_6TQ; CAN_HandleStruct.Init.Mode = CAN_MODE_LOOPBACK; CAN_HandleStruct.Init.NART = ENABLE; CAN_HandleStruct.Init.Prescaler = 6; CAN_HandleStruct.Init.RFLM = DISABLE; CAN_HandleStruct.Init.SJW = DISABLE; CAN_HandleStruct.Init.TTCM = DISABLE; CAN_HandleStruct.Init.TXFP = DISABLE; CAN_HandleStruct.Instance = CAN; CAN_HandleStruct.Lock = HAL_UNLOCKED; CAN_HandleStruct.pRxMsg = &CanRxMsgStruct; CAN_HandleStruct.pTxMsg = &CanTxMsgStruct; HAL_CAN_Init(&CAN_HandleStruct); CAN_FilterConfStruct.BankNumber = 0; CAN_FilterConfStruct.FilterActivation = ENABLE; CAN_FilterConfStruct.FilterFIFOAssignment = CAN_FILTER_FIFO0; CAN_FilterConfStruct.FilterIdHigh = 0x0000; CAN_FilterConfStruct.FilterIdLow = 0x0000; CAN_FilterConfStruct.FilterMaskIdHigh = 0x0000; CAN_FilterConfStruct.FilterMaskIdLow = 0x0000; CAN_FilterConfStruct.FilterMode = CAN_FILTERMODE_IDMASK; CAN_FilterConfStruct.FilterNumber = 0; CAN_FilterConfStruct.FilterScale = CAN_FILTERSCALE_32BIT; HAL_CAN_ConfigFilter(&CAN_HandleStruct, &CAN_FilterConfStruct); while(1) { for(i=0;i<=8;i++) { Txbuf=cnt+i; } CanTxMsgStruct.DLC = 8; CanTxMsgStruct.ExtId = 0x12; CanTxMsgStruct.IDE = 0; CanTxMsgStruct.RTR = 0; CanTxMsgStruct.StdId = 0x12; for(i=0;i<8;i++) CanTxMsgStruct.Data=Txbuf; HAL_CAN_Transmit(&CAN_HandleStruct,0); HAL_CAN_Receive(&CAN_HandleStruct,CAN_FIFO0, 1000); for(i=0;i<CanRxMsgStruct.DLC; i++) Rxbuf = CanRxMsgStruct.Data; if(CanRxMsgStruct.DLC) { for(i=0;i<=8;i++) { printf("%d\t", Rxbuf); } } HAL_Delay(1000); } }
所在话题
参与活动
-
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
线下 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
网络 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
网络 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
网络 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
线下 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
线下 2020-10-16