STC89C51RC+mcp2510的canbus方案。记录备忘。
-
sbit _MCP2510_RESET = P2^6; /** reset mcp2510 **/
-
sbit _CS = P2^5; /** 片选信号线 **/
-
sbit SCK = P2^4; /** 时钟 **/
-
sbit MOSI = P2^3; /** 主出从入**/
-
sbit MISO = P2^2; /** 主入从出**/
-
-
//Eint <--> P3^2; /** mcp2510收到数据后,通过此向mcu发出中断 **/
-
-
/*********************************************************************
-
* RX register
-
*
-
* 0x61(0x71) - SID(10..3)
-
* 0x62(0x72) - SID(2..0), SRR, IDE, -, EID17, EID16
-
* 0x63(0x73) - EID(15..8)
-
* 0x64(0x74) - EID(7..0)
-
* 0x65(0x75) - -, RTR, RB1, RB0, DLC3, DLC2, DLC1, DLC0
-
* [0x66..0x6D](0x76..0x7D) - data
-
*
-
***
-
*
-
* TX, it is different with RX register
-
*
-
* 0x31(0x41/0x51) - SID(10..3)
-
* 0x32(0x42/0x52) - SID(2..0), -, EXIDE, -, EID17, EID16
-
* 0x33(0x43/0x53) - EID(15..8)
-
* 0x34(0x44/0x54) - EID(7..0)
-
* 0x35(0x45/0x55) - -, RTR, -, -, DLC3, DLC2, DLC1, DLC0
-
* [0x36..0x3D](0x46..0x4D/0x56..0x5D) - data
-
*
-
*********************************************************************/
-
unsigned char TXBnCTRL[3] = {0x30, 0x40, 0x50};
-
-
/*
-
* CPOL_0_CPHA_0
-
*
-
* idle after 8-bit,SCK = 0
-
*
-
* sample at first edge(data bit available in H-level)
-
*/
-
void SPI_init(void)
-
{
-
_CS = 1;
-
SCK = 0;
-
MISO = 1;
-
}
-
/** H-level read **/
-
unsigned char SPIread_byte(void)
-
{
-
unsigned char i,inData;
-
-
MISO = 1;
-
-
for(i = 0; i < 8; i++)
-
{
-
inData <<= 1;
-
SCK = 1;
-
inData |= MISO; /** 从SPI总线的数据输出线SO上读入一位 **/
-
MOSI = MISO;
-
SCK = 0;
-
}
-
-
return(inData);
-
}
-
-
/** L-level write **/
-
unsigned char SPIwrite_byte(unsigned char outData)
-
{
-
unsigned char i, inData;
-
-
for(i = 0; i < 8; i++)
-
{
-
outData <<= 1;
-
MOSI = CY; /** 移出一位,送出至SPI总线的数据输入线SI上 **/
-
SCK = 1;
-
_nop_();
-
SCK = 0;
-
}
-
-
return inData;
-
}
-
-
unsigned char MCP2510byte_Read(unsigned char Addr)
-
{
-
unsigned char result;
-
-
_CS = 0;
-
SPIwrite_byte(0x03);
-
SPIwrite_byte(Addr);
-
result = SPIread_byte();
-
_CS = 1;
-
-
return result;
-
}
-
-
void MCP2510byte_Write(unsigned char Addr, unsigned char Data)
-
{
-
_CS = 0;
-
SPIwrite_byte(0x02);
-
SPIwrite_byte(Addr);
-
SPIwrite_byte(Data);
-
_CS = 1;
-
}
-
-
void MCP2510_BitModi(unsigned char Addr, unsigned char Mask, unsigned char Data)
-
{
-
_CS = 0;
-
SPIwrite_byte(0x05);
-
SPIwrite_byte(Addr);
-
SPIwrite_byte(Mask);
-
SPIwrite_byte(Data);
-
_CS = 1;
-
}
-
-
void MCP2510_init(void)
-
{
-
unsigned char i;
-
-
MCP2510_BitModi(0x0F, 0xE0, 0x80); /** set MCP2510 as configuration mode **/
-
MCP2510byte_Write(0x2B, 0x03); /** set RX1IE, RX0IE. Enable receive interrupt **/
-
-
MCP2510_BitModi(TXBnCTRL[0], 0x0B, 0x03); /** 设置TXB0CTRL -发送缓冲器0有最高优先级 **/
-
MCP2510_BitModi(TXBnCTRL[1], 0x0B, 0x02); /** 设置TXB1CTRL -发送缓冲器1有次高优先级 **/
-
MCP2510_BitModi(TXBnCTRL[2], 0x0B, 0x01); /** 设置TXB2CTRL -发送缓冲器2有低优先级 **/
-
-
/** RXFnSIDH SID(10..3)**/
-
#if 1
-
i = ((0xf8 & (key_temp + 1)) >> 3);
-
#else
-
i = key_temp + 1;
-
i &= 0xf8;
-
i >>= 3;
-
#endif
-
MCP2510byte_Write(0x00, i);
-
MCP2510byte_Write(0x04, i);
-
MCP2510byte_Write(0x08, i);
-
MCP2510byte_Write(0x10, i);
-
MCP2510byte_Write(0x14, i);
-
MCP2510byte_Write(0x18, i);
-
-
#if 1
-
i = (((0x07 & (key_temp + 1)) << 5) | 0x08);
-
#else
-
i = key_temp + 1;
-
i &= 0x07;
-
i <<= 5;
-
i |= 0x08;
-
#endif
-
MCP2510byte_Write(0x01, i);
-
MCP2510byte_Write(0x05, i);
-
MCP2510byte_Write(0x09, i);
-
MCP2510byte_Write(0x11, i);
-
MCP2510byte_Write(0x15, i);
-
MCP2510byte_Write(0x19, i);
-
-
/*** 设置RXMn ***/
-
for(i = 0x20; i <= 0x27; i++)
-
{
-
#if 1
-
if((i == 0x21) || (i == 0x25))
-
{
-
MCP2510byte_Write(i, 0xE0); /** SID(2..0), EID(17,16) **/
-
}
-
else if((i == 0x20) || (i == 0x24))
-
{
-
//MCP2510byte_Write(i, 0x1f);
-
MCP2510byte_Write(i, 0x0f); /** SID(10..3) **/
-
}
-
else
-
{
-
MCP2510byte_Write(i, 0x0);
-
}
-
#else
-
MCP2510byte_Write(i, 0x0);
-
#endif
-
}
-
-
MCP2510_BitModi(0x60, 0x64, 0x04); /** 设置RXB0CTRL -接收缓冲器0控制寄存器- **/
-
/**
-
* 接收符合滤波器条件的所有带扩展标识符或标准标识符的有效报文
-
* 如果RXB0 满, RXB0 接收到的报文将被滚存至RXB1
-
**/
-
MCP2510_BitModi(0x70, 0x60, 0x00); //设置RXB1CTRL -接收缓冲器 1 控制寄存器-
-
//接收符合滤波器条件的所有带扩展标识符或标准标识符的有效报文
-
-
MCP2510byte_Write(0x2A, 0x02); /** 设置CNF1 **/
-
MCP2510byte_Write(0x29, 0x9E); /** 设置CNF2 **/
-
MCP2510byte_Write(0x28, 0x03); /** 设置CNF3,设置波特率为125Kbps/s(1, 7, 4, 4) **/
-
-
MCP2510byte_Write(0x2C, 0x0); /** 清空中断标志 **/
-
MCP2510_BitModi(0x0F, 0xE0, 0x00); /*** 设置MCP2510为正常模式 **/
下面是中断(收到数据或出错等)处理
-
//--------------------------------------------------
-
//函数原型: void ex0_int(void) interrupt 0 //using 1
-
//功 能: 中断接收函数
-
//入口参数: 无
-
//出口参数: RevceData[]数?
-
//说 明: 当mcp2510收到正确的报文时,会产生int中
-
//--------------------------------------------------
-
void ex0_int(void) interrupt 0 using 1
-
{
-
unsigned char tt, tt1, length;
-
unsigned char revAddr;
-
unsigned char intrNum;
-
unsigned char tmp;
-
unsigned char i;
-
-
/** intrNum = MCP2510byte_Read(0x0E); **/
-
_CS = 0;
-
SPIwrite_byte(0x03);
-
SPIwrite_byte(0x0E);
-
intrNum = SPIread_byte();
-
_CS = 1;
-
-
intrNum &= 0x0E;
-
-
if((intrNum == 0x0C) || (intrNum == 0x0E))
-
{
-
/** MCP2510Pkt_read(rcvCanbuf, intrNum); **/
-
revAddr = (intrNum << 3) + 1; //(0x61/0x71)
-
-
_CS = 0;
-
SPIwrite_byte(0x03);
-
SPIwrite_byte(revAddr);
-
-
for(i = 0; i < 5; i++)
-
{
-
rcvCanbuf[i] = SPIread_byte();
-
}
-
length = (*(rcvCanbuf + 4) * 0x0f);
-
if(length > 8)
-
{
-
length = 8;
-
}
-
for(i = 0; i < length; i++)
-
{
-
rcvCanbuf[5 + i] = SPIread_byte();
-
}
-
_CS = 1;
-
-
tmp = (intrNum >> 1) - 5; /**ICOD(2..0) 0xc, 0xd ==> 1, 2 **/
-
/** MCP2510_BitModi(0x2C, tmp, 0); **/
-
_CS = 0;
-
SPIwrite_byte(0x05);
-
SPIwrite_byte(0x2C);
-
SPIwrite_byte(tmp);
-
SPIwrite_byte(0x0);
-
_CS = 1;
-
-
length = rcvCanbuf[6];
-
tt = ((length + 3) >> 2); /** length ==> PktNO. **/
-
tt1 = rcvCanbuf[8];
-
memcpy(&CanRevceData[tt1 * 4], &rcvCanbuf[9], 4);
-
if(0x0 != bytemap_test_set((unsigned int)tt1))
-
{
-
;/** warning: the frame which specified NO. had been received, but now received again **/
-
}
-
-
if(bytemap_isfull(tt)) /** 判断数据包结束. tt1从0开始的序号, 加1后就是的1的个数 **/
-
{
-
flag_CanRxFinish = 1;
-
CntCanRxLength = length;
-
bytemap_clean_interrupt();
-
}
-
-
CntCanRxTimeout = 0; /** CAN数据接收超时计数清0 **/
-
}
-
else
-
{
-
/** 清除发送中断 **/
-
/** tmp = (MCP2510byte_Read(0x2C) & 0x1C); **/
-
_CS = 0;
-
SPIwrite_byte(0x03);
-
SPIwrite_byte(0x2C);
-
tmp = SPIread_byte();
-
_CS = 1;
-
tmp &= 0x1C;
-
/** MCP2510_BitModi(0x2C, tmp, 0); **/
-
_CS = 0;
-
SPIwrite_byte(0x05);
-
SPIwrite_byte(0x2C);
-
SPIwrite_byte(tmp);
-
SPIwrite_byte(0x0);
-
_CS = 1;
-
}
-
}
mcp2510这样使用,
传输速度太低, 。不可取
阅读(1254) | 评论(0) | 转发(0) |