设置成正常模式后,接收数据的时候必须用仿真器来跑才能正确接到数据
离线后,直接上电总是收不到正确的数,何解?
代码:
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC Configuration */
NVIC_Configuration();
/* GPIO ports pins Configuration */
GPIO_Configuration();
while (1)
{
//TestRx = CAN_Polling();
TestRx = CAN_Receving();
if (TestRx == FAILED)
{
// Turn on led connected to GPIO_LED pin8 (LD3)
GPIO_SetBits(GPIO_LED, GPIO_Pin_11);
}
else
{
// Turn on led connected to GPIO_LED pin6 (LD1)
GPIO_ResetBits(GPIO_LED, GPIO_Pin_11);
delay();
GPIO_SetBits(GPIO_LED, GPIO_Pin_11);
delay();
}
}
}
TestStatus CAN_Receving(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CanRxMsg RxMessage;
/* CAN register init */
CAN_DeInit();
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;
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_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=2;
CAN_Init(&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;
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);
/* receive */
RxMessage.StdId=0x00;
RxMessage.ExtId=0x00;
RxMessage.IDE=CAN_ID_STD;
RxMessage.IDE=0;
RxMessage.DLC=0;
RxMessage.FMI=0;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x00;
CAN_Receive(CAN_FIFO0, &RxMessage);
if (RxMessage.StdId!=0x11)
{
return FAILED;
}
/*
if (RxMessage.ExtId!=0x22)
{
return FAILED;
}
*/
if (RxMessage.IDE!=CAN_ID_STD)
{
return FAILED;
}
if (RxMessage.DLC!=2)
{
return FAILED;
}
if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0x3344)
{
return FAILED;
}
return PASSED; /* Test Passed */
} |