📄 can.c
字号:
//
//----------------------------------------------------------------------------
// @Parameters Number of the message object (1-15)
//
//----------------------------------------------------------------------------
// @Date 01-2-24 12:51:13
//
//****************************************************************************
/*
bit CAN_bNewData(unsigned char ObjNr)
{
bit bReturn;
bReturn = 0;
if((CAN_OBJ[ObjNr-1].MCR1 & 0x03) == 0x02) // if NEWDAT
{
bReturn = 1;
}
return(bReturn);
}
*/
/*
//****************************************************************************
// @Function void CAN_vGetMsgObj(unsigned char ObjNr, unsigned char *BufferPointer)
//
//----------------------------------------------------------------------------
// @Description This function get the data block of the chosen message object.
//
//----------------------------------------------------------------------------
// @Returnvalue none
//
//----------------------------------------------------------------------------
// @Parameters Number of message object to be read (1-15)
// @Parameters Pointer to a buffer to be filled by this function.
//
//----------------------------------------------------------------------------
// @Date 01-2-24 12:51:13
//
//****************************************************************************/
/*
void CAN_vGetMsgObj(unsigned char ObjNr,unsigned char * BufferPointer)
{
unsigned char i;
for(i = 0; i < (CAN_OBJ[ObjNr-1].MCFG & 0xf0) >> 4; i++)
{
* (BufferPointer++)= CAN_OBJ[ObjNr-1].Data[i];//get the data block
}
if(ObjNr < 15 || (CAN_OBJ[ObjNr-1].MCR0 & 0x0c) != 0x08)
{
CAN_OBJ[ObjNr-1].MCR1 = 0xfd; // reset NEWDAT
}
}
*/
//****************************************************************************
// @Function void CAN_vStoreMsgObj(unsigned char ObjNr,unsigned char *BasePointer)
//
//----------------------------------------------------------------------------
// @Description This function store the data block of the chosen message object
// to the buffer specified by BufferPointer and identifier
//
//----------------------------------------------------------------------------
// @Returnvalue none
//
//----------------------------------------------------------------------------
// @Parameters ObjNr---Number of message object to be read (1-15)
// BasePointer----base pointer of the data buffer.
//----------------------------------------------------------------------------
// @Date 01-10-30 9:51:13
//
//****************************************************************************/
void CAN_vStoreMsgObj(unsigned char ObjNr,unsigned char * BasePointer)
{
unsigned char i;
unsigned int id;
id=CAN_OBJ[ObjNr-1].UAR0; //id's high 8 bit
id=(id<<8 | CAN_OBJ[ObjNr-1].UAR1)>>5;//change id to int
//##############################################
//BasePointer+=((id-1)<<3); //address the buffer by id
//##################################################
BasePointer+=(id<<3); //address the buffer by id
for(i = 0; i <8; i++)
{
* BasePointer= CAN_OBJ[ObjNr-1].Data[i];//get the data block
BasePointer++;
}
if(ObjNr < 15 || (CAN_OBJ[ObjNr-1].MCR0 & 0x0c) != 0x08)
{
CAN_OBJ[ObjNr-1].MCR1 = 0xfd; // reset NEWDAT
}
}
//****************************************************************************
// @Function void CAN_vStoreID(unsigned char ObjNr,unsigned char *BasePointer)
//
//----------------------------------------------------------------------------
// @Description This function store identifier of the chosen message object
// to the buffer specified by BufferPointer and identifier
//
//----------------------------------------------------------------------------
// @Returnvalue none
//
//----------------------------------------------------------------------------
// @Parameters ObjNr---Number of message object to be read (1-15)
// BasePointer----base pointer of the data buffer.
//----------------------------------------------------------------------------
// @Date 01-10-30 9:51:13
//
//****************************************************************************/
void CAN_vStoreID(unsigned char ObjNr,unsigned char * BasePointer)
{
unsigned int id;
id=CAN_OBJ[ObjNr-1].UAR0; //id's high 8 bit
id=(id<<8 | CAN_OBJ[ObjNr-1].UAR1)>>5;//change id to int
if(id<=10)
{
BasePointer+=(id-1);
(*BasePointer)=(unsigned char)id;
}
}
//////////////////////////////与发送有关函数////////////////////////////////////
//****************************************************************************
// @Function bit CAN_bRequestMsgObj(unsigned char ObjNr)
//
//----------------------------------------------------------------------------
// @Description If a TRANSMIT OBJECT is to be reconfigured it must first be
// accessed. The access to the transmit object is exclusive.
// This function checks whether the choosen message object is
// still executing a transmit request, or if the object can
// be accessed exclusively.
// After the message object is reserved, it can be reconfigured
// by using the function CAN_vConfigMsgObj or CAN_vLoadData.
// Both functions enable access to the object for the CAN
// controller.
// By calling the function CAN_vTransmit transfering of data
// is started.
//
//----------------------------------------------------------------------------
// @Returnvalue 0 message object is busy (a transfer is actice), else 1
//
//----------------------------------------------------------------------------
// @Parameters Number of the message object (1-14)
//
//----------------------------------------------------------------------------
// @Date 01-2-24 12:51:13
//
//****************************************************************************
bit CAN_bRequestMsgObj(unsigned char ObjNr)
{
bit bReturn;
bReturn = 0;
if((CAN_OBJ[ObjNr-1].MCR1 & 0x30) == 0x10) // if TXRQ==0
{
CAN_OBJ[ObjNr-1].MCR1 = 0xfb; // set CPUUPD
bReturn = 1;
}
return(bReturn);
}
/*
//****************************************************************************
// @Function void CAN_vLoadData(unsigned char ObjNr, unsigned char *pubBuffer)
//
//----------------------------------------------------------------------------
// @Description If a message 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_bRequestMsgObj before calling this function. This
// prevents the CAN controller from working with invalid data.
//
//----------------------------------------------------------------------------
// @Returnvalue none
//
//----------------------------------------------------------------------------
// @Parameters Number of the message object to be configured (1-15)
// @Parameters Pointer on a data buffer
//
//----------------------------------------------------------------------------
// @Date 01-2-24 12:51:13
//
//****************************************************************************
void CAN_vLoadData(unsigned char ObjNr, unsigned char *pubBuffer)
{
unsigned char i;
CAN_OBJ[ObjNr-1].MCR1=0xfa;//set CPUUPD and NEWDAT
for(i=0;i<(CAN_OBJ[ObjNr-1].MCFG & 0xf0)>>4;i++)
{
CAN_OBJ[ObjNr-1].Data[i]= * (pubBuffer++);
}
CAN_OBJ[ObjNr-1].MCR1 = 0xf7; // reset CPUUPD
}
*/
//****************************************************************************
// @Function void CAN_vConfigMsgObj(unsigned char ObjNr,unsigned int id,unsigned char pubBuffer)
//
//----------------------------------------------------------------------------
// @Description This function sets up the message objects. This includes
// the 8 data bytes, the identifier (11- or 29-bit), number (0-7 bytes) and the XTD-bit.
// The message is not sent; for this the function
// CAN_vTransmit must be called.
//
//----------------------------------------------------------------------------
// @Returnvalue none
//
//----------------------------------------------------------------------------
// @Parameters ObjNr---Number of the message object to be configured (1-15)
// id---identifier;
// pubBuffer---Pointer on a message object's data;
//
//----------------------------------------------------------------------------
// @Date 01-2-24 12:51:13
//
//****************************************************************************
void CAN_vConfigMsgObj(unsigned char ObjNr,unsigned int id,unsigned char *pubBuffer)
{
unsigned char i;
CAN_OBJ[ObjNr-1].MCR1=0xfa;//set CPUUPD and NEWDAT
CAN_OBJ[ObjNr-1].MCR0=0x7f;
///////configure identifier//////////
id=id<<5;
CAN_OBJ[ObjNr-1].UAR1=(unsigned char)(id % 256);
CAN_OBJ[ObjNr-1].UAR0=(unsigned char)(id/256);
///////load data////////////
for(i=0;i<8;i++)
{
CAN_OBJ[ObjNr-1].Data[i]= * pubBuffer;
pubBuffer++;
wdt_refresh();
cs5045=0;
_nop_();
_nop_();
cs5045=1;
}
CAN_OBJ[ObjNr-1].MCR1 = 0xf7; // reset CPUUPD
CAN_OBJ[ObjNr-1].MCR0 = 0xbf;
}
//****************************************************************************
// @Function void CAN_vTransmit(unsigned char ObjNr)
//
//----------------------------------------------------------------------------
// @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.
// send a data frame or a romote frame
//----------------------------------------------------------------------------
// @Returnvalue none
//
//----------------------------------------------------------------------------
// @Parameters Number of the message object to be sent (1-14)
//
//----------------------------------------------------------------------------
// @Date 01-2-24 12:51:13
//
//****************************************************************************
void CAN_vTransmit(unsigned char ObjNr)
{
CAN_OBJ[ObjNr-1].MCR1 = 0xE7; // set TXRQ, reset CPUUPD
}
//****************************************************************************
//function: void error_warn(void);
//description: communication show ,if error warn
//parameter: none
//return: none
//****************************************************************************
//void error_warn(void)
//{
// ;
//}
//////////////////////////信息控制寄存器位操作函数///////////////////////////////
//****************************************************************************
// @Function bit read_msg_MCR(unsigend char ObjNr,MCR_BIT)
//
//----------------------------------------------------------------------------
// @Description This function read one bit of the message object's control
// register.
//----------------------------------------------------------------------------
// @Returnvalue Bit value maybe "1" or "0"
//
//----------------------------------------------------------------------------
// @Parameters Number of the message object (1-15);
// Bit location of the message control register;
//----------------------------------------------------------------------------
// @Date 01-2-24 12:51:13
//
//****************************************************************************
/*
bit read_msg_MCR(ObjNr,MCR_BIT)
unsigned char ObjNr;
unsigned char MCR_BIT;
{
unsigned char MCR_temp;
bit temp_bit;
switch(MCR_BIT)
{
case MSGVAL:
MCR_temp=CAN_OBJ[ObjNr-1].MCR0; //read MCR0
if((MCR_temp & 0xc0)==0x80){temp_bit=1;} //if MSGVAL=1 then set temp_bit
if((MCR_temp & 0xc0)==0x40){temp_bit=0;} //if MSGVAL=0 then clear temp_bit
break;
case TXIE:
MCR_temp=CAN_OBJ[ObjNr-1].MCR0; //read MCR0
if((MCR_temp & 0x30)==0x20){temp_bit=1;} //if TXIE=1 then set temp_bit
if((MCR_temp & 0x30)==0x10){temp_bit=0;} //if TXIE=0 then clear temp_bit
break;
case RXIE:
MCR_temp=CAN_OBJ[ObjNr-1].MCR0; //read MCR0
if((MCR_temp & 0x0c)==0x08){temp_bit=1;} //if RXIE=1 then set temp_bit
if((MCR_temp & 0x0c)==0x04){temp_bit=0;} //if RXIE=0 then clear temp_bit
break;
case INTPND:
MCR_temp=CAN_OBJ[ObjNr-1].MCR0; //read MCR0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -