⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 can.lst

📁 本程序是针对Infineon公司的XC167CI处理器而编写的CAN网络程序。CAN(控制局域网)协议是一种广泛的应用于汽车电子的网络协议。本程序建立了三个CAN节点
💻 LST
📖 第 1 页 / 共 4 页
字号:
  490         // @Parameters    ubObjNr: 
  491         //                Number of the message object (0-31)
  492         //
  493         //----------------------------------------------------------------------------
  494         // @Date          2006-12-10
  495         //
  496         //****************************************************************************
  497         
  498         // USER CODE BEGIN (NewData,1)
  499         
  500         // USER CODE END
  501         
  502         ubyte CAN_ubNewData(ubyte ubObjNr)
  503         {
  504  1        ubyte ubReturn;
  505  1      
  506  1        ubReturn = 0;
  507  1        if((CAN_HWOBJ[ubObjNr].uwMSGCTR & 0x0300) == 0x0200)  // if NEWDAT
  508  1        {
  509  2          ubReturn = 1;
  510  2        }
  511  1        return(ubReturn);
  512  1      
  513  1      } //  End of function CAN_ubNewData
  514         
  515         
  516         //****************************************************************************
  517         // @Function      void CAN_vTransmit(ubyte ubObjNr) 
  518         //
  519         //----------------------------------------------------------------------------
  520         // @Description   This function triggers the CAN controller to send the 
  521         //                selected message.
  522         //                If the selected message object is a TRANSMIT OBJECT then 
  523         //                this function triggers the sending of a data frame. If 
  524         //                however the selected message object is a RECEIVE OBJECT 
  525         //                this function triggers the sending of a remote frame.
  526         //
  527         //----------------------------------------------------------------------------
  528         // @Returnvalue   None
  529         //
  530         //----------------------------------------------------------------------------
  531         // @Parameters    ubObjNr: 
  532         //                Number of the message object (0-31)
  533         //
  534         //----------------------------------------------------------------------------
  535         // @Date          2006-12-10
  536         //
  537         //****************************************************************************
  538         
  539         // USER CODE BEGIN (Transmit,1)
  540         
  541         // USER CODE END
  542         
  543         void CAN_vTransmit(ubyte ubObjNr)
  544         {
  545  1        CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xe7ff;  // set TXRQ, reset CPUUPD
  546  1      
  547  1      } //  End of function CAN_vTransmit
  548         
  549         
  550         //****************************************************************************
  551         // @Function      void CAN_vConfigMsgObj(ubyte ubObjNr, TCAN_SWObj *pstObj) 
