xjsfsq 发表于 2009-9-2 14:47:54

有关C8051F040单片机CAN总线配置问题

请教高手指点一下,C8051F040单片机配置CAN总线各个寄存器的顺序,以及如何配置这些寄存器。请详细点。书上有配置好的程序,但我不明白为什么这样配,每个寄存器都到底干什么的

lnskngdc 发表于 2009-9-2 15:24:19

这是我刚开始时用的程序,只是在例程上修改了点,注释有中文有英文,别告诉我你看不懂英文哦!


void clear_msg_objects (void)
{
uchar i;
SFRPAGE= CAN0_PAGE;
CAN0ADR= IF1CMDMSK;    // Point to Command Mask Register 1
CAN0DATL = 0xFF;         // Set direction to WRITE all IF registers to Msg Obj
for (i=1;i<33;i++)
    {
      CAN0ADR = IF1CMDRQST; // Write blank (reset) IF registers to each msg obj
      CAN0DATL = i;
    }
}

void init_msg_object_TX (uchar MsgNum,uint id)
{
        uint tempreg;
        SFRPAGE = CAN0_PAGE;
        CAN0ADR = IF1CMDMSK; //指向IF1 Command Mask Registers
        CAN0DAT = 0x00f3;
        /*   IF1 Command Mask Registers=0x00b3
        WR/RD=1,Mask=0,Arb=1,Control=1,ClrIntPnd=0,TxRqst=0,DataA=1,DataB=1,一次发送8字节数据*/

        tempreg=0x0000;
//        CAN0ADR = IF1MSK1;
//        CAN0DAT = 0x0000;
        CAN0ADR = IF1MSK2;//test
        CAN0DAT = tempreg; //test2fc0

        CAN0ADR = IF1ARB1;            
        CAN0DAT = 0x0000;
        /*IF1 Arbitration Registers1 =0x0000,即ID15-0=0*/

        CAN0ADR = IF1MSGC;
        CAN0DAT = 0x0084;          //发送4个字节数据
        /*IF1 Message Control Registers=0x0088
        NewDat=0,MsgLst=0,IntPnd=0,UMask=0,TxIE=0,RxIE=0,RmtEn=0,TxRqst=0
        EoB=1,DLC3-0=0100,即数据长度为4*/

        tempreg=id<<2;//标准id为ID28-ID18,所以要左移2位
        tempreg&=0x1fff;
        tempreg|=0xa000;
        CAN0ADR = IF1ARB2;
        CAN0DAT = tempreg; //地址自增,指向IF1 Arbitration Registers2
        /*IF1 Arbitration Registers2=101(id)00b
        MsgVal=1,Xtd=0,为标准模式,扩展ID无效,Dir=1,为发送*/

        CAN0ADR = IF1CMDRQST;         
        CAN0DAT = MsgNum;   
        /*IF1 Command Request Registers=MsgNum,将以上配置写入MsgNum号消息*/
}
void init_msg_object_RX (uchar MsgNum,uint id)
{
        uint tempreg;
           SFRPAGE = CAN0_PAGE;
        CAN0ADR = IF2CMDMSK;
        CAN0DAT = 0x00fb;            
        /*   IF2 Command Mask Registers=0x00bb
        WR/RD=1,Mask=0,Arb=1,Control=1,ClrIntPnd=1,TxRqst=0,DataA=1,DataB=1,一次发送8字节数据*/


        tempreg=0x0000;
//        CAN0ADR = IF2MSK1;
//        CAN0DAT = 0x0000;
        CAN0ADR = IF2MSK2;//test
        CAN0DAT = tempreg; //test2fc0

        CAN0ADR = IF2ARB1;         
        CAN0DAT = 0x0000;
        /*IF2 Arbitration Registers1 =0x0000,即ID15-0=0*/

        CAN0ADR = IF2MSGC;
        CAN0DAT = 0x1488;
        /*IF2 Message Control Registers=0x0488
        NewDat=0,MsgLst=0,IntPnd=0,UMask=1,TxIE=0,RxIE=1,接收中断使能;RmtEn=0,TxRqst=0
        EoB=1,DLC3-0=1000,即数据长度为8*/
       
        tempreg=(id<<2);//标准id为ID28-ID18,所以要左移2位
        tempreg&=0x1fff;
        tempreg|=0x8000;                   
        CAN0ADR = IF2ARB2;
        CAN0DAT = tempreg;
       
        /*IF2 Arbitration Registers2=100(id)00b
        MsgVal=1,Xtd=0,为标准模式,扩展ID无效,Dir=0,为接收*/


        CAN0ADR = IF2CMDRQST;      
        CAN0DATL = MsgNum;         
        /*IF2 Command Request Registers=MsgNum,将以上配置写入MsgNum号消息*/

}



//Start CAN
void start_CAN (void)
{
SFRPAGE= CAN0_PAGE;
CAN0CN|= 0x41;       // 进入初始化状态,置位 CCE & INIT
CAN0ADR= BITREG   ;// 将索引指向 Timing register
CAN0DAT= 0x4FC1;   // 由上面计算得来
//CAN0DAT= 0x5EC0;//250MHz
//CAN0DAT= 0x4FC3; //250k
CAN0ADR= IF1CMDMSK;// 将索引指向Command Mask 1          
CAN0DAT= 0x0083;   // Config for TX : WRITE to CAN RAM, write data bytes,
                                      // set TXrqst/NewDat, clr IntPnd

// RX-IF2 operation may interrupt TX-IF1 operation
CAN0ADR= IF2CMDMSK;// Point to Command Mask 2
CAN0DATL = 0x1F;       // Config for RX : READ CAN RAM, read data bytes,
                         // clr NewDat and IntPnd
CAN0CN|= 0x06;       // Global Int. Enable IE and SIE
CAN0CN&= ~0x41;      // Clear CCE and INIT bits, starts CAN state machine
}

xjsfsq 发表于 2009-9-3 11:03:17

谢谢给的程序,对我还是有所启发的,谢谢!

byin 发表于 2010-5-6 15:09:24

mark

billsjz 发表于 2010-8-29 22:35:22

mark

abao_wjx 发表于 2010-12-11 14:25:30

mark

xw802 发表于 2011-3-4 16:45:18

有用,mark

wanzu 发表于 2011-11-19 23:06:53

mark

mvpgpz 发表于 2013-8-22 15:16:06

mark,学习中...

zhangyunbo 发表于 2013-9-13 09:58:26

mark!!!!学习学习!
页: [1]
查看完整版本: 有关C8051F040单片机CAN总线配置问题