我使用的是TM4C129的开发板,在调试CAN通信时,LOOPBACK模式下调通了,另外一个板子用CAN芯片连接上也可以接收到信号,可是换成BASIC模式下就不行了,收发都没有反应,我用示波器也抓不到信号。有没有大神能帮帮我,搞了很久也没有找到解决办法。程序如下:
[code]void CANIntHandler(void)
{
uint32_t ulStatus;
ulStatus = CANIntStatus(CAN1_BASE, CAN_INT_STS_CAUSE);
sprintf(send,"ulStatus1=%d
",ulStatus);
UARTSend(send,strlen(send));
if(ulStatus == CAN_INT_INTID_STATUS){
sprintf(send,"ulStatus2=%d
",ulStatus);
UARTSend(send,strlen(send));
ulStatus = CANStatusGet(CAN1_BASE, CAN_STS_CONTROL);
sprintf(send,"ulStatus3=%d
",ulStatus);
UARTSend(send,strlen(send));
g_bErrFlag = 1;
}
else if(ulStatus == 1)
{
INT_CNT_ST++;
sprintf(send,"ulStatus4=%d ST=%d
",ulStatus,INT_CNT_ST);
UARTSend(send,strlen(send));
CANIntClear(CAN1_BASE, 1);
g_ulMsgCount_TX++;
g_bErrFlag = 0;
}
else if(ulStatus==2){
INT_CNT_RE++;
CANIntClear(CAN1_BASE, 2);
sprintf(send,"ulStatus5=%d
",ulStatus);
UARTSend(send,strlen(send));
g_ulMsgCount_RX++;
g_bRXFlag = 1;
g_bErrFlag = 0;
}
}
int main(void){
FPUEnable(); //ê1ÄüFPU
FPULazyStackingEnable();
tCANMsgObject sCANMessage;
uint8_t ucMsgData[4];
ui32SysClock = SysCtlClockFreqSet ( ( SYSCTL_XTAL_25MHZ |SYSCTL_OSC_MAIN |SYSCTL_USE_PLL |SYSCTL_CFG_VCO_480), 120000000);
IntMasterDisable();
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN1);
GPIOPinConfigure(GPIO_PB0_CAN1RX);
GPIOPinConfigure(GPIO_PB1_CAN1TX);
GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
CANInit(CAN1_BASE);
CANBitRateSet(CAN1_BASE, ui32SysClock, 1000000);
HWREG(CAN1_BASE + CAN_O_CTL) = HWREG(CAN1_BASE + CAN_O_CTL) | CAN_CTL_TEST;
//HWREG(CAN1_BASE + CAN_O_TST) = HWREG(CAN1_BASE + CAN_O_TST) | CAN_TST_LBACK | CAN_TST_SILENT;
HWREG(CAN1_BASE + CAN_O_TST) = HWREG(CAN1_BASE + CAN_O_TST) | CAN_TST_LBACK;
CANIntEnable(CAN1_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);
CANEnable(CAN1_BASE);
IntEnable(INT_CAN1);
*(uint32_t *)ucMsgData = 3; // ÅäÖ÷¢ËíμÄêy¾YÎa0
sCANMessage.ui32MsgID = 1; // ê1óÃ1×÷Îa·¢ËíID
sCANMessage.ui32MsgIDMask = 0; // 2»éèÖÃMASKÆá±Î
sCANMessage.ui32Flags = MSG_OBJ_TX_INT_ENABLE; //ê1Äü·¢ËíÖD¶Ï
sCANMessage.ui32MsgLen = sizeof(ucMsgData); //·¢Ëíêy¾Y°ü′óD¡Îa4¸ö×Ö½ú
sCANMessage.pui8MsgData = ucMsgData; // Ö¸Ïò·¢Ëíêy¾YμÄÖ¸Õë
srCANMessage.ui32MsgID = 1; // ½óêÕIDλÎa0
srCANMessage.ui32MsgIDMask = 0; // 2»éèÆá±Î£¬½óêÕËùóD¶«Î÷
srCANMessage.ui32Flags = MSG_OBJ_RX_INT_ENABLE | MSG_OBJ_USE_ID_FILTER; // ê1Äü½óêÕÖD¶ÏoíID1yÂË
srCANMessage.ui32MsgLen = 8; // ÔêDí×î¶à8¸ö×Ö½úμÄêy¾Y
ROM_CANMessageSet(CAN1_BASE, 2 , &srCANMessage, MSG_OBJ_TYPE_RX); // ½«message object 2éèÖÃÎa½óêÕ±¨ÎĶÔÏó
IntMasterEnable(); // ê1ÄüÖD¶Ï
while(1){
sprintf(send,"loop
");
UARTSend(send,strlen(send));
//HWREG(CAN1_BASE + CAN_O_IF1CRQ) = HWREG(CAN1_BASE + CAN_O_IF1CRQ) | CAN_IF1CRQ_BUSY;
CANMessageSet_test(CAN1_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_TX);
LEDWrite(CLP_D1, 1);
SysCtlDelay(ui32SysClock / 10);
LEDWrite(CLP_D1, 0);
SysCtlDelay(ui32SysClock / 10);
sprintf(send,"send msg=%x
",sCANMessage.pui8MsgData[0]);
UARTSend(send,strlen(send));
if(g_bErrFlag) // í¨1yg_bErrFlag±äá¿2é¿′êÇ·ñ3ö′í
{
sprintf(send,"ErrFlag=%d
",g_bErrFlag);
UARTSend(send,strlen(send));
g_bErrFlag = 1; // Ôù′ÎÖøö1°é
}
else{
g_ulMsgCount_TX = g_ulMsgCount_TX;
}
(*(uint32_t *)ucMsgData)++;
sprintf(send,"g_bRXFlag=%d
",g_bRXFlag);
UARTSend(send,strlen(send));
if (g_bRXFlag){
GPIOPinWrite(CLP_D2_PORT, CLP_D2_PIN, CLP_D2_PIN);
srCANMessage.pui8MsgData = ucrMsgData;
CANMessageGet(CAN1_BASE, 2 , &srCANMessage,0);
sprintf(send,"get msg=%x
",srCANMessage.pui8MsgData[0]);
UARTSend(send,strlen(send));
g_bRXFlag=0;
if(srCANMessage.ui32Flags & MSG_OBJ_DATA_LOST){
DATA_LOST = 1;
}
}
}
}
此帖出自
小平头技术问答
一周热门 更多>