发布于2021-05-30 20:22 阅读(1235) 评论(0) 点赞(5) 收藏(1)
使用CubeMax进行参数配置,将端口重映射到PD0和PD1
配置其他参数
#include "bsp_can.h"
#include "can.h"
#include "stm32f4xx_hal_can.h"
#include <stdio.h>
/*******************************************************************************************
* @Func my_can_filter_init
* @Brief CAN1和CAN2滤波器配置
* @Param CAN_HandleTypeDef* hcan
* @Retval None
* @Date 2015/11/30
*******************************************************************************************/
void CAN1_Filter_Config(void)
{
//can1 &can2 use same filter config
CAN_FilterTypeDef CAN_FilterConfigStructure;
CAN_FilterConfigStructure.FilterMode = CAN_FILTERMODE_IDMASK;
CAN_FilterConfigStructure.FilterScale = CAN_FILTERSCALE_32BIT;
CAN_FilterConfigStructure.FilterIdHigh = 0x0000;
CAN_FilterConfigStructure.FilterIdLow = 0x0000;
CAN_FilterConfigStructure.FilterMaskIdHigh = 0x0000;
CAN_FilterConfigStructure.FilterMaskIdLow = 0x0000;
CAN_FilterConfigStructure.FilterFIFOAssignment = CAN_FilterFIFO0;
CAN_FilterConfigStructure.FilterBank = 14;//can1(0-13)和can2(14-27)分别得到一半的filter
CAN_FilterConfigStructure.FilterActivation = ENABLE;
if(HAL_CAN_ConfigFilter(&hcan1, &CAN_FilterConfigStructure) != HAL_OK)
{
printf("CAN Filter Config Fail!\r\n");
Error_Handler();
}
}
static CAN_TxHeaderTypeDef TxMessage; //CAN发送的消息的消息头
static CAN_RxHeaderTypeDef RxMessage; //CAN接收的消息的消息头
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
uint8_t data[8];
HAL_StatusTypeDef status;
status = HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxMessage, data);
printf("--->Data Receieve!\r\n");
if (HAL_OK == status){
printf("--->Data Receieve!\r\n");
printf("RxMessage.StdId is %#x\r\n", RxMessage.StdId);
printf("data[0] is 0x%02x\r\n", data[0]);
printf("data[1] is 0x%02x\r\n", data[1]);
printf("data[2] is 0x%02x\r\n", data[2]);
printf("data[3] is 0x%02x\r\n", data[3]);
printf("<---\r\n");
}
}
uint32_t CAN_TX_BOX;
/* CAN 发送数据测试函数 */
void CAN1_Send_Test(void)
{
printf("----------------Start-------------------\r\n");
uint8_t data[4] = {0x01, 0x02, 0x03, 0x04};
TxMessage.IDE = CAN_ID_STD; //设置ID类型
TxMessage.StdId = 0x111; //设置ID号
TxMessage.RTR = CAN_RTR_DATA; //设置传送数据帧
TxMessage.DLC = 4; //设置数据长度
TxMessage.TransmitGlobalTime = DISABLE;
HAL_Delay(100);
if (HAL_CAN_AddTxMessage(&hcan1, &TxMessage, data, &CAN_TX_BOX) != HAL_OK) {
printf("Send mail is %d\r\n", CAN_TX_BOX);
Error_Handler();
}
uint32_t mail = HAL_CAN_GetTxMailboxesFreeLevel(&hcan1);
printf("Send mail is %d\r\n", CAN_TX_BOX);
printf("Free mail is %d\r\n", mail);
printf("CAN send test data success!\r\n");
}
CAN1_Filter_Config();
if (HAL_CAN_Start(&hcan1) != HAL_OK) {
printf("CAN Start Fail\r\n");
Error_Handler();
}
printf("CAN Start Success\r\n");
if (HAL_CAN_Start(&hcan1) != HAL_OK) {
printf("CAN Start Fail\r\n");
Error_Handler();
}
printf("CAN Start Success\r\n");
作者:天神下凡
链接:http://www.phpheidong.com/blog/article/86847/320d65b0d47a9bf060f7/
来源:php黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 php黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-4
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!