|
高手们,帮我分析一下啊!
int main(void)
{
CanRxMsg RxMessage;
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC Configuration */
NVIC_Configuration();
LED_Configuration();
CAN_Configuration();
while(1)
{
if (CAN_Polling() == FAILED)
leddisplay(LEDTWO, LEDON);
else
leddisplay(LEDTHREE, LEDON);
if (CAN_MessagePending(CAN_FIFO0))
{
/* receive */
RxMessage.StdId = 0x00;
RxMessage.IDE = CAN_ID_STD;
RxMessage.DLC = 0x00;
RxMessage.Data[0] = 0x00;
RxMessage.Data[1] = 0x00;
CAN_Receive(CAN_FIFO0, &RxMessage);
}
}
}
TestStatus CAN_Polling ( void )
{
CanTxMsg TxMessage;
u32 retry = TIMEOUT;
u8 TransmitMailbox;
leddisplay(LEDONE, LEDOFF);
leddisplay(LEDTWO, LEDOFF);
leddisplay(LEDTHREE, LEDOFF);
while (CAN_Reset() && retry --) ;
if (!retry)
return FAILED;
TxMessage.StdId = 0x12;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 0x02;
TxMessage.Data[0] = 0x55;
TxMessage.Data[1] = 0xAA;
retry = TIMEOUT;
TransmitMailbox = CAN_Transmit(&TxMessage); // 执行一次,在USB-CAN接收端收到100多帧。
while ((CAN_TransmitStatus(TransmitMailbox) != CANTXOK) && retry --) ; //状态一直是CANTXPENDING
if (!retry)
return FAILED;
return PASSED;
}
u8 CAN_Reset ( void )
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
// 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_Mode_LoopBack;//;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 = 0x05; // 50 kbps
if (CAN_Init(&CAN_InitStructure) == 0x00)
{
leddisplay(LEDONE, LEDON);
return 0xff;
}
// CAN filter init
CAN_FilterInitStructure.CAN_FilterNumber = 0x00;
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 = 0x00;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
return 0x00;
} |
|