高中生
最后登录1970-1-1
在线时间 小时
注册时间2014-12-14
|
楼主 |
发表于 2015-11-24 15:12:59
|
显示全部楼层
#ifndef __CAN_H__
#define __CAN_H__
// write your header here
#include "2515.h"
#include "SPI.h"
#include "reg51.h"
#include "Function.h"
#define CAN_10Kbps 0x31
#define CAN_25Kbps 0x13
#define CAN_50Kbps 0x09
#define CAN_100Kbps 0x04
#define CAN_125Kbps 0x03
#define CAN_250Kbps 0x01
#define CAN_500Kbps 0x00
//void SPIReset(void);
//void SPIByteWrite(unsigned char ,unsigned char );
//unsigned char SPIByteRead(unsigned char );
unsigned char dummy;
void Init2515(unsigned int IDF)
{
SPIReset();
delay_ms(1);
//SPIByteWrite(CANCTRL,0x80);//CAN1¤×÷ÔúÅäÖÃÄ£ê½
SPIByteWrite(RXM0SIDH,0x00);
SPIByteWrite(RXM0SIDL,0x00);
SPIByteWrite(RXF0SIDH,0x00);
SPIByteWrite(RXF0SIDL,0x00);
SPIByteWrite(RXM1SIDH,0x00);
SPIByteWrite(RXM1SIDL,0x00);
SPIByteWrite(RXF2SIDH,0x00);
SPIByteWrite(RXF2SIDL,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éÑù£¬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(TXB0CTRL,0x03);//éèÖ÷¢Ëí»o3åÆ÷0¿ØÖƼÄ′æÆ÷ÇåáãTXREQ,·¢ËíóÅÏ輶×î¸ß
//BitModiMcp2515(TXB0CTRL,0x08,0x00);
SPIByteWrite(TXB0SIDH,IDF);//éèÖ÷¢Ëí»o3åÆ÷0μıê×¼±ê궷û£¬′yDT¸Ä***
SPIByteWrite(TXB0SIDL,0xE0);//óÃμ½±ê×¼±ê궷û
SPIByteWrite(TXB0DLC,DLC_8);//éèÖ÷¢Ëíêy¾YμÄ3¤¶èÎa8¸ö×Ö½ú
//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,0x55);//éèÖýóêÕ»o3åÆ÷0μıê×¼±ê궷û£¬′yDT¸Ä***
//SPIByteWrite(RXB0SIDL,0xE0);//óÃμ½±ê×¼±ê궷û
SPIByteWrite(RXB0CTRL,0x20);//½ö½ö½óêÕ±ê×¼±ê궷ûμÄóDD§DÅÏ¢£¬FIILHIT0=0±íê¾RXB0 £¬2éóÃFILHIT0
SPIByteWrite(RXB0DLC,DLC_8);//éèÖýóêÕêy¾YμÄ3¤¶èÎa8¸ö×Ö½ú
SPIByteWrite(RXF0SIDH,0xE0);//3õê¼»ˉ½óêÕÂË2¨Æ÷0£¬′yDT¸Ä***
SPIByteWrite(RXF0SIDL,0xE0);
SPIByteWrite(RXM0SIDH,0xFF);//3õê¼»ˉ½óêÕÆá±ÎÆ÷0£¬′yDT¸Ä***
SPIByteWrite(RXM0SIDL,0xE0);
//éèÖýóêÕ»o3åÆ÷1μıê궷ûoí3õê¼»ˉêy¾Y
//SPIByteWrite(RXB1SIDH,IDF); //éèÖýóêÕ»o3åÆ÷0μıê×¼±ê궷û£¬′yDT¸Ä***
//SPIByteWrite(RXB1SIDL,0xE0);//óÃμ½±ê×¼±ê궷û
SPIByteWrite(RXB1CTRL,0x20);//½ö½ö½óêÕ±ê×¼±ê궷ûμÄóDD§DÅÏ¢£¬FIILHIT0=0±íê¾RXB0 £¬2éóÃFILHIT0
SPIByteWrite(RXB1DLC,DLC_8);//éèÖýóêÕêy¾YμÄ3¤¶èÎa8¸ö×Ö½ú
SPIByteWrite(RXF2SIDH,0x00);//3õê¼»ˉ½óêÕÂË2¨Æ÷2£¬′yDT¸Ä***
SPIByteWrite(RXF2SIDL,0xE0);
SPIByteWrite(RXF3SIDH,0x01);//3õê¼»ˉ½óêÕÂË2¨Æ÷3£¬′yDT¸Ä***
SPIByteWrite(RXF3SIDL,0xE0);
SPIByteWrite(RXF4SIDH,0x02);//3õê¼»ˉ½óêÕÂË2¨Æ÷4£¬′yDT¸Ä***
SPIByteWrite(RXF4SIDL,0xE0);
SPIByteWrite(RXF5SIDH,0x56);//3õê¼»ˉ½óêÕÂË2¨Æ÷5£¬′yDT¸Ä***
SPIByteWrite(RXF5SIDL,0xE0);
SPIByteWrite(RXM1SIDH,0xFF);//3õê¼»ˉ½óêÕÆá±ÎÆ÷1£¬′yDT¸Ä***
SPIByteWrite(RXM1SIDL,0xE0);
//éèÖýóêÕ»o3åÆ÷0ÖD¶Ï
SPIByteWrite(CANINTF,0x00);//½óêÕíêò»′αØDë¶ÔÖD¶Ï±ê־λÇå0
SPIByteWrite(CANINTE,0x02);//½óêÕ»o3åÆ÷0ÂúÖD¶Ïê1Äüλ***DT¸Ä3é01-02
//éèÖÃμ¥′¥·¢Ä£ê½1 ê1Äü£¬±¨ÎÄ3¢êÔ·¢Ëíò»′Σ»0½ûÖ1¡£èçóDDèòa±¨ÎÄ»áÖØ·¢
//BitModiMcp2515(CANCTRL,0x08,0x00);
SPIByteWrite(CANCTRL,REQOP_NORMAL | CLKOUT_ENABLED);//éèÖÃÕy3£Ä£ê½
BitModiMcp2515(CANCTRL,0xE7,0x04);
dummy=SPIByteRead(CANSTAT);
if (OPMODE_NORMAL != (dummy && 0xE0))
SPIByteWrite(CANCTRL,REQOP_NORMAL | CLKOUT_ENABLED);//ÅD¶Ï½øèëÕy3£1¤×÷Ä£ê½
//SPIByteWrite(CANCTRL,0x00);//Ñ¡¶¨Õy3£1¤×÷Ä£ê½
}
//CAN·¢Ëí3ìDò
void CAN_Send(unsigned char *CAN_TX_Buf)
{
unsigned char tempdata;
tempdata=SPIByteRead(CAN_RD_STATUS);
if(tempdata&0x04) //ÅD¶ÏTXREQ±ê־λ
{
delay_ms(1);
//SPIByteWrite(TXB0CTRL,0);//Çå3yTXREQ±ê־λ
BitModiMcp2515(TXB0CTRL,0x08,0x00);
while(SPIByteRead(CAN_RD_STATUS)&0x04);//μè′yTXREQÇåáã
}
SPIByteWrite(TXB0D0,CAN_TX_Buf[0]);
SPIByteWrite(TXB0D1,CAN_TX_Buf[1]);
SPIByteWrite(TXB0D2,CAN_TX_Buf[2]);
SPIByteWrite(TXB0D3,CAN_TX_Buf[3]);
SPIByteWrite(TXB0D4,CAN_TX_Buf[4]);
SPIByteWrite(TXB0D5,CAN_TX_Buf[5]);
SPIByteWrite(TXB0D6,CAN_TX_Buf[6]);
SPIByteWrite(TXB0D7,CAN_TX_Buf[7]);
/* if(tempdata&0x04) //ÅD¶ÏTXREQ±ê־λ
{
delay_ms(2);
//SPIByteWrite(TXB0CTRL,0);//Çå3yTXREQ±ê־λ
BitModiMcp2515(TXB0CTRL,0x08,0x00);
while(SPIByteRead(CAN_RD_STATUS)&0x04);//μè′yTXREQÇåáã
}*/
CS=LOW;
//WriteSPI(CAN_RTS_TXB0);//·¢Ëí»o3åÆ÷0ÇëÇó·¢Ëí
BitModiMcp2515(TXB0CTRL,0x08,0xFF);
CS=HIGH;
}
//CAN½óêÕ3ìDò
/*void CAN_Receive(unsigned char *CAN_RX_Buf)
{
CAN_RX_Buf[0]=SPIByteRead(RXB0D0);
CAN_RX_Buf[1]=SPIByteRead(RXB0D1);
CAN_RX_Buf[2]=SPIByteRead(RXB0D2);
CAN_RX_Buf[3]=SPIByteRead(RXB0D3);
CAN_RX_Buf[4]=SPIByteRead(RXB0D4);
CAN_RX_Buf[5]=SPIByteRead(RXB0D5);
CAN_RX_Buf[6]=SPIByteRead(RXB0D6);
CAN_RX_Buf[7]=SPIByteRead(RXB0D7);
SPIByteWrite(CANINTF,0x00);//Çå¿Õ½óêÕÖD¶Ï±ê־λ
//′ò¿a½óêÕ»o3åÆ÷½óêÕDÂDÅÏ¢£¬¸Ã¾äèçoÎD′
}*/
void CAN_Receive(unsigned char *CAN_RX_Buf)
{
BitModiMcp2515(CANINTF,0x02,0x00);//Çå¿Õ½óêÕÖD¶Ï±ê־λRX1IF
CAN_RX_Buf[0]=SPIByteRead(RXB1D0);
CAN_RX_Buf[1]=SPIByteRead(RXB1D1);
CAN_RX_Buf[2]=SPIByteRead(RXB1D2);
CAN_RX_Buf[3]=SPIByteRead(RXB1D3);
CAN_RX_Buf[4]=SPIByteRead(RXB1D4);
CAN_RX_Buf[5]=SPIByteRead(RXB1D5);
CAN_RX_Buf[6]=SPIByteRead(RXB1D6);
CAN_RX_Buf[7]=SPIByteRead(RXB1D7);
//SPIByteWrite(CANINTF,0x00); //Çå¿Õ½óêÕÖD¶Ï±ê־λ
//BitModiMcp2515(CANINTF,0x02,0x00);//Çå¿Õ½óêÕÖD¶Ï±ê־λRX1IF
//′ò¿a½óêÕ»o3åÆ÷½óêÕDÂDÅÏ¢£¬¸Ã¾äèçoÎD′
}
#endif是不是在这个里
|
|