我将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éÑù£¬
HSEG1=(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)//ÅD¶ÏTXREQ±ê־λ
{
delay_ms(1);
SPIByteWrite(TXB0CTRL,0);//Çå3yTXREQ±ê־λ
while(SPIByteRead(CAN_RD_STATUS)&0x04);//μè′yTXREQÇåáã
}
CS=0;
WriteSPI(CAN_RTS_TXB0);//·¢Ëí»o3åÆ÷0ÇëÇó·¢Ëí
CS=1;
}
void SPIByteWrite(u8 addr,u8 value) //Ïò2515Ö¸¶¨ò»¸öμØÖ·D′Ö¸áî
{
CS=0;
WriteSPI(CAN_WRITE);
WriteSPI(addr);
WriteSPI(value);
CS=1;
}
void WriteSPI(u8 ch) //D′oˉêy
{
SPI1_ReadWriteByte(ch);
}
void SPIReset() //Æ÷¼t¸′λ
{
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Ãû : CAN_Receive_Buffer
* Ãèêö : CAN½óêÕò»Ö¡êy¾Y
* êäèë : *CAN_TX_Buf(′y½óêÕêy¾Y»o3åÇøÖ¸Õë)
* êä3ö : ÎT
* ·μ»ØÖμ : len(½óêÕμ½êy¾YμÄ3¤¶è,0~8×Ö½ú)
* ËμÃ÷ : Î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);//¶á衽óêÕ»o3åÆ÷0½óêÕμ½μÄêy¾Y3¤¶è(0~8¸ö×Ö½ú)
while(i<len)
{
CAN_RX_Buf[i]=SPIByteRead(RXB0D0+i);//°ÑCAN½óêÕμ½μÄêy¾Y·ÅèëÖ¸¶¨»o3åÇø
USART_SendData(USART1,CAN_RX_Buf[i]);
i++;
}
}
SPIByteWrite(CANINTF,0);//Çå3yÖD¶Ï±ê־λ(ÖD¶Ï±êÖ¾¼Ä′æÆ÷±ØDëóéMCUÇåáã)
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};//¸′λ
// u8 buf1[8]={0x01,0x55,0x55,0x55,0x55,0x55,0x55,0x55};//Ä£ê½Ñ¡Ôñ
// u8 buf2[8]={0,0,0,0,0,0,0,0};
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
delay_init(168); //3õê¼»ˉÑóê±oˉêy
uart_init(115200); //3õê¼»ˉ′®¿ú2¨ìØÂêÎa115200
LED_Init(); //3õê¼»ˉLED
SPI1_Init();
SPI1_SetSpeed(SPI_BaudRatePrescaler_4);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
MCP_2515_init();
CAN1_Configuration(); //CAN13õê¼»ˉ
while(1)
{
LED0=!LED0;
CAN_Send_anylength(buf,8);
delay_ms(1000);
CAN_Receive_Buffer(canbuf);
}
}
求大神解答,调了4天了,总是通讯不了
一周热门 更多>