1、默认STM32CAN1就是主站。CAN2是从站
2、主站配置如下:参考时钟是72M/2=36M
波特率:36M/9/(2+2+1)=800K.所以通讯频率800K。
注意此处BUS-Off一定要enable。否则出现插上下载器两个站就不能同时发送的情况
3、中断设置
4、can开启函数:
HAL_CAN_Start(&hcan1);
HAL_CAN_ActivateNotification(&hcan1,CAN_IT_RX_FIFO0_MSG_PENDING);
can发送函数:
void CAN_TRANSMIT1(void)
{
TxMessage2.DLC=8;
TxMessage2.StdId=0x314;
TxMessage2.ExtId=0x00001314;
TxMessage2.IDE=CAN_ID_EXT;
TxMessage2.RTR=CAN_RTR_DATA;
uint8_t TR_BUF[8]=“22222222”;
if(HAL_CAN_AddTxMessage(&hcan1,&TxMessage2,TR_BUF,(uint32_t*)CAN_TX_MAILBOX1)!=HAL_OK)
{
printf("???1");
}
}
5、can1配置
static void MX_CAN1_Init(void)
{
/* USER CODE BEGIN CAN1_Init 0 /
CAN_FilterTypeDef sFilterConfig;
/ USER CODE END CAN1_Init 0 */
/* USER CODE BEGIN CAN1_Init 1 */
/* USER CODE END CAN1_Init 1 /
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 18;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_2TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = ENABLE;
hcan1.Init.AutoWakeUp = DISABLE;
hcan1.Init.AutoRetransmission = DISABLE;
hcan1.Init.ReceiveFifoLocked = DISABLE;
hcan1.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
Error_Handler();
}
/ USER CODE BEGIN CAN1_Init 2 */
sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
if(HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig)!=HAL_OK)
{
Error_Handler();
}
/* USER CODE END CAN1_Init 2 */
}
6、CAN1中断函数
void CAN1_RX0_IRQHandler(void)
{
/* USER CODE BEGIN CAN1_RX0_IRQn 0 */
HAL_CAN_GetRxMessage(&hcan1,CAN_RX_FIFO0,&RxMessage1,RXBUF);
/* USER CODE END CAN1_RX0_IRQn 0 /
HAL_CAN_IRQHandler(&hcan1);
/ USER CODE BEGIN CAN1_RX0_IRQn 1 */
/* USER CODE END CAN1_RX0_IRQn 1 */
}
7、CAN2配置
static void MX_CAN2_Init(void)
{
/* USER CODE BEGIN CAN2_Init 0 /
CAN_FilterTypeDef sFilterConfig;
/ USER CODE END CAN2_Init 0 */
/* USER CODE BEGIN CAN2_Init 1 */
/* USER CODE END CAN2_Init 1 /
hcan2.Instance = CAN2;
hcan2.Init.Prescaler = 18;
hcan2.Init.Mode = CAN_MODE_NORMAL;
hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan2.Init.TimeSeg1 = CAN_BS1_2TQ;
hcan2.Init.TimeSeg2 = CAN_BS2_1TQ;
hcan2.Init.TimeTriggeredMode = DISABLE;
hcan2.Init.AutoBusOff = ENABLE;
hcan2.Init.AutoWakeUp = DISABLE;
hcan2.Init.AutoRetransmission = DISABLE;
hcan2.Init.ReceiveFifoLocked = DISABLE;
hcan2.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan2) != HAL_OK)
{
Error_Handler();
}
/ USER CODE BEGIN CAN2_Init 2 */
sFilterConfig.FilterBank = 14;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
if(HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig)!=HAL_OK)
{
Error_Handler();
}
HAL_CAN_Start(&hcan2);
HAL_CAN_ActivateNotification(&hcan2,CAN_IT_RX_FIFO0_MSG_PENDING);
/* USER CODE END CAN2_Init 2 */
}
8、CAN2中断函数
void CAN2_RX0_IRQHandler(void)
{
/* USER CODE BEGIN CAN2_RX0_IRQn 0 /
HAL_CAN_GetRxMessage(&hcan2,CAN_RX_FIFO0,&RxMessage1,RXBUF);
/ USER CODE END CAN2_RX0_IRQn 0 /
HAL_CAN_IRQHandler(&hcan2);
/ USER CODE BEGIN CAN2_RX0_IRQn 1 */
/* USER CODE END CAN2_RX0_IRQn 1 */
}
9、CAN2发送函数
void CAN_TRANSMIT1(void)
{
static uint8_t ll;
TxMessage2.DLC=8;
TxMessage2.StdId=0x003;
TxMessage2.ExtId=0x00000003;
TxMessage2.IDE=CAN_ID_EXT;
TxMessage2.RTR=CAN_RTR_DATA;
ll++;
if(ll>100) {ll=0;}
uint8_t TR_BUF[8]={0,1,2,3,4,5,6,ll};
if(HAL_CAN_AddTxMessage(&hcan2,&TxMessage2,TR_BUF,(uint32_t*)CAN_TX_MAILBOX1)!=HAL_OK)
{
//´íÎó
}
}