我最近在用STM32F105和TJA1050调试CAN通讯我都整了一天不知道是什么原因,一下是我的硬件电路图和和CAN程序配置:
void CAN_Config(void)
{
/* CAN register init */
CAN_DeInit(CAN1);
CAN_DeInit(CAN2);
CAN_StructInit(&CAN_InitStructure);
/* CAN1 cell init */
CAN_InitStructure.CAN_TTCM = DISABLE;
CAN_InitStructure.CAN_ABOM = DISABLE;
CAN_InitStructure.CAN_AWUM = DISABLE;
CAN_InitStructure.CAN_NART = DISABLE;
CAN_InitStructure.CAN_RFLM = DISABLE;
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
CAN_InitStructure.CAN_Prescaler = 4;
CAN_Init(CAN1, &CAN_InitStructure);
CAN_Init(CAN2, &CAN_InitStructure);
/* CAN1 filter init */
CAN_FilterInitStructure.CAN_FilterNumber = 0; //
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* CAN2 filter init */
CAN_FilterInitStructure.CAN_FilterNumber = 14; //
CAN_FilterInit(&CAN_FilterInitStructure);
/* Transmit */
TxMessage.StdId = 0x321;
TxMessage.ExtId = 0x01;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 1;
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
CAN_ITConfig(CAN2,CAN_IT_FMP0, ENABLE);
}
中断函数如下:
void CAN1_RX0_IRQHandler(void)
{
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
if ((RxMessage.StdId == 0x321)&&(RxMessage.IDE == CAN_ID_STD)&&(RxMessage.DLC == 1)&&(RxMessage.Data[0] == 0xAA))
{
USRAT_Send_String("----------YES-------CAN1\n", 24, 1);
USRAT_Send_String(&RxMessage.Data[0], 1, 1);
}
else
{
USRAT_Send_String("ON1",4, 1);
}
CAN_ClearITPendingBit(CAN1, CAN_IT_EPV);
}
void CAN2_RX0_IRQHandler(void)
{
CAN_Receive(CAN2, CAN_FIFO0, &RxMessage);
if ((RxMessage.StdId == 0x321)&&(RxMessage.IDE == CAN_ID_STD)&&(RxMessage.DLC == 1)&&(RxMessage.Data[0] == 0x55))
{
USRAT_Send_String("----------YES-------CAN2\n", 26, 1);
USRAT_Send_String(&RxMessage.Data[0], 1, 1);
}
else
{
USRAT_Send_String("ON", 3, 1);
}
CAN_ClearITPendingBit(CAN2, CAN_IT_EPV);
}
主函数调用如下:
TxMessage.Data[0] = 0xAA;
CAN_Transmit(CAN2, &TxMessage);
delayms(100);
TxMessage.Data[0] = 0x55;
CAN_Transmit(CAN1, &TxMessage);
) |