#include <m8c.h> // part specific constants and macros #include "PSoCAPI.h" // PSoC API definitions for all User Modules #include "mcp2515.h"
extern unsigned char SPITxBuff[8]; extern unsigned char SPIRxBuff[8];
void Dly(unsigned int di) { unsigned int di1; for(di1=0;di1<di;di1++); }
void Mcp2515Reset(void) //MCP2515复位 { PRT1DR&=~0x20; //rest Dly(1000); PRT1DR|=0x20; SPIM_Start(SPIM_SPIM_MODE_0|SPIM_SPIM_MSB_FIRST); PRT1DR&=~0x80; //cs=0 while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); SPIM_SendTxData(MCP2515_RESET); //RESET PRT1DR|=0x80; //cs=1 } void Mcp2515Write(unsigned char iAddress,unsigned char *iTxbuff,unsigned char iLen)//将iTxbuff数据写入iAddress地址开始iLen字节 { unsigned char i; SPIM_Start(SPIM_SPIM_MODE_0|SPIM_SPIM_MSB_FIRST); PRT1DR&=~0x80; //cs=0 while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); SPIM_SendTxData(MCP2515_WRITE); while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); SPIM_SendTxData(iAddress); for(i=0;i<iLen;i++) { while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); SPIM_SendTxData(iTxbuff); } PRT1DR|=0x80; //cs=1 } unsigned char Mcp2515ReadSta(unsigned char iStatus) //读取两种状态寄存器用 { unsigned char i; SPIM_Start(SPIM_SPIM_MODE_0|SPIM_SPIM_MSB_FIRST); PRT1DR&=~0x80; //cs=0 while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); SPIM_SendTxData(iStatus); SPIM_Start(SPIM_SPIM_MODE_3|SPIM_SPIM_MSB_FIRST); while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); SPIM_SendTxData(0); while( ! (SPIM_bReadStatus() & SPIM_SPIM_RX_BUFFER_FULL ) ); i=SPIM_bReadRxData(); return i; }
void Mcp2515Rts(unsigned char tbx ) //请求发送缓冲? ,bit0 缓冲0 ,bit1 缓冲1 bit2缓冲2 { unsigned char i; i=tbx|0x80; SPIM_Start(SPIM_SPIM_MODE_0|SPIM_SPIM_MSB_FIRST); PRT1DR&=~0x80; //cs=0 while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); SPIM_SendTxData(i); //RESET PRT1DR|=0x80; //cs=1 } void Mcp2515Bitmod(unsigned char iAddressm,unsigned char iMSIDH,unsigned char iDatam) //位修改模式,参数地址,屏蔽位,数据 { unsigned char ibu[4]; unsigned char i; SPIM_Start(SPIM_SPIM_MODE_0|SPIM_SPIM_MSB_FIRST); ibu[0]=MCP2515_BITMOD; ibu[1]=iAddressm; ibu[2]=iMSIDH; ibu[3]=iDatam; PRT1DR&=~0x80; //cs=0 for(i=0;i<4;i++) { while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); SPIM_SendTxData(ibu); } PRT1DR|=0x80; //cs=1 }
void Mcp2515Read(unsigned char iAddress1,unsigned char *iRxbuff,unsigned char iLen1) { unsigned char i; SPIM_Start(SPIM_SPIM_MODE_0|SPIM_SPIM_MSB_FIRST); PRT1DR&=~0x80; //cs=0 while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); SPIM_SendTxData(MCP2515_READ); while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); SPIM_SendTxData(iAddress1); SPIM_Start(SPIM_SPIM_MODE_3 | SPIM_SPIM_MSB_FIRST); for(i=0;i<iLen1;i++) { while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); SPIM_SendTxData(0); while( ! (SPIM_bReadStatus() & SPIM_SPIM_RX_BUFFER_FULL ) ); iRxbuff=SPIM_bReadRxData(); } PRT1DR|=0x80; //cs=1 } void Mcp2515WriteSigleReg(unsigned char regname,unsigned char regdata) { unsigned char i; unsigned char ibuf[3]; SPIM_Start(SPIM_SPIM_MODE_0|SPIM_SPIM_MSB_FIRST); ibuf[0]=MCP2515_WRITE; ibuf[1]=regname; ibuf[2]=regdata; PRT1DR&=~0x80; //cs=0 for(i=0;i<3;i++) { while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); SPIM_SendTxData(ibuf); } PRT1DR|=0x80; //cs=1 } void InisMcp2515(void) { Mcp2515Reset(); //复位 Mcp2515WriteSigleReg(MCP2515_CANCTRL,MODE_CONFIG); //配置模式 Mcp2515WriteSigleReg(MCP2515_CNF1,SJW1+5); //设置CNF1 Mcp2515WriteSigleReg(MCP2515_CNF2,0XD9); //设置CNF2 Mcp2515WriteSigleReg(MCP2515_CNF3,0X02); //设置CNF3 Mcp2515WriteSigleReg(MCP2515_CANINTE,MCP2515_RX_INT); //设置中断类型 } 相关链接:https://bbs.21ic.com/upfiles/img/200712/2007127211926819.rar |