使用ZLG公司的Demo程序CAN驱动程序,调用CANSendData发送返回为0,说明发送成功,用示波器测了CANTXD一直是高电平,只有一个40us脉宽的低脉冲, 初始化设置 void caninit(eCANNUM CanNum,uint8 Baud) { HwEnCAN(CanNum); SoftRstCAN(CanNum); CANEWL(CanNum).Bits.EWL_BIT = USE_EWL_CAN[CanNum]; //初始化波特率 //CANBTR(CanNum).Word = USE_BTR_CAN[CanNum]; switch (Baud) { case 0: CANBTR(CanNum).Word = (uint32)BPS_12K; break; case 1: CANBTR(CanNum).Word = (uint32)BPS_20K; break; case 2: CANBTR(CanNum).Word = (uint32)BPS_20K; break; case 3: CANBTR(CanNum).Word = (uint32)BPS_50K; break; default: CANBTR(CanNum).Word = (uint32)BPS_12K; break; } //初始化中断为非向量中断 VICDefVectAddr =(uint32)can_isr; VICIntEnable |=(1<<19)|(1<<(20+ CanNum))|(1<<(26+CanNum));
//中断使能 CANIER(CanNum).Word= USE_INT_CAN[CanNum]; //配置验收滤波器(旁路状态) CANAFMR.Bits.AccBP_BIT =1; CANAFMR.Bits.eFCAN_BIT = 0;
//初始化模式 //CANMOD(CanNum).Bits.TPM_BIT = USE_TPM_CAN[CanNum]; //CANMOD(CanNum).Bits. LOM_BIT = USE_MOD_CAN[CanNum]; //CANCMR(CanNum).Bits.SRR_BIT = 1; CANMOD(CanNum).Bits.TPM_BIT = USE_TPM_CAN1; CANMOD(CanNum).Bits. LOM_BIT = USE_MODE_CAN1; CANMOD(CanNum).Bits.STM_BIT = 1; CANMOD(CanNum).Bits.RPM_BIT = 0; //CANCMR(CanNum).Bits.SRR_BIT = 1;
//自测试模式 //CANMOD(CanNum).Bits.STM_BIT = 1; //CANMOD(CanNum).Bits.TM_BIT = 0; //初始化接收环形缓冲区 CANRcvBufApp.FullFlag1 = CANRcvBufApp.FullFlag2 = 0; CANRcvBufApp.ReadPoint1 = CANRcvBufApp.ReadPoint2 = 0; CANRcvBufApp.WritePoint1 = CANRcvBufApp.WritePoint2 = 0; //启动CAN SoftEnCAN(CanNum); }
我的测试函数 void test(void) { uint8 tmp[8] = {0}; uint8 datanum = 0; uint32 id = 0; uint8 i = 0; uint8 tempid[4] = {0}; stcRxBUF cbuf; if (ReadCANRcvCyBuf(CAN1,&cbuf) == 0) { datanum = cbuf.CANRcvFS.Bits.DLC_BIT; for(i=0;i<4;i++) { tempid = *((uint8 *)&cbuf.RxCANID + 3 - i); } send_buf(UART0,tempid,4); switch (datanum & 0x0F) { case 0: break; case 1: tmp[0] = cbuf.CANRDA.Bits.Data1_BIT; break; case 2: tmp[0] = cbuf.CANRDA.Bits.Data1_BIT; tmp[1] = cbuf.CANRDA.Bits.Data2_BIT; break; case 3: tmp[0] = cbuf.CANRDA.Bits.Data1_BIT; tmp[1] = cbuf.CANRDA.Bits.Data2_BIT; tmp[2] = cbuf.CANRDA.Bits.Data3_BIT; break; case 4: tmp[0] = cbuf.CANRDA.Bits.Data1_BIT; tmp[1] = cbuf.CANRDA.Bits.Data2_BIT; tmp[2] = cbuf.CANRDA.Bits.Data3_BIT; tmp[3] = cbuf.CANRDA.Bits.Data4_BIT; break; case 5: tmp[0] = cbuf.CANRDA.Bits.Data1_BIT; tmp[1] = cbuf.CANRDA.Bits.Data2_BIT; tmp[2] = cbuf.CANRDA.Bits.Data3_BIT; tmp[3] = cbuf.CANRDA.Bits.Data4_BIT; tmp[4] = cbuf.CANRDB.Bits.Data1_BIT; break; case 6: tmp[0] = cbuf.CANRDA.Bits.Data1_BIT; tmp[1] = cbuf.CANRDA.Bits.Data2_BIT; tmp[2] = cbuf.CANRDA.Bits.Data3_BIT; tmp[3] = cbuf.CANRDA.Bits.Data4_BIT; tmp[4] = cbuf.CANRDB.Bits.Data1_BIT; tmp[5] = cbuf.CANRDB.Bits.Data2_BIT; break; case 7: tmp[0] = cbuf.CANRDA.Bits.Data1_BIT; tmp[1] = cbuf.CANRDA.Bits.Data2_BIT; tmp[2] = cbuf.CANRDA.Bits.Data3_BIT; tmp[3] = cbuf.CANRDA.Bits.Data4_BIT; tmp[4] = cbuf.CANRDB.Bits.Data1_BIT; tmp[5] = cbuf.CANRDB.Bits.Data2_BIT; tmp[6] = cbuf.CANRDB.Bits.Data3_BIT; break; case 8: default: tmp[0] = cbuf.CANRDA.Bits.Data1_BIT; tmp[1] = cbuf.CANRDA.Bits.Data2_BIT; tmp[2] = cbuf.CANRDA.Bits.Data3_BIT; tmp[3] = cbuf.CANRDA.Bits.Data4_BIT; tmp[4] = cbuf.CANRDB.Bits.Data1_BIT; tmp[5] = cbuf.CANRDB.Bits.Data2_BIT; tmp[6] = cbuf.CANRDB.Bits.Data3_BIT; tmp[7] = cbuf.CANRDB.Bits.Data4_BIT; break; } send_buf(UART0,tmp,datanum); }
#if 1 /*两块目标板,接收都调通,在测试发送时,其中一个在此处 用#if 0屏蔽,只处理接收数据,另一个节点负责发送CAN数据*/ /*0成功,1失败*/ if (SendToCAN() == 0) char_SendBufByIrq(UART0,"成功!
"); else char_SendBufByIrq(UART0,"失败!
"); #endif delay(10000); }
/*向CAN发送数据*/ uint32 SendToCAN(void) { static uint8 SRCANBuf[16]; static uint8 CAN_NODE_ID = 001; uint32 i; static stcTxBUF CTbuf; //准备CAN帧数据 SRCANBuf[0] = CAN1; SRCANBuf[1] = 0x88;//帧信息:扩展帧 数据帧 DLC =8B SRCANBuf[14] = 0x02; //计算标示符 SRCANBuf[2] = 0x01<<4; //PRI 28位置高 SRCANBuf[2] |= (uint8)((uint8)(CAN_NODE_ID>>6) &0x1);//24b SRCANBuf[3] = (uint8)(CAN_NODE_ID<<2);//18~23b SRCANBuf[4] = 0; SRCANBuf[5] = 0; SRCANBuf[6] = 1; SRCANBuf[7] = 2; SRCANBuf[8] = 3; SRCANBuf[9] = 4; SRCANBuf[10] = 5; SRCANBuf[11] = 6; SRCANBuf[12] = 7; SRCANBuf[13] = 8; SRCANBuf[15] = 10;
CTbuf.TxFrameInfo.Bits.PRIO_BIT =SRCANBuf[0];//CAN通道号 CTbuf.TxFrameInfo.Bits.FF_BIT = 1; //帧格式 CTbuf.TxFrameInfo.Bits.RTR_BIT = 0; //帧类型 CTbuf.TxFrameInfo.Bits.DLC_BIT = SRCANBuf[1]; //数据长度
for (i=0; i<4; i++) { *((uint8 *)&CTbuf.TxCANID.Word +i)=SRCANBuf[5-i]; }
//memcpy((uint8 *)&CTbuf.CANTDA ,&SRCANBuf[6],4); //memcpy((uint8 *)&CTbuf.CANTDB ,&SRCANBuf[10],4); CTbuf.CANTDA.Bits.Data1_BIT = SRCANBuf[6]; CTbuf.CANTDA.Bits.Data2_BIT = SRCANBuf[7]; CTbuf.CANTDA.Bits.Data3_BIT = SRCANBuf[8]; CTbuf.CANTDA.Bits.Data4_BIT = SRCANBuf[9]; CTbuf.CANTDB.Bits.Data1_BIT = SRCANBuf[10]; CTbuf.CANTDB.Bits.Data2_BIT = SRCANBuf[11]; CTbuf.CANTDB.Bits.Data3_BIT = SRCANBuf[12]; CTbuf.CANTDB.Bits.Data4_BIT = SRCANBuf[13]; /*0成功,1失败*/ if (CANSendData(SRCANBuf[0],SRCANBuf[14],&CTbuf)==0) return 0; else return 1;
}
/*ZLG提供的驱动函数*/ uint32 CANSendData(eCANNUM CanNum,uint32 Cmd,P_stcTxBUF Buf) { uint32 i,status=0;
if (0 != CANSR(CanNum).Bits.TBS1_BIT) { i = SEND_TX_BUF1; } else if (0 != CANSR(CanNum).Bits.TBS2_BIT) { i = SEND_TX_BUF2; } else if (0 != CANSR(CanNum).Bits.TBS3_BIT) { i = SEND_TX_BUF3; } else { i = 0xFF; }
/*0成功,1失败*/ status = WriteCanTxBuf(CanNum,i, USE_TPM_CAN[CanNum],Buf);
if (status == 0) { #if 1 if(CANMOD(CanNum).Bits.SM_BIT != 0) { CanQuitSM(CanNum); } #endif CanSendCmd(CanNum,Cmd,i); } return (status); }
uint32 WriteCanTxBuf(eCANNUM CanNum,uint32 TxBufNum,uint32 TPM,P_stcTxBUF Buf) { uint32 status=0; switch (TxBufNum) { case SEND_TX_BUF1: if ((0 == CANSR(CanNum).Bits.TS1_BIT) && (0 != CANSR(CanNum).Bits.TBS1_BIT)) { CANTFI1(CanNum)=Buf->TxFrameInfo; CANTID1(CanNum)=Buf->TxCANID; CANTDA1(CanNum)=Buf->CANTDA; CANTDB1(CanNum)=Buf->CANTDB; } else { status = 0x01; } break; case SEND_TX_BUF2: if ((0== CANSR(CanNum).Bits.TS2_BIT)&&(0 !=CANSR(CanNum).Bits.TBS2_BIT)) { CANTFI2(CanNum)=Buf->TxFrameInfo; CANTID2(CanNum)=Buf->TxCANID; CANTDA3(CanNum)=Buf->CANTDA; CANTDB2(CanNum)=Buf->CANTDB; } else { status = 0x01; } break; case SEND_TX_BUF3: if ((0== CANSR(CanNum).Bits.TS3_BIT)&&(0 != CANSR(CanNum).Bits.TBS3_BIT)) { CANTFI3(CanNum)=Buf->TxFrameInfo; CANTID3(CanNum)=Buf->TxCANID; CANTDA3(CanNum)=Buf->CANTDA; CANTDB3(CanNum)=Buf->CANTDB; } else { status = 0x01; } break; default: status = 0x01; break; } if (TPM == 1) { CANMOD(CanNum).Bits.TPM_BIT =1; } return(status); } void __irq can_isr(void) { uint32 j = 0; uCANICR k; uint8 tmp=0;
//ON_TCAN_LED(); if (CANLUTerr.Word != 0 ) //LUT Error Program { //add or modify code j = CANLUTerrAd.Word; }
for (j=0; j<CAN_MAX_NUM; j++) { k = CANICR(j); if (k.Bits.RI_BIT != 0) { //add code RCAN_LEDGlint(); WriteCANRcvCyBuf(j); //CAN接收中断 } if (k.Bits.TI1_BIT != 0) { //add code //COMM_LEDGlint(); //CANCMR(j).Bits.TR_BIT = 1; } if (k.Bits.TI2_BIT != 0) { //add code } if (k.Bits.TI3_BIT != 0) { //add code } if (k.Bits.BEI_BIT != 0) { //uint32 i = 0; //add code //erro = k.Bits.ERRBIT_BIT; tmp = k.Bits.ERRC_BIT; COMM_LEDGlint();/*led闪烁,取反,该灯一直闪烁不停*/ /* switch (tmp) { case 2: TCAN_LEDGlint(); break; default: COMM_LEDGlint(); break; } */ CanBufOffLinePrg(j); } if (k.Bits.ALI_BIT != 0) { //add code //ON_TCAN_LED(); } if (k.Bits.EPI_BIT != 0) { //add code //ON_TCAN_LED(); } if (k.Bits.WUI_BIT != 0) { //add code }
if (k.Bits.DOI_BIT != 0) { //add code ClrCanDataOver(j); }
} //ON_TCAN_LED(); VICVectAddr = 0x00; }
不知道问题出在哪,收,发节点一直进总线错误中断, |