在使用STM32F405调试CAN的时,使用官方的例程的环回模式时,数据收发都可以,但是将环回模式更改为正常模式,且接上CAN卡想抓数据时,发现发送数据失败(超时), CAN初始化代码如下:
void CanComInit(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
//// CAN register init
CAN_DeInit(CAN_ONE_PERIPH_PORT);
//// CAN 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_Mode_LoopBack;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
//// Baudrate (CAN clocked at 42 MHz)
//// Baudrate = 42 MHz/((1+BS1+BS2)*CAN_Prescaler)
CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 2;
CAN_Init(CAN_ONE_PERIPH_PORT, &CAN_InitStructure);
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0020; //// StdId = 0x01
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x003F;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
test = 1;
while(1)
{
//// transmit
TxMessage.StdId = test;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 2;
TxMessage.Data[0] = 0xCA;
TxMessage.Data[1] = 0xFf;
TransmitMailbox = CAN_Transmit(CAN_ONE_PERIPH_PORT, &TxMessage);
num = 0;
while((CAN_TransmitStatus(CAN_ONE_PERIPH_PORT, TransmitMailbox) != CANTXOK) && (num != 0xFFFFF))
{
num++;
}
//// num = 0;
//// while((CAN_MessagePending(CAN_ONE_PERIPH_PORT, CAN_FIFO0) < 1) && (num != 0xFFFF))
//// {
//// num++;
//// }
//// //// receive
//// RxMessage.StdId = 0x00;
//// RxMessage.IDE = CAN_ID_STD;
//// RxMessage.DLC = 0;
//// RxMessage.Data[0] = 0x00;
//// RxMessage.Data[1] = 0x00;
//// CAN_Receive(CAN_ONE_PERIPH_PORT, CAN_FIFO0, &RxMessage);
//// if (RxMessage.StdId != 0x11)
//// {
//// RxMessage.StdId = 0xFF;
//// }
}
}
发送后,num == 0xFFFFF 退出循环...
代码只是将环回模式更改为正常模式,其余没有更改,不知道为什么???
求指点...
|