香主,我在调试CAN通信,但是调了多日依旧不知道哪里出了问题!请教下香主和坛里的高手!
发送函数
void CAN_Configuration(void)
{
CAN_InitTypeDef CAN_InitStructure;
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* 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; //禁止接收FIFO锁定模式
CAN_InitStructure.CAN_TXFP=ENABLE; //禁止发送FIFO优先级
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_2tq; //已经折算好了
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;
CAN_InitStructure.CAN_Prescaler=24;
CAN_Init(CAN1,&CAN_InitStructure);
}
unsigned char CAN_MessageSend(void)
{
CanTxMsg TxMessage;
u32 i = 0;
u8 TransmitMailbox = 0;
/* transmit */
TxMessage.StdId=0xa3; /* 设置标准id 注意标准id的最高7位不能全是隐性(1)。共11位 0xa3*/
TxMessage.ExtId=0x00;
TxMessage.RTR=CAN_RTR_DATA;//数据帧
TxMessage.IDE=CAN_ID_STD;//标准帧格式
TxMessage.DLC=2; //帧长度,单位:字节
TxMessage.Data[0]=0xCA;//待传数据
TxMessage.Data[1]=0xFE;
/*数据发送*/
TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
i = 0;
while((CAN_TransmitStatus(CAN1,TransmitMailbox) == CANTXFAILED) && (i != 0xFF)) //等待CAN总线传输数据成功
{
i++;
}
if(i==0xff)//发送超时,但发送不成功
{
return 0;
}
return 1; /* Test Passed */
}
/******************************接收函数**********************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
unsigned char CAN_Configuration(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE; //禁止时间触发通讯模式
CAN_InitStructure.CAN_ABOM=DISABLE; //禁止自动离线模式
CAN_InitStructure.CAN_AWUM=DISABLE; //禁止自动唤醒模式
CAN_InitStructure.CAN_NART=DISABLE; //禁止非自动重传模式 ENABLE
CAN_InitStructure.CAN_RFLM=DISABLE; //禁止接收FIFO锁定模式
CAN_InitStructure.CAN_TXFP=DISABLE; //禁止发送FIFO优先级 ENABLE
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_2tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;
CAN_InitStructure.CAN_Prescaler=24;
CAN_Init(CAN1,&CAN_InitStructure);
/* CAN 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; //((0x11<<21)|CAN_ID_STD|CAN_RTR_DATA)&0xFFFF;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;//((0x11<<21)|CAN_ID_STD|CAN_RTR_REMOTE)&0xFFFF;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;; //0x0000; //标识符需要比较(发送标识符为0x11,标准数据帧)
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;; //0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;//关联到FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //激活过滤器0
CAN_FilterInit(&CAN_FilterInitStructure);
return 1;
}
unsigned char CAN_MessageReceive(void)
{
CanRxMsg RxMessage;
if(CAN_MessagePending(CAN1, CAN_FIFO0)==0)
return 0;
RxMessage.StdId=0x00;
RxMessage.IDE=CAN_ID_STD;
RxMessage.DLC=0;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x00;
CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);
if ((RxMessage.StdId!=0xa3) && (RxMessage.IDE!=CAN_ID_STD) && (RxMessage.DLC!=2))
{
return 0;
}
if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE)
{
return 0;
}
return 1; /* Test Passed */
}
实在是找不出问题! |