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

📄 can.c

📁 英飞凌C166之XC164CS双CAN通讯程序
💻 C
📖 第 1 页 / 共 2 页
字号:
  {
    CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xfbff;               // set CPUUPD 
    ubReturn = 1;
  }
  return(ubReturn);

} //  End of function CAN_ubRequestMsgObj


//****************************************************************************
// @Function      ubyte CAN_ubNewData(ubyte ubObjNr) 
//
//----------------------------------------------------------------------------
// @Description   This function checks whether the selected RECEIVE OBJECT 
//                has received a new message. If so the function returns the 
//                value '1'.
//
//----------------------------------------------------------------------------
// @Returnvalue   1 the message object has received a new message, else 0
//
//----------------------------------------------------------------------------
// @Parameters    ubObjNr: 
//                Number of the message object (0-31)
//
//----------------------------------------------------------------------------
// @Date          2007-5-25
//
//****************************************************************************

// USER CODE BEGIN (NewData,1)

// USER CODE END

ubyte CAN_ubNewData(ubyte ubObjNr)
{
  ubyte ubReturn;

  ubReturn = 0;
  if((CAN_HWOBJ[ubObjNr].uwMSGCTR & 0x0300) == 0x0200)  // if NEWDAT
  {
    ubReturn = 1;
  }
  return(ubReturn);

} //  End of function CAN_ubNewData


//****************************************************************************
// @Function      void CAN_vTransmit(ubyte ubObjNr) 
//
//----------------------------------------------------------------------------
// @Description   This function triggers the CAN controller to send the 
//                selected message.
//                If the selected message object is a TRANSMIT OBJECT then 
//                this function triggers the sending of a data frame. If 
//                however the selected message object is a RECEIVE OBJECT 
//                this function triggers the sending of a remote frame.
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    ubObjNr: 
//                Number of the message object (0-31)
//
//----------------------------------------------------------------------------
// @Date          2007-5-25
//
//****************************************************************************

// USER CODE BEGIN (Transmit,1)

// USER CODE END

void CAN_vTransmit(ubyte ubObjNr)
{
  CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xe7ff;  // set TXRQ, reset CPUUPD

} //  End of function CAN_vTransmit


//****************************************************************************
// @Function      void CAN_vConfigMsgObj(ubyte ubObjNr, TCAN_SWObj *pstObj) 
//
//----------------------------------------------------------------------------
// @Description   This function sets up the message objects. This includes 
//                the 8 data bytes, the identifier (11- or 29-bit), the 
//                acceptance mask (11- or 29-bit), the data number (0-8 
//                bytes), the frame counter value and the XTD-bit (standard 
//                or extended identifier).  The direction bit (DIR), the NODE 
//                bit and the RMM (remote monitoring) bit can not be changed. 
//                The message is not sent; for this the function 
//                CAN_vTransmit must be called.
//                
//                The structure of the SW message object is defined in the 
//                header file CAN.H (see TCAN_SWObj).
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    ubObjNr: 
//                Number of the message object to be configured (0-31)
// @Parameters    *pstObj: 
//                Pointer on a message object
//
//----------------------------------------------------------------------------
// @Date          2007-5-25
//
//****************************************************************************

// USER CODE BEGIN (ConfigMsgObj,1)

// USER CODE END

void CAN_vConfigMsgObj(ubyte ubObjNr, TCAN_SWObj *pstObj)
{
  ubyte i;

  CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xfb7f;     // set CPUUPD, reset MSGVAL

  if(pstObj->uwMsgCfg & 0x0004)             // extended identifier
  {
    CAN_HWOBJ[ubObjNr].uwMSGCFG |= 0x0004;
    CAN_HWOBJ[ubObjNr].ulCANAR   = pstObj->ulID ;
    CAN_HWOBJ[ubObjNr].ulCANAMR  = pstObj->ulMask ;
  }
  else                                      // standard identifier
  {
    CAN_HWOBJ[ubObjNr].uwMSGCFG &= ~(uword)0x0004;
    CAN_HWOBJ[ubObjNr].ulCANAR   = pstObj->ulID << 18;
    CAN_HWOBJ[ubObjNr].ulCANAMR  = pstObj->ulMask << 18;
  }

  CAN_HWOBJ[ubObjNr].uwCounter = pstObj->uwCounter;

  CAN_HWOBJ[ubObjNr].uwMSGCFG  = (CAN_HWOBJ[ubObjNr].uwMSGCFG & 0x000f) | (pstObj->uwMsgCfg & 0x00f0);

  if(CAN_HWOBJ[ubObjNr].uwMSGCFG & 0x0008)  // if transmit direction
  {
    for(i = 0; i < (pstObj->uwMsgCfg & 0x00f0) >> 4; i++)
    {
      CAN_HWOBJ[ubObjNr].ubData[i] = pstObj->ubData[i];
    }
    CAN_HWOBJ[ubObjNr].uwMSGCTR  = 0xf6bf;  // set NEWDAT, reset CPUUPD, 
  }                                         // set MSGVAL
  else                                      // if receive direction
  {
    CAN_HWOBJ[ubObjNr].uwMSGCTR  = 0xf7bf;  // reset CPUUPD, set MSGVAL
  }

} //  End of function CAN_vConfigMsgObj


//****************************************************************************
// @Function      void CAN_vLoadData(ubyte ubObjNr, ubyte *pubData) 
//
//----------------------------------------------------------------------------
// @Description   If a hardware TRANSMIT OBJECT has to be loaded with data 
//                but not with a new identifier, this function may be used 
//                instead of the function CAN_vConfigMsgObj. The message 
//                object should be accessed by calling the function 
//                CAN_ubRequestMsgObj before calling this function. This 
//                prevents the CAN controller from working with invalid data.
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    ubObjNr: 
//                Number of the message object to be configured (0-31)
// @Parameters    *pubData: 
//                Pointer on a data buffer
//
//----------------------------------------------------------------------------
// @Date          2007-5-25
//
//****************************************************************************

// USER CODE BEGIN (LoadData,1)

// USER CODE END

void CAN_vLoadData(ubyte ubObjNr, ubyte *pubData)
{
  ubyte i;

  CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xfaff;       // set CPUUPD and NEWDAT

  for(i = 0; i < (CAN_HWOBJ[ubObjNr].uwMSGCFG & 0xf0) >> 4; i++)
  {
    CAN_HWOBJ[ubObjNr].ubData[i] = *(pubData++);
  }

  CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xf7ff;       // reset CPUUPD

} //  End of function CAN_vLoadData


//****************************************************************************
// @Function      void CAN_viSRN0(void) 
//
//----------------------------------------------------------------------------
// @Description   This is the interrupt service routine for the Service 
//                Request Node 0 of the TwinCAN module.
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    None
//
//----------------------------------------------------------------------------
// @Date          2007-5-25
//
//****************************************************************************

// USER CODE BEGIN (SRN0,1)

// USER CODE END

void CAN_viSRN0(void) interrupt CAN_SRN0INT
{
  uword uwStatusA;

  // USER CODE BEGIN (SRN0,2)

  // USER CODE END

  while((( ((ulong)CAN_TXIPNDH << 16) + CAN_TXIPNDL) & 0x00000001) || (CAN_ASR & 0x0018))
  {

    // status change interrupt of node A

    uwStatusA = CAN_ASR;
    if (uwStatusA & 0x0008)  // if TXOK
    {
      // Indicates that a message has been transmitted successfully
      // (error free and acknowledged by at least one other node).

      uwStatusA &= 0xfff7;
      CAN_ASR    = uwStatusA;    // reset TXOK

      // USER CODE BEGIN (SRN0_NODEA,3)

      // USER CODE END
    }

    if (uwStatusA & 0x0010)  // if RXOK
    {
      // Indicates that a message has been received successfully.

      uwStatusA &= 0xffef;
      CAN_ASR    = uwStatusA;    // reset RXOK

      // USER CODE BEGIN (SRN0_NODEA,4)

      // USER CODE END
    }


    // USER CODE BEGIN (SRN0_NODEA,13)

    // USER CODE END



    // message object 0 interrupt

    if((CAN_HWOBJ[0].uwMSGCTR & 0x0003) == 0x0002)         // if INTPND 
    {
      if(CAN_TXIPNDL & CAN_TXIPNDL_TXIPND0)   // message object 0 transmit interrupt
      {

        // The transmission of the last message object
        // was successful.

        // USER CODE BEGIN (SRN0_OBJ0,4)

        // USER CODE END

        CAN_HWOBJ[0].uwMSGCTR = 0xfdff;      // reset NEWDAT

      }  // End of TXIPND0

      CAN_HWOBJ[0].uwMSGCTR = 0xfffd;        // reset INTPND

      // USER CODE BEGIN (SRN0_OBJ0,6)

      // USER CODE END

    }


    // USER CODE BEGIN (SRN0,3)

    // USER CODE END


  }  // End of while()

  // USER CODE BEGIN (SRN0,7)

  // USER CODE END

} //  End of function CAN_viSRN0


//****************************************************************************
// @Function      void CAN_viSRN1(void) 
//
//----------------------------------------------------------------------------
// @Description   This is the interrupt service routine for the Service 
//                Request Node 1 of the TwinCAN module.
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    None
//
//----------------------------------------------------------------------------
// @Date          2007-5-25
//
//****************************************************************************

// USER CODE BEGIN (SRN1,1)

// USER CODE END

void CAN_viSRN1(void) interrupt CAN_SRN1INT
{
  uword uwStatusB;

  // USER CODE BEGIN (SRN1,2)

  // USER CODE END

  while((( ((ulong)CAN_RXIPNDH << 16) + CAN_RXIPNDL) & 0x00000002) || (CAN_BSR & 0x0018))
  {

    // status change interrupt of node B

    uwStatusB = CAN_BSR;
    if (uwStatusB & 0x0008)  // if TXOK
    {
      // Indicates that a message has been transmitted successfully
      // (error free and acknowledged by at least one other node).

      uwStatusB &= 0xfff7;
      CAN_BSR    = uwStatusB;    // reset TXOK

      // USER CODE BEGIN (SRN1_NODEB,3)

      // USER CODE END
    }

    if (uwStatusB & 0x0010)  // if RXOK
    {
      // Indicates that a message has been received successfully.

      uwStatusB &= 0xffef;
      CAN_BSR    = uwStatusB;    // reset RXOK

      // USER CODE BEGIN (SRN1_NODEB,4)

      // USER CODE END
    }


    // USER CODE BEGIN (SRN1_NODEB,13)

    // USER CODE END



    // message object 1 interrupt

    if((CAN_HWOBJ[1].uwMSGCTR & 0x0003) == 0x0002)         // if INTPND 
    {
      if(CAN_RXIPNDL & CAN_RXIPNDL_RXIPND1)   // message object 1 receive interrupt
      {

        if((CAN_HWOBJ[1].uwMSGCTR & 0x0300) == 0x0200)     // if NEWDAT is set
        {

          if ((CAN_HWOBJ[1].uwMSGCTR & 0x0c00) == 0x0800)  // if MSGLST is set
          {
            // Indicates that the CAN controller has stored a new 
            // message into this object, while NEWDAT was still set,
            // ie. the previously stored message is lost.

            CAN_HWOBJ[1].uwMSGCTR = 0xf7ff;  // reset MSGLST

            // USER CODE BEGIN (SRN1_OBJ1,1)

            // USER CODE END
          }
          else
          {
            // The CAN controller has stored a new message
            // into this object.

            // USER CODE BEGIN (SRN1_OBJ1,2)

            // USER CODE END
          }

          CAN_HWOBJ[1].uwMSGCTR = 0xfdff;    // reset NEWDAT
        }

      }  // End of RXIPND1


      CAN_HWOBJ[1].uwMSGCTR = 0xfffd;        // reset INTPND

      // USER CODE BEGIN (SRN1_OBJ1,6)

      // USER CODE END

    }


    // USER CODE BEGIN (SRN1,3)

    // USER CODE END


  }  // End of while()

  // USER CODE BEGIN (SRN1,7)

  // USER CODE END

} //  End of function CAN_viSRN1



// USER CODE BEGIN (CAN_General,10)

// USER CODE END

⌨️ 快捷键说明

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