基于stm32的mcp2515无法进入正常模式??????

2019-03-23 18:22发布

void MCP_2515_init()
{

           SPIReset();
       delay_ms(1000);
//SPIRead(0x0e);
       
        dummy=SPIByteRead(CANSTAT);
   number=SPIByteRead(CANCTRL);
  //SPIByteWrite(CANCTRL,0x80);//CAN1¤×÷ÔúÅäÖÃÄ£ê½

//  SPIByteWrite(RXM0SIDH,0x00);
//  SPIByteWrite(RXM0SIDL,0x00);

//  SPIByteWrite(RXF0SIDH,0x00);
//  SPIByteWrite(RXF0SIDL,0x00);
  //éèÖÃ2¨ìØÂêÎa10Kbps
  //set CNF1,SJW=00,3¤¶èÎa1TQ,BRP=49,TQ=[2*(BRP+1)]/Fsoc=2*50/8M=12.5us
  SPIByteWrite(CNF1,CAN_125Kbps);
  //set CNF2,SAM=0,Ôú2éÑùμã¶Ô×üÏß½øDDò»′Î2éÑù£¬PHSEG1=(2+1)TQ=3TQ,PRSEG=(0+1)TQ=1TQ
  SPIByteWrite(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ);
  //set CNF3,PHSEG2=(2+1)TQ=3TQ,í¬ê±μ±CANCTRL.CLKEN=1ê±é趨CLKOUTòy½ÅÎaê±¼äêä3öê1Äüλ
  SPIByteWrite(CNF3,PHSEG2_3TQ);

  //set TXB0£¬éèÖ÷¢Ëí»o3åÆ÷0μıê궷ûoí·¢ËíμÄêy¾Y£¬òÔ¼°·¢ËíμÄêy¾Y3¤¶è
  SPIByteWrite(TXB0SIDH,0xFF);//éèÖ÷¢Ëí»o3åÆ÷0μıê×¼±ê궷û£¬′yDT¸Ä***
  SPIByteWrite(TXB0SIDL,0xE0);//óÃμ½±ê×¼±ê궷û

  //SPIByteWrite(TXB0D0,0x1E);//óD′yDT¸Ä¼°è·¶¨êÇ·ñê1óÃ
  //SPIByteWrite(TXB0D1,0x10)£»//óD′yDT¸Ä¼°è·¶¨êÇ·ñê1óÃ

  /*set TXB1
  SPIByteWrite(TXB1SIDH,0x50);    //Set TXB0 SIDH
  SPIByteWrite(TXB1SIDL,0x00);    //Set TXB0 SIDL
  SPIByteWrite(TXB1DLC,0x40 | DLC_8);    //Set DLC = 3 bytes and RTR bit*/

  //éèÖýóêÕ»o3åÆ÷0μıê궷ûoí3õê¼»ˉêy¾Y
  SPIByteWrite(RXB0SIDH,0x00);//éèÖýóêÕ»o3åÆ÷0μıê×¼±ê궷û£¬′yDT¸Ä***
  SPIByteWrite(RXB0SIDL,0x00);//óÃμ½±ê×¼±ê궷û
  SPIByteWrite(RXB0CTRL,0x20);//½ö½ö½óêÕ±ê×¼±ê궷ûμÄóDD§DÅÏ¢£¬FIILHIT0=0±íê¾RXB0 £¬2éóÃFILHIT0
  SPIByteWrite(RXB0DLC,DLC_8);//éèÖýóêÕêy¾YμÄ3¤¶èÎa8¸ö×Ö½ú

  SPIByteWrite(RXF0SIDH,0xFF);//3õê¼»ˉ½óêÕÂË2¨Æ÷0£¬′yDT¸Ä***
  SPIByteWrite(RXF0SIDL,0xE0);
  SPIByteWrite(RXM0SIDH,0xFF);//3õê¼»ˉ½óêÕÆá±ÎÆ÷0£¬′yDT¸Ä***
  SPIByteWrite(RXM0SIDL,0xE0);

  //éèÖýóêÕ»o3åÆ÷0ÖD¶Ï
  SPIByteWrite(CANINTF,0x00);//Çå¿ÕÖD¶Ï±ê־λ
  SPIByteWrite(CANINTE,0x01);//½óêÕ»o3åÆ÷0ÂúÖD¶Ïê1Äüλ


  SPIByteWrite(CANCTRL,REQOP_NORMAL|CLKOUT_ENABLED);//éèÖÃÕy3£Ä£ê½

  delay_ms(20);
  dummy=SPIByteRead(CANSTAT);        //èç1û»1′|óúÅäÖÃģ꽣¬¾í½øèë1¤×÷Ä£ê½ÅäÖÃ
  delay_ms(20);
        dummy=SPIByteRead(CANSTAT);
        if(OPMODE_NORMAL!=(dummy&&0xE0))
  {
    SPIByteWrite(CANCTRL,REQOP_NORMAL|CLKOUT_ENABLED);//ÅD¶Ï½øèëÕy3£1¤×÷Ä£ê½
  }
                 

}                //3õê¼»ˉíêè«íêè«Õyè·

读写函数都正确,就是 到SPIByteWrite(CANCTRL,REQOP_NORMAL|CLKOUT_ENABLED);这以后,无法进入正常模式?求大神帮忙啊

此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
1条回答
ienglgge
1楼-- · 2019-03-24 05:50
/ SPIByteWrite(CANCTRL,REQOP_NORMAL|CLKOUT_ENABLED),之前如果正常,写函数本身好的。看这一句里写的参数是否正确。无法进入正常模式。能读到相关寄存器的值吗。看看芯片处于什么状态。是不是哪里配置不对。

一周热门 更多>