C166 COMPILER V6.04, CAN                                                                   12/10/2006 20:50:28 PAGE 10  

  552         //
  553         //----------------------------------------------------------------------------
  554         // @Description   This function sets up the message objects. This includes 
  555         //                the 8 data bytes, the identifier (11- or 29-bit), the 
  556         //                acceptance mask (11- or 29-bit), the data number (0-8 
  557         //                bytes), the frame counter value and the XTD-bit (standard 
  558         //                or extended identifier).  The direction bit (DIR), the NODE 
  559         //                bit and the RMM (remote monitoring) bit can not be changed. 
  560         //                The message is not sent; for this the function 
  561         //                CAN_vTransmit must be called.
  562         //                
  563         //                The structure of the SW message object is defined in the 
  564         //                header file CAN.H (see TCAN_SWObj).
  565         //
  566         //----------------------------------------------------------------------------
  567         // @Returnvalue   None
  568         //
  569         //----------------------------------------------------------------------------
  570         // @Parameters    ubObjNr: 
  571         //                Number of the message object to be configured (0-31)
  572         // @Parameters    *pstObj: 
  573         //                Pointer on a message object
  574         //
  575         //----------------------------------------------------------------------------
  576         // @Date          2006-12-10
  577         //
  578         //****************************************************************************
  579         
  580         // USER CODE BEGIN (ConfigMsgObj,1)
  581         
  582         // USER CODE END
  583         
  584         void CAN_vConfigMsgObj(ubyte ubObjNr, TCAN_SWObj *pstObj)
  585         {
  586  1        ubyte i;
  587  1      
  588  1        CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xfb7f;     // set CPUUPD, reset MSGVAL
  589  1      
  590  1        if(pstObj->uwMsgCfg & 0x0004)             // extended identifier
  591  1        {
  592  2          CAN_HWOBJ[ubObjNr].uwMSGCFG |= 0x0004;
  593  2          CAN_HWOBJ[ubObjNr].ulCANAR   = pstObj->ulID ;
  594  2          CAN_HWOBJ[ubObjNr].ulCANAMR  = pstObj->ulMask ;
  595  2        }
  596  1        else                                      // standard identifier
  597  1        {
  598  2          CAN_HWOBJ[ubObjNr].uwMSGCFG &= ~(uword)0x0004;
  599  2          CAN_HWOBJ[ubObjNr].ulCANAR   = pstObj->ulID << 18;
  600  2          CAN_HWOBJ[ubObjNr].ulCANAMR  = pstObj->ulMask << 18;
  601  2        }
  602  1      
  603  1        CAN_HWOBJ[ubObjNr].uwCounter = pstObj->uwCounter;
  604  1      
  605  1        CAN_HWOBJ[ubObjNr].uwMSGCFG  = (CAN_HWOBJ[ubObjNr].uwMSGCFG & 0x000f) | (pstObj->uwMsgCfg & 0x00f0);
  606  1      
  607  1        if(CAN_HWOBJ[ubObjNr].uwMSGCFG & 0x0008)  // if transmit direction
  608  1        {
  609  2          for(i = 0; i < (pstObj->uwMsgCfg & 0x00f0) >> 4; i++)
  610  2          {
  611  3            CAN_HWOBJ[ubObjNr].ubData[i] = pstObj->ubData[i];
  612  3          }
  613  2          CAN_HWOBJ[ubObjNr].uwMSGCTR  = 0xf6bf;  // set NEWDAT, reset CPUUPD, 
C166 COMPILER V6.04, CAN                                                                   12/10/2006 20:50:28 PAGE 11  

  614  2        }                                         // set MSGVAL
  615  1        else                                      // if receive direction
  616  1        {
  617  2          CAN_HWOBJ[ubObjNr].uwMSGCTR  = 0xf7bf;  // reset CPUUPD, set MSGVAL
  618  2        }
  619  1      
  620  1      } //  End of function CAN_vConfigMsgObj
  621         
  622         
  623         //****************************************************************************
  624         // @Function      void CAN_vLoadData(ubyte ubObjNr, ubyte *pubData) 
  625         //
  626         //----------------------------------------------------------------------------
  627         // @Description   If a hardware TRANSMIT OBJECT has to be loaded with data 
  628         //                but not with a new identifier, this function may be used 
  629         //                instead of the function CAN_vConfigMsgObj. The message 
  630         //                object should be accessed by calling the function 
  631         //                CAN_ubRequestMsgObj before calling this function. This 
  632         //                prevents the CAN controller from working with invalid data.
  633         //
  634         //----------------------------------------------------------------------------
  635         // @Returnvalue   None
  636         //
  637         //----------------------------------------------------------------------------
  638         // @Parameters    ubObjNr: 
  639         //                Number of the message object to be configured (0-31)
  640         // @Parameters    *pubData: 
  641         //                Pointer on a data buffer
  642         //
  643         //----------------------------------------------------------------------------
  644         // @Date          2006-12-10
  645         //
  646         //****************************************************************************
  647         
  648         // USER CODE BEGIN (LoadData,1)
  649         
  650         // USER CODE END
  651         
  652         void CAN_vLoadData(ubyte ubObjNr, ubyte *pubData)
  653         {
  654  1        ubyte i;
  655  1      
  656  1        CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xfaff;       // set CPUUPD and NEWDAT
  657  1      
  658  1        for(i = 0; i < (CAN_HWOBJ[ubObjNr].uwMSGCFG & 0xf0) >> 4; i++)
  659  1        {
  660  2          CAN_HWOBJ[ubObjNr].ubData[i] = *(pubData++);
  661  2        }
  662  1      
  663  1        CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xf7ff;       // reset CPUUPD
  664  1      
  665  1      } //  End of function CAN_vLoadData
  666         
  667         
  668         //****************************************************************************
  669         // @Function      void CAN_viSRN0(void) 
  670         //
  671         //----------------------------------------------------------------------------
  672         // @Description   This is the interrupt service routine for the Service 
  673         //                Request Node 0 of the TwinCAN module.
  674         //
  675         //----------------------------------------------------------------------------
C166 COMPILER V6.04, CAN                                                                   12/10/2006 20:50:28 PAGE 12  

  676         // @Returnvalue   None
  677         //
  678         //----------------------------------------------------------------------------
  679         // @Parameters    None
  680         //
  681         //----------------------------------------------------------------------------
  682         // @Date          2006-12-10
  683         //
  684         //****************************************************************************
  685         
  686         // USER CODE BEGIN (SRN0,1)
  687           unsigned int i;
  688         // USER CODE END
  689         
  690         void CAN_viSRN0(void) interrupt CAN_SRN0INT
  691         {
  692  1        uword uwStatusB;
  693  1      
  694  1        // USER CODE BEGIN (SRN0,2)
  695  1      
  696  1        // USER CODE END
  697  1      
  698  1        while((( ((ulong)CAN_RXIPNDH << 16) + CAN_RXIPNDL) & 0x00000001) || (CAN_BSR & 0x0018))
  699  1        {
  700  2      
  701  2          // status change interrupt of node B
  702  2      
  703  2          uwStatusB = CAN_BSR;
  704  2          if (uwStatusB & 0x0008)  // if TXOK
  705  2          {
  706  3            // Indicates that a message has been transmitted successfully
  707  3            // (error free and acknowledged by at least one other node).
  708  3      
  709  3            uwStatusB &= 0xfff7;
  710  3            CAN_BSR    = uwStatusB;    // reset TXOK
  711  3      
  712  3            // USER CODE BEGIN (SRN0_NODEB,3)
  713  3      
  714  3            // USER CODE END
  715  3          }
  716  2      
  717  2          if (uwStatusB & 0x0010)  // if RXOK
  718  2          {
  719  3            // Indicates that a message has been received successfully.
  720  3      
  721  3            uwStatusB &= 0xffef;
  722  3            CAN_BSR    = uwStatusB;    // reset RXOK
  723  3      
  724  3            // USER CODE BEGIN (SRN0_NODEB,4)
  725  3      
  726  3            // USER CODE END
  727  3          }
  728  2      
  729  2      
  730  2          // USER CODE BEGIN (SRN0_NODEB,13)
  731  2      
  732  2          // USER CODE END
  733  2      
  734  2      
  735  2      
  736  2          // message object 0 interrupt
  737  2      
C166 COMPILER V6.04, CAN                                                                   12/10/2006 20:50:28 PAGE 13  

  738  2          if((CAN_HWOBJ[0].uwMSGCTR & 0x0003) == 0x0002)         // if INTPND 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -