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

📄 can.c

📁 Keil 下编译的基于mcs51单片机的PCA82C250CAN收发器控制的程序源代码。
💻 C
📖 第 1 页 / 共 3 页
字号:
            if((MCR_temp & 0x03)==0x02){temp_bit=1;} //if INTPND=1 then set temp_bit
            if((MCR_temp & 0x03)==0x01){temp_bit=0;} //if INTPND=0 then clear temp_bit
            break;
        case RMTPND:
            MCR_temp=CAN_OBJ[ObjNr-1].MCR1;        //read MCR1
            if((MCR_temp & 0xc0)==0x80){temp_bit=1;} //if RMTPND=1 then set temp_bit
            if((MCR_temp & 0xc0)==0x40){temp_bit=0;} //if RMTPND=0 then clear temp_bit
            break;
        case TXRQ:
            MCR_temp=CAN_OBJ[ObjNr-1].MCR1;        //read MCR1
            if((MCR_temp & 0x30)==0x20){temp_bit=1;} //if TXRQ=1 then set temp_bit
            if((MCR_temp & 0x30)==0x10){temp_bit=0;} //if TXRQ=0 then clear temp_bit
            break;
        case CPUUPD:
            MCR_temp=CAN_OBJ[ObjNr-1].MCR1;        //read MCR1
            if((MCR_temp & 0x0c)==0x08){temp_bit=1;} //if CPUUPD=1 then set temp_bit
            if((MCR_temp & 0x0c)==0x04){temp_bit=0;} //if CPUUPD=0 then clear temp_bit
            break;
        case NEWDAT:
            MCR_temp=CAN_OBJ[ObjNr-1].MCR1;        //read MCR1
            if((MCR_temp & 0x03)==0x02){temp_bit=1;} //if NEWDAT=1 then set temp_bit
            if((MCR_temp & 0x03)==0x01){temp_bit=0;} //if NEWDAT=0 then clear temp_bit
            break;
    }
    return (temp_bit);
}
*/
//****************************************************************************
// @Function      void set_msg_MCR(unsigend char ObjNr,MCR_BIT,bit bit_value)
//
//----------------------------------------------------------------------------
// @Description   This function set or reset one bit of the message object's control 
//                 register.
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    Number of the message object (1-15);
//                Bit location of the message control register; 
//                Bit value that will be set.
//----------------------------------------------------------------------------
// @Date          01-2-24 12:51:13
//
//****************************************************************************


//modify one bit of mesage object control regist
void set_msg_MCR(ObjNr,MCR_BIT,BIT_VALUE) 
        unsigned char ObjNr;             //message object n(1~15).
        unsigned char MCR_BIT;           //MSGVAL,TXIE,RXIE etc.
        bit BIT_VALUE;                   //bit value 0 or 1.
{
    switch(MCR_BIT)
    {
        case MSGVAL:
            if(BIT_VALUE)
            {
               CAN_OBJ[ObjNr-1].MCR0=0xbf;
            }     
            else
            {
               CAN_OBJ[ObjNr-1].MCR0=0x7f;   
            }
            break;
        case TXIE:
            if(BIT_VALUE)
            {
               CAN_OBJ[ObjNr-1].MCR0=0xef;
            }     
            else
            {
               CAN_OBJ[ObjNr-1].MCR0=0xdf;   
            }
            break;
        case RXIE:
            if(BIT_VALUE)
            {
               CAN_OBJ[ObjNr-1].MCR0=0xfb;
            }     
            else
            {
               CAN_OBJ[ObjNr-1].MCR0=0xf7;   
            }
            break;
        case INTPND:
            if(BIT_VALUE)
            {
               CAN_OBJ[ObjNr-1].MCR0=0xfe;
            }     
            else
            {
               CAN_OBJ[ObjNr-1].MCR0=0xfd;   
            }
            break;
         case RMTPND:
            if(BIT_VALUE)
            {
               CAN_OBJ[ObjNr-1].MCR1=0xbf;
            }     
            else
            {
               CAN_OBJ[ObjNr-1].MCR1=0x7f;   
            }
            break;
        case TXRQ:
            if(BIT_VALUE)
            {
               CAN_OBJ[ObjNr-1].MCR1=0xef;
            }     
            else
            {
               CAN_OBJ[ObjNr-1].MCR1=0xdf;   
            }
             break;
        case CPUUPD:
            if(BIT_VALUE)
            {
               CAN_OBJ[ObjNr-1].MCR1=0xfb;
            }     
            else
            {
               CAN_OBJ[ObjNr-1].MCR1=0xf7;   
            }
            break;
        case NEWDAT:
            if(BIT_VALUE)
            {
               CAN_OBJ[ObjNr-1].MCR1=0xfe;
            }     
            else
            {
               CAN_OBJ[ObjNr-1].MCR1=0xfd;   
            }
            break;
        default:
            break;
        
    }
}
//未使用函数
//****************************************************************************
// @Function      void CAN_vConfigMsgObj(unsigned char ObjNr, TCAN_Obj *pstObj)
//
//----------------------------------------------------------------------------
// @Description   This function sets up the message objects. This includes
//                the 8 data bytes, the identifier (11- or 29-bit), the data 
//                number (0-7 bytes) and the XTD-bit.
//                The message is not sent; for this the function
//                CAN_vTransmit must be called.
//                
//                The structure of the "software" message object is defined in the 
//                header file CAN.H (see TCAN_Obj).
//
//----------------------------------------------------------------------------
// @Returnvalue   none
//
//----------------------------------------------------------------------------
// @Parameters    Number of the message object to be configured (1-15)
// @Parameters    Pointer on a message object
//
//----------------------------------------------------------------------------
// @Date          01-2-24 12:51:13
//
//****************************************************************************
//LAC:configure a "hardware "object which in CAN controller with a "software"
//message object which maybe in MCU
/*void CAN_vConfigMsgObj(unsigned char ObjNr, TCAN_Obj *pstObj)
{
  unsigned char i;
  unsigned long v;

  CAN_OBJ[ObjNr-1].MCR1 = 0xfb;   // set CPUUPD, reset MSGVAL
  CAN_OBJ[ObjNr-1].MCR0 = 0x7f;  

  if(pstObj->MsgCfg & 0x04)     // extended identifier
  {
    v = 0x00000000;
    v = (pstObj->Identifier & 0x0000001f);         // ID  4.. 0
    CAN_OBJ[ObjNr-1].LAR1  = v << 3;
    v = 0x00000000;
    v = (pstObj->Identifier & 0x00001fe0) >>  5;   // ID 12.. 5
    CAN_OBJ[ObjNr-1].LAR0  = (v & 0x00000ff);

    v = 0x00000000;
    v = (pstObj->Identifier & 0x001fe000) >>  13;  // ID 13..20
    CAN_OBJ[ObjNr-1].UAR1  = (v & 0x00000ff);
    v = 0x00000000;
    v = (pstObj->Identifier & 0x1fe00000) >> 21;   // ID 21..28
    CAN_OBJ[ObjNr-1].UAR0  = (v & 0x00000ff);
  }
  else                               // standard identifier
  {
    CAN_OBJ[ObjNr-1].LAR1  = 0x00;
    CAN_OBJ[ObjNr-1].LAR0  = 0x00;

    v = 0x00000000;
    v = (pstObj->Identifier & 0x00000007) << 5;    // ID 18..20
    CAN_OBJ[ObjNr-1].UAR1  = v;
    v = 0x00000000;
    v = (pstObj->Identifier & 0x000007f8) >> 3;    // ID 21..28
    CAN_OBJ[ObjNr-1].UAR0  = v;
  }

  if(CAN_OBJ[ObjNr-1].MCFG & 0x08)   // if transmit direction
  {
    CAN_OBJ[ObjNr-1].MCFG = pstObj->MsgCfg | 0x08;

    for(i = 0; i < (pstObj->MsgCfg & 0xf0) >> 4;i++)
    {
      CAN_OBJ[ObjNr-1].Data[i] = pstObj->ObjectData[i];
    }
    CAN_OBJ[ObjNr-1].MCR1  = 0xf6;  // set NEWDAT, reset CPUUPD, set MSGVAL
    CAN_OBJ[ObjNr-1].MCR0  = 0xbf;  
  }
  else                              // if receive direction
  {
    CAN_OBJ[ObjNr-1].MCFG = pstObj->MsgCfg & 0xf7;

    CAN_OBJ[ObjNr-1].MCR1  = 0xf7;  // reset CPUUPD, set MSGVAL
    CAN_OBJ[ObjNr-1].MCR0  = 0xbf; 
  }

  CAN_OBJ[ObjNr-1].Customer = pstObj->ubUser;
}
//****************************************************************************
// @Function      bit CAN_bMsgLost(unsigned char ObjNr)
//
//----------------------------------------------------------------------------
// @Description   If a RECEIVE OBJECT receives new data before the old object
//                has been read, the old object is lost. The CAN controller
//                indicates this by setting the message lost bit (MSGLST).
//                This function returns the status of this bit.
//                
//                Note:
//                This function resets the message lost bit (MSGLST).
//
//----------------------------------------------------------------------------
// @Returnvalue   1 the message object has lost a message, else 0
//
//----------------------------------------------------------------------------
// @Parameters    Number of the message object (1-15)
//
//----------------------------------------------------------------------------
// @Date          01-2-24 12:51:13
//
//****************************************************************************

bit CAN_bMsgLost(unsigned char ObjNr)
{
  bit bReturn;

  bReturn = 0;
  if((CAN_OBJ[ObjNr-1].MCR1 & 0x0c) == 0x08)  // if set MSGLST 
  {
    bReturn = 1;
    CAN_OBJ[ObjNr-1].MCR1 = 0xF7;
  }
  return(bReturn);
}

//****************************************************************************
// @Function      bit CAN_bDelMsgObj(unsigned char ObjNr)
//
//----------------------------------------------------------------------------
// @Description   This function marks the selected message object as not valid.
//                This means that this object cannot be sent or received.
//                If the selected object is busy (meaning the object is
//                transmitting a message or has received a new message) this
//                function returns the value "0" and the object is not deleted.
//
//----------------------------------------------------------------------------
// @Returnvalue   1 the message object was deleted, else 0
//
//----------------------------------------------------------------------------
// @Parameters    Number of the message object (1-15)
//
//----------------------------------------------------------------------------
// @Date          01-2-24 12:51:13
//
//****************************************************************************

bit CAN_bDelMsgObj(unsigned char ObjNr)
{
  bit bReturn;

  bReturn = 0;
  if(!(CAN_OBJ[ObjNr-1].MCR1 & 0xa2))  // if set RMTPND, TXRQ or NEWDAT
  {
    CAN_OBJ[ObjNr-1].MCR0 = 0x7f;      // reset MSGVAL
    bReturn = 1;
  }
  return(bReturn);
}


//****************************************************************************
// @Function      void CAN_vReleaseObj(unsigned char ObjNr)
//
//----------------------------------------------------------------------------
// @Description   This function resets the NEWDAT flag of the selected RECEIVE
//                OBJECT, so that the CAN controller have access to it.
//                This function must be called if the function CAN_bNewData
//                detects, that new data are present in the message object and
//                the actual data have been read by calling the function
//                CAN_vGetMsgObj. 
//
//----------------------------------------------------------------------------
// @Returnvalue   none
//
//----------------------------------------------------------------------------
// @Parameters    Number of the message object (1-15)
//
//----------------------------------------------------------------------------
// @Date          01-2-24 12:51:13
//
//****************************************************************************

void CAN_vReleaseObj(unsigned char ObjNr)
{
  CAN_OBJ[ObjNr-1].MCR1 = 0xFD;               // reset NEWDAT
}*/

⌨️ 快捷键说明

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