小弟初学没几天,用F4discovery的开发板,再用cubeMX的软件配置了外部晶振,系统时钟168MHz,APB1是42MHz,生成的代码看起来比较繁琐,大都是结构体套结构体,不知道具体是多少的波特率,所以自行配置需求500K,编译无任何问题,灯亮,四轴飞行器,但TX引脚无信号输出。
调试过程中,第一次会进入发信箱,但是好像始终超时,不能发送,reset后再调试就会说信箱繁忙,最后就像跳入一个死循环。
求救大侠指点迷津!
CAN初始化代码如下:
void MX_CAN1_Init(void)
{
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 7;//16;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SJW = 1;//CAN_SJW_1TQ;
hcan1.Init.BS1 = 3;//CAN_BS1_1TQ;
hcan1.Init.BS2 = 6;//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;
HAL_CAN_Init(&hcan1);
}
main函数部分代码如下:
pTxMsg_test.Data[0] = 0x11;
pTxMsg_test.Data[1] = 0x22;
pTxMsg_test.Data[2] = 0x33;
pTxMsg_test.Data[3] = 0x44;
pTxMsg_test.Data[4] = 0x55;
pTxMsg_test.Data[5] = 0x66;
pTxMsg_test.Data[6] = 0x77;
pTxMsg_test.Data[7] = 0x11;
pTxMsg_test.DLC = 8;
pTxMsg_test.StdId = 0x300;
CAN_HandleTypeDef CanTxMsg_test;
CanTxMsg_test.pTxMsg = &pTxMsg_test;
CanTxMsg_test.Instance = CAN1;
/* Infinite loop */
while (1)
{
HAL_Delay(500);
BSP_LED_Toggle(LED3);
BSP_LED_Toggle(LED4);
BSP_LED_Toggle(LED5);
BSP_LED_Toggle(LED6);
HAL_CAN_Transmit(&CanTxMsg_test, 10);
}
引脚定义如下:
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hcan->Instance==CAN1)
{
/* Peripheral clock enable */
__CAN1_CLK_ENABLE();
/**CAN1 GPIO Configuration
PD0 ------> CAN1_RX
PD1 ------> CAN1_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
} |