Home > other >  C8051F040 chip can communication problem, a great god answer!!!!!!!!!!!
C8051F040 chip can communication problem, a great god answer!!!!!!!!!!!

Time:09-25

#include
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

# define CANCTRL 0 x00//control register
# define CANSTAT 0 x01//status register
# define ERRCNT 0 x02/register/error count
# define BITREG bit 0 x03//timing register
# define INTREG 0 x04//interrupt register
# define CANTSTR 0 x05//test register
# define BRPEXT 0 x06//BRP extension register
//IF1 interface register
//IF1 command request register
# define IF1CMDRQST 0 x08//IF1 command request
# define IF1CMDMSK 0 x09//orders mask register
# define IF1MSK1 0 x0a//mask register 1
# define IF1MSK2 0 x0b//mask register 2
# define IF1ARB1 0 x0c//secondary load register 1
# define IF1ARB2 0 x0d//secondary load register 2
//IF1 message control register
# define IF1MSGC 0 x0e//message control register
# define IF1DATA1 0 x0f//data A register
# define IF1DATA2 0 x10
# define IF1DATB1 0 x11//data register B
# define IF1DATB2 0 x12
//IF2 interface register
//IF2 command request register
# define IF2CMDRQST 0 x20
# define IF2CMDMSK 0 x21//orders mask register
# define IF2MSK1 0 x22//mask register 1
# define IF2MSK2 0 x23//mask register 2
# define IF2ARB1 0 x24//arbitration register 1
# define IF2ARB2 0 x25//arbitration register 2
//IF2 message control register
# define IF2MSGC 0 x26
# define IF2DATA1 0 x27//data A register
# define IF2DATA2 0 x28
# define IF2DATB1 0 x29//data register B
# define IF2DATB2 0 x2a
//==message processor registers
//==register transfer requests==
# define TRANSREQ1 0 x40
# define TRANSREQ2 0 x41
# define NEWDAT1 0 x48//new data register
# define NEWDAT2 0 x49
Hang//==interrupt register
# define INTPEND1 0 x50
# define INTPEND2 0 x51
# define MSGVAL1 0 x58//message effectively register
# define MSGVAL2 0 x59
//global variable
Char MsgNum;//the message object number
Char status;//state
Int m;
Sfr16 CAN0DAT=0 xd8;
Void main ()
{
SFRPAGE=0 x0f;
OSCXCN_Init ();//oscillator initialization

SFRPAGE=0 x0f;//port initialization
IO_Init ();

Clear_msg_objects ();//remove the message RAM
//send to use, can need not in CAN2.0
Init_msg_object_RX x04 (0);//to accept early make function

EIE2=0 x20;//enabled CAN interrupt
Start_CAN ();//CAN make the early set function
EA=1;

}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//the function name: void clear_msg_objects (void)
//can work: clear message object
//description:
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Void clear_msg_objects (void)
{
SFRPAGE=CAN0_PAGE;
CAN0ADR=IF1CMDMSK;//points to command mask register
CAN0DATL=0 XFF;//write all the direction of the message object as
For (m=0; M<33. M++)
{
CAN0ADR=IF1CMDRQST;//remove 32 message object
CAN0DATL=m;
}
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//the function name: void init_msg_object_RX (char MsgNum)
//can work: accept early function make the
//description:
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Void init_msg_object_RX (char MsgNum)
{
SFRPAGE=CAN0_PAGE;
CAN0ADR=IF2CMDMSK;//points to the command register 1 mask
CAN0DAT=0 x00f8;//will be selected request message buffer register data sent to the command register addressing the message object
//set to write, and change all the message object, in addition to the identification and data bit//mask mask a constant
//send the ID number + dir + + MsgVal XTD to message object
//send control bits to the message object
//when the write operation, this bit is ignored
//to accept a constant
//data byte 0-3 constant
//data bytes 4-7 constant
//add mask bit
CAN0ADR=IF2MSK1;//(acceptable ID 00 01 10, 11)
CAN0DAT=0 x0000;
CAN0DAT=0 x0000;//no action/message to accept the filtering direction of extension for accept full use filter/11 shielding operator

CAN0ADR=IF2ARB1;//points to the arbitration register
CAN0DAT=0 x0000;//set the ID of arbitration priority the highest priority

CAN0DAT=0 xc000;//message object is configured, and through the message processing to consider
//set the message effectively, there is no extension ID, direction to accept//message objects using standard identifier 11
//message to accept
//identifier of the 11
CAN0ADR=IF2MSGC;
CAN0DAT=0 x1480;//message control register
//set to accept allows, banning remote frame//there is no new data is written to the message object
//from CPU to the reset, no messages are lost,
//this message object is not interrupt source
//screen to ignore (using)
//after the success of the frame transmission, interrupt hang will change
//after the success of the frame to accept, the interrupt will hang will set the
//in a remote frames, send the request does not change
//the message object without waiting for transmission
//a single message or FIFO last object

CAN0ADR=IF2CMDRQST;//points to command request register
CAN0DATL=MsgNum;//write message objects, namely to which message objects,
//after 3 -- 6 CAN clock cycle, IF the contents of the registers will be moved to the CAN message object of memory in the
}


//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//the function name: void start_CNA (void)
//CAN work: CAN make the early setting function
//description:
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Void start_CAN (void)
{
SFRPAGE=CAN0_PAGE;
CAN0CN=CAN0CN | 0 x41;//can make CCE and INIT
//make early can make, buy to start early to make into a normal operation of 1//
//there are data to reach the designated position regularly CPU registers (when the INIT=1)
//start the interference automatic retransmission
//don't interrupt status error generated
//do not produce state change interrupt
//disable module interrupt IRQ_B always at a high logic level
//start early to make change

CAN0ADR=BITREG;//points to a timer assignment
CAN0DAT=0 x2303;//to a timer assignment

//add
//CAN0ADR=CANTSTR;
//CAN0DAT=0 x0004;
//send to use, can in the CAN2.0 don't
//CAN0ADR=IF1CMDMSK;//points to the command register 1 mask
//CAN0DAT=0 x0087;//write:
//set CAN RAM to write, write data bytes,//set to send request a
//will send 0-3 sent to the message object
//will send 4-7 sent to the message object

CAN0ADR=IF2CMDMSK; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related