选择对应CAN,勾上Mode里的Activated,启动CAN
1. Parameter Settings:
Bit Timing Parameters(位时间参数)
Baud Rate(波特率) = APB1 peripheral clocks(时钟树所设置的APB1总线频率) /(Time Quanta in Bit Segment 1 + Time Quanta in Bit Segment 2 + ReSynchronization Jump Width)/ Prescaler(分频值)
Basic Parameters(基本参数,使能or不使能)
Advanced Parameters (高级参数)
2. NVIC Settings:
3. User Constants:设置常量
4. GPIO Settings:引脚配置(不可修改)
最后设置工程名生成即可
CAN_TxHeaderTypeDef Tx1Message;/*can1发送帧相关信息*/
CAN_RxHeaderTypeDef Rx1Message;/*can1接收帧相关信息*/
uint8_t CAN1_Rx_data[8];/*can1发送帧数据*/
uint8_t CAN1_Tx_data[8];/*can1接收帧数据*/
void can_device_init(void)
{
CAN_FilterTypeDef can_filter;/*过滤器设置*/
can_filter.FilterBank = 0;
/*使用的过滤器编号。使用一个CAN,则可选0~13;使用两个CAN可选0~27*/
can_filter.FilterMode = CAN_FILTERMODE_IDMASK;
/*过滤器模式选择。掩码模式填写CAN_FILTERMODE_IDMASK,
列表模式填写CAN_FILTERMODE_IDLIST */
can_filter.FilterScale = CAN_FILTERSCALE_32BIT;
/*过滤器位宽,32位是CAN_FILTERSCALE_32BIT,
16位是CAN_FILTERSCALE_16BIT */
can_filter.FilterIdHigh = 0x0000;
/*过滤器验证码ID高16位,参数值:0~0xFFFF*/
can_filter.FilterIdLow = 0x0000;
/*过滤器验证码ID低16位,参数值:0~0xFFFF*/
can_filter.FilterMaskIdHigh = 0x0000;
/*过滤器掩码ID高16位,参数值:0~0xFFFF*/
can_filter.FilterMaskIdLow = 0x0000;
/*过滤器掩码ID低16位,参数值:0~0xFFFF*/
can_filter.FilterFIFOAssignment = CAN_FilterFIFO0;
/*将通过的报文放入哪个FIFOx中,填写FIFO(x)*/
can_filter.SlaveStartFilterBank = 0;
/*can2开始的过滤器*/
can_filter.FilterActivation = ENABLE;
/*是否使能过滤器,DISABLE 或 ENABLE */
HAL_CAN_ConfigFilter(&hcan1, &can_filter);
while (HAL_CAN_ConfigFilter(&hcan1, &can_filter) != HAL_OK);
/*配置can1过滤器并等待至配置完成*/
can_filter.FilterBank = 14;
HAL_CAN_ConfigFilter(&hcan2, &can_filter);
while (HAL_CAN_ConfigFilter(&hcan2, &can_filter) != HAL_OK);
/*配置can2过滤器并等待至配置完成*/
HAL_Delay(100);
HAL_CAN_Start(&hcan1);
HAL_CAN_ActivateNotification(&hcan1,CAN_IT_RX_FIFO0_MSG_PENDING);
/*开启can1并使能中断*/
HAL_CAN_Start(&hcan2);
HAL_CAN_ActivateNotification(&hcan2,CAN_IT_RX_FIFO0_MSG_PENDING);
/*开启can2并使能中断*/
}
void can1_send_message(int16_t TX_ID, int16_t iq1, int16_t iq2, int16_t iq3, int16_t iq4)
{
uint8_t FreeTxNum = 0;
Tx2Message.StdId = TX_ID;
/*标准ID。参数值:0到0x7FF*/
Tx2Message.ExtId = 0;
/*扩展ID。参数值:0到0x1FFFFFFF*/
Tx2Message.IDE = CAN_ID_STD;
/*标准帧填写CAN_ID_STD,扩展帧填写CAN_ID_EXT */
Tx2Message.RTR = CAN_RTR_DATA;
/*数据帧填写CAN_RTR_DATA,遥控帧填写CAN_RTR_REMOTE */
Tx2Message.DLC = 0x08;
/*数据长度,参数值:0到8*/
CAN1_Tx_data[0] = iq1 >> 8;
CAN1_Tx_data[1] = iq1;
CAN1_Tx_data[2] = iq2 >> 8 ;
CAN1_Tx_data[3] = iq2;
CAN1_Tx_data[4] = iq3 >> 8;
CAN1_Tx_data[5] = iq3;
CAN1_Tx_data[6] = iq4 >> 8;
CAN1_Tx_data[7] = iq4;
FreeTxNum = HAL_CAN_GetTxMailboxesFreeLevel(&hcan1);
while(FreeTxNum == 0)FreeTxNum = HAL_CAN_GetTxMailboxesFreeLevel(&hcan1);
/*等待发送邮箱有空*/
HAL_CAN_AddTxMessage(&hcan1, &Tx1Message,CAN1_Tx_data,(uint32_t*)CAN_TX_MAILBOX0);
/*将数据放入邮箱*/
}
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
if(hcan == &hcan1)
{
//将fifo0接收到的id等信息赋给Rx1Message,数据赋给CAN1_Rx_data
HAL_CAN_GetRxMessage(hcan,CAN_RX_FIFO0,&Rx1Message,CAN1_Rx_data);
//根据ID进行不同的操作,也在中断外进行操作
switch (Rx1Message.StdId)
{
};
__HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
}
else if(hcan == &hcan2 )
{
HAL_CAN_GetRxMessage(hcan,CAN_RX_FIFO0,&Rx2Message, CAN2_Rx_data);
switch (Rx2Message.StdId)
{
}
__HAL_CAN_ENABLE_IT(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
}
}
最终接收的数据将通过中断存入CAN1_Rx_data,发送数据则通过can1_send_message函数发送。