使用Ardunio扩展板上的SPI-CAN电路来实现CAN通信。STM32H743本身自带CAN通信,板子没有CAN收发芯片,使用扩展板来实现CAN通信功能。
一、硬件电路
1.1、STM32H743电路图部分
1.2、Ardunio扩展板CAN部分电路
1.3、整体硬件连接
二、程序部分
2.1、mcp2515.c
#include "config.h"
void SPI0_WRITE(uchar dat)
{
SPI1_ReadWriteByte(dat);
}
void MCP2515_Reset(void)
{
can_cs_l();
SPI0_WRITE(CAN_RESET);
can_cs_h();
}
void MCP2515_BitModity(uchar addr, uchar msk, uchar dat)
{
can_cs_l();
SPI0_WRITE(CAN_BIT_MODIFY);
SPI0_WRITE(addr);
SPI0_WRITE(msk);
SPI0_WRITE(dat);
can_cs_h();
return;
}
void SPI0_WriteByte(uchar adr,uchar dat)
{
can_cs_l();
SPI0_WRITE(CAN_WRITE);
SPI0_WRITE(adr);
SPI0_WRITE(dat);
can_cs_h();
}
void CAN_Send(uchar *CAN_TX_Buf)
{
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_l();
SPI0_WRITE(CAN_RTS | 0x01);
can_cs_h();
}
void Init_mcp2515(void)
{
uint16_t i;
MCP2515_Reset();
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_l();
SPI0_WRITE(CAN_READ); //???
SPI0_WRITE(adr); //???
//rxdat=SPI0_READ();
rxdat=SPI1_ReadWriteByte(0x00);
can_cs_h();
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);
CAN_RX_Buf[8]=SPI0_ReadByte(RXB0SIDH);
CAN_RX_Buf[9]=SPI0_ReadByte(RXB0SIDL);
CAN_RX_Buf[10]=SPI0_ReadByte(RXB0EID8);
CAN_RX_Buf[11]=SPI0_ReadByte(RXB0EID0);
CAN_RX_Buf[12]=SPI0_ReadByte(RXB0DLC);
SPI0_WriteByte(CANINTF,0);
}
void clearcanintf(void)
{
SPI0_WriteByte(CANINTF,0);
}
2.2、main.c
#include "config.h"
int main(void)
{
uint8_t txdat[100]="www.eeworld.com.cn-stm32h743 usart test\r\n";
uint8_t rxdat[100];
uint8_t can_tx_buf[8];
uint8_t i=0;
uint8_t js=0;
uint8_t jsdat=0;
uint8_t t;
uint8_t str[6]={'0','0','0','.','0','m'};
CPU_CACHE_Enable();
HAL_Init();
SystemClock_Config();
LED_Init();
EXTI_Init();
spi_init();
KEY_Init();
UART_Init();
BUS_Init();
Init_lcd();
Init_mcp2515();
while (1)
{
if(rx_flag==1)
{
rx_flag=0;
CAN_RecRXB(can_rx_buf);
CAN_Send(can_rx_buf);
HAL_UART_Transmit(&UartHandle, can_rx_buf, 8, 100);
}
}
}
2.3、程序源码
三、执行结果
3.1、CAN收发测试
3.2、串口输出CAN收到的数据
本帖最后由 TL-LED 于 2020-6-1 14:36 编辑