#include #include "..\..\lib\7186e.h" #include "..\..\lib\xc100.h" /*If the UserCANInt() function is not used, please don't remove it.*/ void UserCANInt(char CANInt){ if (CANInt) ; /*avoid complier warning*/ if (CANInt==0x01){ /*for Rx interrupt*/ L3On(); } else if (CANInt==0x02){ /*for Tx interrupt*/ L2On(); } Delay(3); //It doesn't recommend to do this in real application L2Off(); L3Off(); } void main(void) { int ret,i; unsigned char MsgMode,MsgRTR,MsgDataLen,MsgData[8]; unsigned long MsgID,MsgUpperTimeStamp,MsgLowerTimeStamp,MsgCnt=0; unsigned long CANBaud[]={1000000UL,800000UL,500000UL,250000UL,200000UL,125000UL, 100000UL,50000UL,25000UL,20000UL,10000UL,5000UL}; int FoundBaud=0; InitLib(); /********************************************************/ /* Start to detect CAN bus baudrate */ /********************************************************/ Print("Start to detect CAN bus baudrate\r\n"); for(i=0;i<=11;i++) { FoundBaud=CAN_SearchBaud(CANBaud[i],0,0,500); if((FoundBaud == 0) || (FoundBaud != CAN_AutoBaudTimeout)) break; } if(FoundBaud == CAN_NoError) Print("Find CAN Baudrate: %lu\r\n",CANBaud[i]); else if(FoundBaud == CAN_AutoBaudTimeout) Print("Timeout, CAN Baudrate Not Find, use default setting.\r\n"); else Print("CAN_SearchBaud Error, %d\r\n",FoundBaud); /********************************************************/ /* Initial all LEDs */ /********************************************************/ L1Off(); L2Off(); L3Off(); /*******************************************************/ /* initialize and configure the CAN controller */ /*******************************************************/ if(FoundBaud == CAN_NoError ) ret=XC100Init(1,3,CANBaud[i],0,0,0x00000000UL,0xffffffffUL); else ret=XC100Init(1,3,125000UL,0,0,0x00000000UL,0xffffffffUL); /* XCANInit function Parameter descriptions 1 : for 7186EX(D) model number (7188XB(D) model number is 0) 3 : for Receive and transmission interrupt 125000UL : for CAN baud 0 : for BT0 of user defined baud 0 : for BT1 of user defined baud 0x00000000UL : for AccCode of CAN message filter 0xffffffffUL : for AccMask of CAN message filter */ switch (ret){ /*Check if configuration is OK*/ case CAN_ResetError: Print("Reset Error!\n\r"); return; case CAN_SetACRError: Print("\n\rSet ACR Error!"); return; case CAN_SetAMRError: Print("\n\rSet AMR Error!"); return; case CAN_SetBaudRateError: Print("\n\rSet Baud Rate Error!"); return; case CAN_BaudNotSupport: Print("\n\rBaud Rate Not Support!"); return; case CAN_ConfigError: Print("\n\rConfiguration Failure!"); return; default: if (ret >0){ Print("Default=%d\n\r",ret); return; } } /********************************************************/ /* Enable Irq */ /********************************************************/ CAN_CreateBuffer(0,1000); //Create RX Buffer CAN_CreateBuffer(1,1000); //Create TX Buffer CAN_InstallIrq(); L1On(); /*Turn on LED1 for indicating that the demo is starting*/ Print("Press any key to exit the program.\n\r"); while(1){ /********************************************************/ /* Receive CAN messages from CAN bus */ /********************************************************/ ret=GetCANMsg(&MsgMode,&MsgID,&MsgRTR,&MsgDataLen,MsgData,&MsgUpperTimeStamp,&MsgLowerTimeStamp); if (!ret){ MsgCnt++; Print("%lu(%lus-%luus):Mode=%d,ID=%lx,RTR=%d,Len=%d",MsgCnt,MsgUpperTimeStamp ,MsgLowerTimeStamp,MsgMode,MsgID,MsgRTR,MsgDataLen); if (MsgDataLen && !MsgRTR){ Print(",Data="); for (i=0;i