一、can界面配置
1、配置波特率
注意:高速can通信速率的范围200kbps~1mbps
波特率的计算:
2、开启接收中断
3、初始化
1、需要对CAN_FilterTypeDef FilterConfig结构体进行初始化
配置过滤器的结构体
CAN_FilterTypeDef FilterConfig
/*
可以过滤通过id为0x13x 0x42x
标准id共11位即最大到0x7ff
使用过滤组0
配置FilterConfig.SlaveStartFilterBank,从can的过滤组由14后到27
使用掩码模式可过滤关键id
*/
FilterConfig.FilterActivation=CAN_FILTER_ENABLE;
FilterConfig.FilterBank=0;
FilterConfig.FilterFIFOAssignment=CAN_FILTER_FIFO0;//选择对应的fifo通道可以是0或1,与fifo_rx中断对应
FilterConfig.FilterIdHigh=0x135<<5;
FilterConfig.FilterIdLow=0x423<<5;
FilterConfig.FilterMaskIdHigh=0;
FilterConfig.FilterMaskIdLow=0x7F0<<5;
FilterConfig.FilterMode= CAN_FILTERMODE_IDMASK;
FilterConfig.FilterScale=CAN_FILTERSCALE_16BIT;
FilterConfig.SlaveStartFilterBank=14;
HAL_CAN_ConfigFilter(&hcan1,&FilterConfig );
/*过滤通过的id为0x535 0x323 0x463 0x327
采用列表模式
过滤固定的id
*/
FilterConfig.FilterActivation=CAN_FILTER_ENABLE;
FilterConfig.FilterBank=1;
FilterConfig.FilterFIFOAssignment=CAN_FILTER_FIFO0;
FilterConfig.FilterIdHigh=0x535<<5;
FilterConfig.FilterIdLow=0x323<<5;
FilterConfig.FilterMaskIdHigh=0x327<<5;
FilterConfig.FilterMaskIdLow=0x463<<5;
FilterConfig.FilterMode= CAN_FILTERMODE_IDLIST;
FilterConfig.FilterScale=CAN_FILTERSCALE_16BIT;
FilterConfig.SlaveStartFilterBank=14;
HAL_CAN_ConfigFilter(&hcan1,&FilterConfig );
/*
过滤使用列表模式采用扩展帧格式
可通过id 0x1800a001
采用标准帧
可通过id 0x135
*/
FilterConfig.FilterActivation=CAN_FILTER_ENABLE;
FilterConfig.FilterBank=2;
FilterConfig.FilterFIFOAssignment=CAN_FILTER_FIFO0;
FilterConfig.FilterIdHigh=((0x1800a001<<3)>>16)&0xffff;
FilterConfig.FilterIdLow=((0x1800a001<<3)&0xffff)|CAN_ID_EXT;
FilterConfig.FilterMaskIdHigh=((0x135<<21)&0xffff)>>16;
FilterConfig.FilterMaskIdLow=0|CAN_ID_STD;
FilterConfig.FilterMode= CAN_FILTERMODE_IDLIST;
FilterConfig.FilterScale=CAN_FILTERSCALE_32BIT;
FilterConfig.SlaveStartFilterBank=14;
HAL_CAN_ConfigFilter(&hcan1,&FilterConfig );
/*
过滤使用掩码模式采用扩展帧格式
可通过id 0x1234567x
*/
FilterConfig.FilterActivation=CAN_FILTER_ENABLE;
FilterConfig.FilterBank=3;
FilterConfig.FilterFIFOAssignment=CAN_FILTER_FIFO0;
FilterConfig.FilterIdHigh=((0x12345678<<3)>>16)&0xffff;
FilterConfig.FilterIdLow=((0x12345678<<3)&0xffff)|CAN_ID_EXT;
FilterConfig.FilterMaskIdHigh=((0xfffffff0<<3)&0xffff)>>16;
FilterConfig.FilterMaskIdLow=((0xfffffff0<<3)|CAN_ID_EXT)&0xffff;;
FilterConfig.FilterMode= CAN_FILTERMODE_IDMASK;
FilterConfig.FilterScale=CAN_FILTERSCALE_32BIT;
FilterConfig.SlaveStartFilterBank=14;
HAL_CAN_ConfigFilter(&hcan1,&FilterConfig );
HAL_CAN_Start(&hcan1);//开启can
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING); //使能rx中断
2、配置CAN_TxHeaderTypeDef txpHeader;结构体
用于发送数据的结构体
//发送can数据
uint8_t CAN1_Send_Msg(uint32_t id,uint8_t* msg,uint8_t len)
{
uint8_t i=0;
uint32_t TxMailbox=CAN_TX_MAILBOX0 ;
uint8_t message[8];
txpHeader.StdId=id; //标准标识符
txpHeader.ExtId=id; //扩展标识符(29位)
//txpHeader.IDE=CAN_ID_STD; //使用标准帧
txpHeader.IDE=CAN_ID_EXT;
txpHeader.RTR=CAN_RTR_DATA; //数据帧
txpHeader.DLC=len;
for(i=0;i<len;i++)
{
message[i]=msg[i];
}
printf("发送者id 0x%0x ",txpHeader.ExtId);
if(HAL_CAN_AddTxMessage(&hcan1, &txpHeader,message, &TxMailbox) != HAL_OK)//发送
{
return 1;
}
else
{
while(HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) != 3) {}
return 0;
}
}
配置CAN_RxHeaderTypeDef rxpHeader;;结构体
用于接收数据的结构体
//接收can数据
uint8_t CAN1_Receive_Msg(uint8_t *buf)
{
uint8_t i;
uint8_t RxData[8];
if(HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_RX_FIFO0) != 1)
{
return 0;
}
if(HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &rxpHeader, RxData) != HAL_OK)
{
return -1;
}
for(i=0;i<rxpHeader.DLC;i++)
buf[i]=RxData[i];
return rxpHeader.DLC;
}
//中断回调函数处理
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *canHandle)
{
uint8_t j=0;
uint8_t rbuf[8];
if(HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &rxpHeader, rbuf) != HAL_OK)
{
}
printf("中断接收id 0x%0x 数据:",rxpHeader.ExtId);
for(j=0;j<rxpHeader.DLC;j++)
{
printf("0x%0x ",rbuf[j]);
}
printf("\r\n");
HAL_CAN_ActivateNotification(canHandle, CAN_IT_RX_FIFO0_MSG_PENDING);
}
4、发送测试
uint8_t res;
res=CAN1_Send_Msg(0x1800a001,pool_buffer,8);
if(res)
{
printf("Send Failed!\r\n");
}
else
{
printf("发送数据:\n\r",);
for(int j=0;j<8;j++)
{
printf("0x%0x ",pool_buffer[j]);
}
printf("\r\n");
}
5、测试结果显示:
补充:
1、模式说明
CAN_MODE_NORMAL 正常模式
CAN_MODE_LOOPBACK 回环模式
CAN_MODE_SILENT 静默模式
CAN_MODE_SILENT_LOOPBACK 回环静默模式
2、过滤器配置
其中掩码模式中配置以为参考如下
3、显隐性电平问题
简单讲:电路中说的显性电平在数据分析时为0,隐形为1,是反字面意义的。