mcp2515通讯问题

2019-07-20 12:25发布

我将mcp2515模块设置为环回模式,将缓冲器里的数据发送到串口,然后将其显示串口调试助手上。
这是mcp2515的内容:#include "MCP2515.h"
#include  "delay.h"
#include  "led.h"
#include  "spi.h"

u8 dummy=0;
//u8 number=0,number1=0,number2=0,j,number3=0,number4=0,number5=0,number6=0;
void MCP_2515_init()
{

           SPIReset();
     delay_ms(100);

  SPIByteWrite(CANCTRL,0x80);//CAN1¤×÷ÔúÅäÖÃÄ£ê½

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

  SPIByteWrite(RXF0SIDH,0x00);
  SPIByteWrite(RXF0SIDL,0x00);
  //éèÖÃ2¨ìØÂêÎa125Kbps
  //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éÑù£&#172HSEG1=(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,0x60);//óÃμ½±ê×¼±ê궷û
  SPIByteWrite(RXB0CTRL,0x60);//½ö½ö½óêÕ±ê×¼±ê궷ûμÄó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_LOOPBACK|CLKOUT_ENABLED);//éèÖÃÕy3£Ä£ê½

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

}                
//3õê¼»ˉíêè«íêè«Õyè·
//¶áÖ¸¶¨μØÖ·μÄÖ¸áî
u8 SPIByteRead(u8 addr)
{
  u8 tempdata;

        CS=0;
  WriteSPI(CAN_READ);
  WriteSPI(addr);
  tempdata=ReadSPI();
  CS=1;
  return tempdata;

}
//CAN·¢ËíèÎòa3¤¶è×Ö½ú
void CAN_Send_anylength(u8 *CAN_TX_Buf,u8 length1)
{

  unsigned char tempdata,j;
  tempdata=SPIByteRead(CAN_RD_STATUS);
  SPIByteWrite(TXB0DLC,length1);

  for(j=0;j<length1;j++)
  {
    SPIByteWrite(TXB0D0+j,CAN_TX_Buf[j]);
  }
       
       

  if(tempdata&0x04)//&#197;D&#182;&#207;TXREQ±ê&#214;&#190;&#206;&#187;
  {
    delay_ms(1);
    SPIByteWrite(TXB0CTRL,0);//&#199;&#229;3yTXREQ±ê&#214;&#190;&#206;&#187;
    while(SPIByteRead(CAN_RD_STATUS)&0x04);//μè′yTXREQ&#199;&#229;á&#227;
  }
  CS=0;
  WriteSPI(CAN_RTS_TXB0);//·¢&#203;í&#187;o3&#229;&#198;÷0&#199;&#235;&#199;ó·¢&#203;í
  CS=1;
}
void SPIByteWrite(u8 addr,u8 value)          //&#207;ò2515&#214;&#184;&#182;¨ò&#187;&#184;&#246;μ&#216;&#214;·D′&#214;&#184;á&#238;
{
  CS=0;
  WriteSPI(CAN_WRITE);
  WriteSPI(addr);
  WriteSPI(value);
  CS=1;
}
void WriteSPI(u8 ch)                  //D′oˉêy
{

    SPI1_ReadWriteByte(ch);

}

void SPIReset()             //&#198;÷&#188;t&#184;′&#206;&#187;
{
   CS=0;
   SPI1_ReadWriteByte(CAN_RESET);
   CS=1;
}
u8 ReadSPI(void)
{  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
   SPI_I2S_SendData(SPI1, 0xff);

  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  return SPI_I2S_ReceiveData(SPI1);
}
/*******************************************************************************
* oˉêy&#195;&#251;  : CAN_Receive_Buffer
* &#195;èê&#246;    : CAN&#189;óê&#213;ò&#187;&#214;&#161;êy&#190;Y
* ê&#228;è&#235;    : *CAN_TX_Buf(′y&#189;óê&#213;êy&#190;Y&#187;o3&#229;&#199;&#248;&#214;&#184;&#213;&#235;)
* ê&#228;3&#246;    : &#206;T
* ·μ&#187;&#216;&#214;μ  : len(&#189;óê&#213;μ&#189;êy&#190;Yμ&#196;3¤&#182;è,0~8×&#214;&#189;ú)
* &#203;μ&#195;÷    : &#206;T
*******************************************************************************/
unsigned char CAN_Receive_Buffer(unsigned char *CAN_RX_Buf)
{
        unsigned char i=0,len=0,temp=0;

        temp = SPIByteRead(CANINTF);
        if(temp & 0x01)
        {
                len=SPIByteRead(RXB0DLC);//&#182;áè&#161;&#189;óê&#213;&#187;o3&#229;&#198;÷0&#189;óê&#213;μ&#189;μ&#196;êy&#190;Y3¤&#182;è(0~8&#184;&#246;×&#214;&#189;ú)
                while(i<len)
                {       

                        CAN_RX_Buf[i]=SPIByteRead(RXB0D0+i);//°&#209;CAN&#189;óê&#213;μ&#189;μ&#196;êy&#190;Y·&#197;è&#235;&#214;&#184;&#182;¨&#187;o3&#229;&#199;&#248;
                        USART_SendData(USART1,CAN_RX_Buf[i]);
                        i++;
                }
        }
        SPIByteWrite(CANINTF,0);//&#199;&#229;3y&#214;D&#182;&#207;±ê&#214;&#190;&#206;&#187;(&#214;D&#182;&#207;±ê&#214;&#190;&#188;&#196;′&#230;&#198;÷±&#216;D&#235;óéMCU&#199;&#229;á&#227;)
        return len;
       

}
这是main.c的程序

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "spi.h"
#include "w25qxx.h"
#include "key.h"  
#include "can.h"
#include "mcp2515.h"







// u8 TEXT_Buffer[]={"Explorer STM32F4 SPI TEST"};
#define SIZE sizeof(TEXT_Buffer)         
unsigned char canbuf[8];
int h;
int main(void)
{
//        u8 Data0 = (unsigned char)((Temp_PWM>>8)&0xff);
//        u8 Data1 = (unsigned char)(Temp_PWM&0xff);
        u8 buf[8]={1,1,1,1,1,1,1,1};//&#184;′&#206;&#187;
//        u8 buf1[8]={0x01,0x55,0x55,0x55,0x55,0x55,0x55,0x55};//&#196;£ê&#189;&#209;&#161;&#212;&#241;
//        u8 buf2[8]={0,0,0,0,0,0,0,0};

        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éè&#214;&#195;&#207;μí3&#214;D&#182;&#207;ó&#197;&#207;è&#188;&#182;·&#214;×é2
        delay_init(168);     //3&#245;ê&#188;&#187;ˉ&#209;óê±oˉêy
        uart_init(115200);        //3&#245;ê&#188;&#187;ˉ′&#174;&#191;ú2¨ì&#216;&#194;ê&#206;a115200
        LED_Init();                                        //3&#245;ê&#188;&#187;ˉLED
        SPI1_Init();
        SPI1_SetSpeed(SPI_BaudRatePrescaler_4);
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
        MCP_2515_init();
  CAN1_Configuration();                               //CAN13&#245;ê&#188;&#187;ˉ
               
                               
                while(1)       
                {
                        LED0=!LED0;
                        CAN_Send_anylength(buf,8);
                       
                        delay_ms(1000);
                        CAN_Receive_Buffer(canbuf);
                       
                       

                       
                       
                       
                       
                       
                }
       
       
}


求大神解答,调了4天了,总是通讯不了
0条回答

一周热门 更多>