基于CAN总线的通讯子程序.docx
《基于CAN总线的通讯子程序.docx》由会员分享,可在线阅读,更多相关《基于CAN总线的通讯子程序.docx(17页珍藏版)》请在冰豆网上搜索。
![基于CAN总线的通讯子程序.docx](https://file1.bdocx.com/fileroot1/2023-2/9/59efc46b-1e47-4018-8b64-0e15d60c7531/59efc46b-1e47-4018-8b64-0e15d60c75311.gif)
基于CAN总线的通讯子程序
接收程序:
//------------------------------------------------------------------------------
//F04x_CAN1.c
//------------------------------------------------------------------------------
//
//
//DEVICE:
C8051F040
//
//AUTHOR:
LS
//
//TOOLS:
KeilC-compilerandSiliconLabsIDE
//
//
//CAN1.candCAN2.careasimpleexampleofconfiguringaCANnetworkto
//transmitandreceivedataonaCANnetwork,andhowtomoveinformationto
//andfromCANRAMmessageobjects.EachC8051F040-TBCANnodeisconfigured
//tosendamessagewhenit'sP3.7buttonisdepressed/released,witha0x11
//toindicatethebuttonispushed,and0x00whenreleased.Eachnodealsohas
//amessageobjectconfiguredtoreceivemessages.TheC8051teststhe
//receiveddataandwillturnon/offthetargetboard'sLED.Whenonetarget
//isloadedwithCAN2.candtheotherisloadedwithCAN1.c,onetarget
//board'spush-buttonwillcontroltheothertargetboard'sLED,establishing
//asimplecontrollinkviatheCANbusandcanbeobserveddirectlyonthe
//targetboards.
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//Includes
////////////////////////////////////////////////////////////////////////////////
#include//SFRdeclarations
//CANProtocolRegisterIndexforCAN0ADR,fromTABLE18.1oftheC8051F040
//datasheet
////////////////////////////////////////////////////////////////////////////////
#defineCANCTRL0x00//ControlRegister
#defineCANSTAT0x01//Statusregister
#defineERRCNT0x02//ErrorCounterRegister
#defineBITREG0x03//BitTimingRegister
#defineINTREG0x04//InterruptLowByteRegister
#defineCANTSTR0x05//Testregister
#defineBRPEXT0x06//BRPExtensionRegister
////////////////////////////////////////////////////////////////////////////////
//IF1InterfaceRegisters
////////////////////////////////////////////////////////////////////////////////
#defineIF1CMDRQST0x08//IF1CommandRestRegister
#defineIF1CMDMSK0x09//IF1CommandMaskRegister
#defineIF1MSK10x0A//IF1Mask1Register
#defineIF1MSK20x0B//IF1Mask2Register
#defineIF1ARB10x0C//IF1Arbitration1Register
#defineIF1ARB20x0D//IF1Arbitration2Register
#defineIF1MSGC0x0E//IF1MessageControlRegister
#defineIF1DATA10x0F//IF1DataA1Register
#defineIF1DATA20x10//IF1DataA2Register
#defineIF1DATB10x11//IF1DataB1Register
#defineIF1DATB20x12//IF1DataB2Register
////////////////////////////////////////////////////////////////////////////////
//IF2InterfaceRegisters
////////////////////////////////////////////////////////////////////////////////
#defineIF2CMDRQST0x20//IF2CommandRestRegister
#defineIF2CMDMSK0x21//IF2CommandMaskRegister
#defineIF2MSK10x22//IF2Mask1Register
#defineIF2MSK20x23//IF2Mask2Register
#defineIF2ARB10x24//IF2Arbitration1Register
#defineIF2ARB20x25//IF2Arbitration2Register
#defineIF2MSGC0x26//IF2MessageControlRegister
#defineIF2DATA10x27//IF2DataA1Register
#defineIF2DATA20x28//IF2DataA2Register
#defineIF2DATB10x29//IF2DataB1Register
#defineIF2DATB20x2A//IF2DataB2Register
////////////////////////////////////////////////////////////////////////////////
//MessageHandlerRegisters
////////////////////////////////////////////////////////////////////////////////
#defineTRANSREQ10x40//TransmissionRest1Register
#defineTRANSREQ20x41//TransmissionRest2Register
#defineNEWDAT10x48//NewData1Register
#defineNEWDAT20x49//NewData2Register
#defineINTPEND10x50//InterruptPending1Register
#defineINTPEND20x51//InterruptPending2Register
#defineMSGVAL10x58//MessageValid1Register
#defineMSGVAL20x59//MessageValid2Register
typedefunsignedcharuchar;
typedefunsignedintuint;
typedefunsignedlongulong;
unionintchar{
uinttempval;
struct{
ucharhig;
ucharlow;}
bytek;
};
xdataunionintcharrdata[4];
xdataucharsdata[8]={0x32,0x33,0x34,0x35,0xb3,0xb4,0xb5,0xb6};
////////////////////////////////////////////////////////////////////////////////
//GlobalVariables
////////////////////////////////////////////////////////////////////////////////
charMsgNum;
charstatus;
inti;
intMOTwoIndex=0;
intMOOneIndex=0;
intStatusCopy;
intRXbuffer[4];
intTXbuffer[8];
intMsgIntNum;
intTemperature;
sbitBUTTON=P3^7;
sbitLED=P1^6;
sfr16CAN0DAT=0xD8;
sbitP20=P2^0;
sbitP21=P2^1;
sbitWRn=P0^7;
sbitRDn=P0^6;
////////////////////////////////////////////////////////////////////////////////
//FunctionPROTOTYPES
////////////////////////////////////////////////////////////////////////////////
//InitializeMessageObject
voidclear_msg_objects(void);
//voidinit_msg_object_TX(charMsgNum);
voidinit_msg_object_RX(charMsgNum,uintdevID);
voidstart_CAN(void);
//voidtransmit_turn_LED_ON(charMsgNum);
//voidtransmit_turn_LED_OFF(charMsgNum);
voidreceive_data(charMsgNum);
voidexternal_osc(void);
voidconfig_IO(void);
voidflash_LED(void);
voidtest_reg_write(chartest);
voidstop_CAN(void);
////////////////////////////////////////////////////////////////////////////////
//MAINRoutine
////////////////////////////////////////////////////////////////////////////////
voidmain(void){
//disablewatchdogtimer
WDTCN=0xde;
WDTCN=0xad;
//configurePortI/O
config_IO();
//switchtoexternaloscillator
external_osc();
////////////////////////////////////////////////////////////////////////////////
//ConfigureCANcommunications
//
//IF1usedforprocedurescallesbymainprogram
//IF2usedforinterruptserviceprocedurereceive_data
//
//MessageObjectassignments:
//0x02:
UsedtotransmitcommandstotoggleitsLED,arbitrationnumber1
//
////////////////////////////////////////////////////////////////////////////////
//ClearCANRAM
clear_msg_objects();
//Initializemessageobjecttotransmitdata
//init_msg_object_TX(0x02);
//init_msg_object_TX(0x03);
//Initializemessageobjecttoreceivedata
//init_msg_object_RX(0x01);
init_msg_object_RX(0x01,65);//ID=130
//EnableCANinterruptsinCIP-51
EIE2=0x20;
//FunctioncalltostartCAN
start_CAN();
//Globalenable8051interrupts
EA=1;
//Loopandwaitforinterrupts
while
(1)
{
/*if(BUTTON==0)
{
while(BUTTON==0){}
transmit_turn_LED_OFF(0x03);
}
else
{
while(BUTTON==1){}
transmit_turn_LED_ON(0x03);
}*/
}
}
////////////////////////////////////////////////////////////////////////////////
//SetupC8051F040
////////////////////////////////////////////////////////////////////////////////
//Switchtoexternaloscillator
voidexternal_osc(void)
{
intn;//localvariableusedindelayFORloop.
SFRPAGE=CONFIG_PAGE;//switchtoconfigpagetoconfigoscillator
OSCXCN=0x77;//startexternaloscillator;22.1MHzCrystal
//systemclockis22.1MHz/2=11.05MHz
for(n=0;n<255;n++);//delayabout1ms
while((OSCXCN&0x80)==0);//waitforoscillatortostabilize
CLKSEL|=0x01;//switchtoexternaloscillator
}
voidconfig_IO(void)
{
SFRPAGE=CONFIG_PAGE;//PortSFR'sonConfigurationpage
XBR3=0x80;//ConfigureCANTXpin(CTX)aspush-pulldigitaloutput
P1MDOUT|=0x40;//ConfigureP1.6aspush-pulltodriveLED
XBR2=0x40;//EnableCrossbar/lowports
//enableP2.0aspush-pulloutput
P2MDOUT|=0x03;
//enableP0.7
P0MDOUT|=0x80;
//enableP0.6
P0MDOUT|=0x40;
//enableP3推挽输出
P3MDOUT=0xff;
//P3漏极开路输出
//P3MDOUT=0x00;
//P3MDIN=0xff;//P3数字输入
//setP2.0,P2.1
//P20=0;
//P21=0;
//setXMIFwrn
WRn=0;
RDn=1;
}
////////////////////////////////////////////////////////////////////////////////
//CANFunctions
////////////////////////////////////////////////////////////////////////////////
//ClearMessageObjects
voidclear_msg_objects(void)
{
SFRPAGE=CAN0_PAGE;
CAN0ADR=IF1CMDMSK;//PointtoCommandMaskRegister1
CAN0DATL=0xFF;//SetdirectiontoWRITEallIFregisterstoMsgObj
for(i=1;i<33;i++)
{
CAN0ADR=IF1CMDRQST;//Writeblank(reset)IFregisterstoeachmsgobj
CAN0DATL=i;
}
}
//InitializeMessageObjectforRX
voidinit_msg_object_RX(charMsgNum,uintdevID)
{
uinttemp;
SFRPAGE=CAN0_PAGE;
CAN0ADR=IF1CMDMSK;//PointtoCommandMask1
CAN0DAT=0x00BB;//SettoWRITE,andalterallMsgObjexceptIDMASK
//anddatabits
CAN0ADR=IF1ARB1;//Pointtoarbitration1register
CAN0DAT=0x0000;//Setarbitration1IDto"0"
//CAN0DAT=0x8004;//Arb2highbyte:
SetMsgValbit,noextendedID,can2.cCAN0DAT=0x8000;
//Dir=RECEIVE
temp=devID<<2;
temp&=0x1fff;//000ID28--ID16
temp|=0x8000;
CAN0DAT=temp;
//CAN0DAT=0x8000;
CAN0DAT=0x0482;//MsgCntrl:
setRXIE,remoteframefunctiondisabled
//DLC=4;
CAN0ADR=IF1CMDRQST;//PointtoCommandRequestreg.
CAN0DATL=MsgNum;//SelectMsgObjpassedintofunctionparameterlist
//--initiateswritetoMsgObj
//3-6CANclockcyclestomoveIFregistercontentstotheMsgObjinCANRAM
}
//InitializeMessageObjectforTX
/*voidinit_msg_object_TX(charMsgNum)
{
SFRPAGE=CAN0_PAGE;
CAN0ADR=IF2CMDMSK;//PointtoCommandMask1
CAN0DAT=0x00B2;//SettoWRITE,&alterallMsgObjexceptIDMASKbits
CAN0ADR=IF2ARB1;//Pointtoarbitration1register
CAN0DAT=0x0000;//Setarbitration1IDtohighestpriority
CAN0DAT=0xA000;//AutoincrementtoArb2highbyte:
can2.cCAN0DAT=0xA004;
//SetMsgValbit,noextendedID,Dir=WRITE
CAN0DAT=0x0081;//MsgCntrl:
DLC=1,remoteframefunctionnotenabled
CAN0ADR=IF2CMDRQST;//PointtoCommandReq