本帖最后由 tlled 于 2020-3-26 08:21 编辑
在上一帖子 【赛元95F】 + 10、SC95F8617 CAN发送测试 中增加接收功能。
一、代码
1.1、main.c
#include "config.h"
uchar can_tx_buf[8];
uchar can_rx_buf[14];
void main(void)
{
uint i=0;
uchar js=0;
Init_led();
Init_uart(32, 9600);
uart_chl(1); //usb-ttl
Init_enit();
Init_mcp2515();
while(1)
{
js++;
if(can_int_flag==1)
{
can_int_flag=0;
CAN_RecRXB(can_rx_buf);
CAN_Send(can_rx_buf);
}
}
}
1.2、mcp2515.c
#include "config.h"
bit SPI0Flag;
bit can_int_flag=0;
void SPI0_Init(void)
{
OTCON |= 0X10; //SPI0
US0CON0 = 0x3F;
US0CON1 = 0x01;
US0CON0 |= 0x80;
IE1 |= 0x01;
EA = 1;
}
void SpiInt(void) interrupt 7
{
if(US0CON1&0X08)
{
US0CON1 &= ~0X08;
}
if(US0CON1&0X80) //write eint
{
US0CON1 &= ~0X80;
SPI0Flag = 1;
}
}
void SPI0_WRITE(uchar dat)
{
US0CON2 = dat;
while(!SPI0Flag);
SPI0Flag = 0;
}
uchar SPI0_READ(void)
{
uchar rxdat;
SPI0_WRITE(0x00);
rxdat=US0CON2;;
return rxdat;
}
void MCP2515_Reset(void)
{
CAN_CS=0;
SPI0_WRITE(CAN_RESET);
CAN_CS=1;
}
void MCP2515_BitModity(uchar addr, uchar msk, uchar dat)
{
CAN_CS=0;
SPI0_WRITE(CAN_BIT_MODIFY);
SPI0_WRITE(addr);
SPI0_WRITE(msk);
SPI0_WRITE(dat);
CAN_CS=1;
return;
}
void SPI0_WriteByte(uchar adr,uchar dat)
{
CAN_CS=0;
SPI0_WRITE(CAN_WRITE);
SPI0_WRITE(adr);
SPI0_WRITE(dat);
CAN_CS=1;
}
void CAN_Send(uchar *CAN_TX_Buf)
{
uint i;
MCP2515_BitModity(TXB0CTRL,0x08,0x00);
SPI0_WriteByte(TXB0SIDH,0x55);
SPI0_WriteByte(TXB0SIDL,0x08);
SPI0_WriteByte(TXB0EID8,0x55);
SPI0_WriteByte(TXB0EID0,0x88);
SPI0_WriteByte(TXB0DLC,DLC_8);
SPI0_WriteByte(TXB0D0,CAN_TX_Buf[0]);
SPI0_WriteByte(TXB0D1,CAN_TX_Buf[1]);
SPI0_WriteByte(TXB0D2,CAN_TX_Buf[2]);
SPI0_WriteByte(TXB0D3,CAN_TX_Buf[3]);
SPI0_WriteByte(TXB0D4,CAN_TX_Buf[4]);
SPI0_WriteByte(TXB0D5,CAN_TX_Buf[5]);
SPI0_WriteByte(TXB0D6,CAN_TX_Buf[6]);
SPI0_WriteByte(TXB0D7,CAN_TX_Buf[7]);
CAN_CS=0;
SPI0_WRITE(CAN_RTS | 0x01);
CAN_CS=1;
}
void Init_mcp2515(void)
{
uint i;
P0CON &= ~0x02; //CANCS
P0CON |= 0x02;
P0PH |= 0x02;
SPI0_Init(); //SPI INIT
MCP2515_Reset();
for(i=0;i<5000;i++);
MCP2515_BitModity(CANCTRL,0xE0,0x80);
SPI0_WriteByte(CNF1,0x04); //100k
SPI0_WriteByte(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ);
SPI0_WriteByte(CNF3,PHSEG2_3TQ);
SPI0_WriteByte(CANINTF,0x00);
SPI0_WriteByte(CANINTE,0x03);
SPI0_WriteByte(RXB0CTRL,0x60);
//SPI0_WriteByte(RXB1CTRL,0x40);
SPI0_WriteByte(RXM0SIDH,0xff);
SPI0_WriteByte(RXM0SIDL,0xff);
SPI0_WriteByte(RXM0EID8,0xff);
SPI0_WriteByte(RXM0EID0,0xff);
// SPI0_WriteByte(RXM1SIDH,0xff);
// SPI0_WriteByte(RXM1SIDL,0xff);
// SPI0_WriteByte(RXM1EID8,0xff);
// SPI0_WriteByte(RXM1EID0,0xff);
SPI0_WriteByte( BFPCTRL, 0 );
SPI0_WriteByte( TXRTSCTRL, 0 );
MCP2515_BitModity(CANCTRL,0xE0,0x00);
}
uchar SPI0_ReadByte(uchar adr)
{
uchar rxdat;
CAN_CS=0;
SPI0_WRITE(CAN_READ); //???
SPI0_WRITE(adr); //???
rxdat=SPI0_READ();
CAN_CS=1;
return rxdat;
}
void CAN_RecRXB(uchar *CAN_RX_Buf)
{
CAN_RX_Buf[0]=SPI0_ReadByte(RXB0D0);
CAN_RX_Buf[1]=SPI0_ReadByte(RXB0D1);
CAN_RX_Buf[2]=SPI0_ReadByte(RXB0D2);
CAN_RX_Buf[3]=SPI0_ReadByte(RXB0D3);
CAN_RX_Buf[4]=SPI0_ReadByte(RXB0D4);
CAN_RX_Buf[5]=SPI0_ReadByte(RXB0D5);
CAN_RX_Buf[6]=SPI0_ReadByte(RXB0D6);
CAN_RX_Buf[7]=SPI0_ReadByte(RXB0D7);
SPI0_WriteByte(CANINTF,0);
}
1.3、eint.c
#include "config.h"
void Init_enit(void)
{
// P2CON &= ~0x08; //p2.3
//
// INT2F |= 0x08;
// INT2R |= 0x08;
P1CON &= ~0x20 ;//P1.5
//P1CON |= 0x20;
P1PH |= 0x20;
INT1F |= 0x20;
// INT1R |= 0x20;
IE |= 0x04;
EA=1;
}
void EX1() interrupt 2
{
if( (CAN_INT==0)||((CAN_INT==1)))
{
can_int_flag=1;
}
}
二、测试结果
CAN通信收到什么数据就发送什么数据。
|