【 ST NUCLEO-H743ZI测评】+ 5、CAN通信收发测试
<div class='showpostmsg'> 本帖最后由 TL-LED 于 2020-6-1 14:36 编辑<p><span style="font-size:16px;"> 使用Ardunio扩展板上的SPI-CAN电路来实现CAN通信。STM32H743本身自带CAN通信,板子没有CAN收发芯片,使用扩展板来实现CAN通信功能。</span></p>
<p> </p>
<p><span style="font-size:16px;"><strong> 一、硬件电路</strong></span></p>
<p><span style="font-size:16px;"> 1.1、STM32H743电路图部分</span></p>
<p><span style="font-size:16px;"> 使用SPI1的PA5,PA6,PA7实现SPI通信。<br />
</span></p>
<p> </p>
<p><span style="font-size:16px;"> 1.2、Ardunio扩展板CAN部分电路</span></p>
<p><span style="font-size:16px;"> </span></p>
<p> </p>
<p><span style="font-size:16px;"> 1.3、整体硬件连接</span></p>
<p> </p>
<p> </p>
<p> </p>
<p><strong><span style="font-size:16px;"> 二、程序部分</span></strong></p>
<p> </p>
<p><span style="font-size:16px;"> 2.1、mcp2515.c</span></p>
<pre>
<code>#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);
SPI0_WriteByte(TXB0D1,CAN_TX_Buf);
SPI0_WriteByte(TXB0D2,CAN_TX_Buf);
SPI0_WriteByte(TXB0D3,CAN_TX_Buf);
SPI0_WriteByte(TXB0D4,CAN_TX_Buf);
SPI0_WriteByte(TXB0D5,CAN_TX_Buf);
SPI0_WriteByte(TXB0D6,CAN_TX_Buf);
SPI0_WriteByte(TXB0D7,CAN_TX_Buf);
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=SPI0_ReadByte(RXB0D0);
CAN_RX_Buf=SPI0_ReadByte(RXB0D1);
CAN_RX_Buf=SPI0_ReadByte(RXB0D2);
CAN_RX_Buf=SPI0_ReadByte(RXB0D3);
CAN_RX_Buf=SPI0_ReadByte(RXB0D4);
CAN_RX_Buf=SPI0_ReadByte(RXB0D5);
CAN_RX_Buf=SPI0_ReadByte(RXB0D6);
CAN_RX_Buf=SPI0_ReadByte(RXB0D7);
CAN_RX_Buf=SPI0_ReadByte(RXB0SIDH);
CAN_RX_Buf=SPI0_ReadByte(RXB0SIDL);
CAN_RX_Buf=SPI0_ReadByte(RXB0EID8);
CAN_RX_Buf=SPI0_ReadByte(RXB0EID0);
CAN_RX_Buf=SPI0_ReadByte(RXB0DLC);
SPI0_WriteByte(CANINTF,0);
}
void clearcanintf(void)
{
SPI0_WriteByte(CANINTF,0);
}</code></pre>
<p> </p>
<p><span style="font-size:16px;"> 2.2、main.c</span></p>
<pre>
<code>#include "config.h"
int main(void)
{
uint8_t txdat="www.eeworld.com.cn-stm32h743 usart test\r\n";
uint8_t rxdat;
uint8_t can_tx_buf;
uint8_t i=0;
uint8_t js=0;
uint8_t jsdat=0;
uint8_t t;
uint8_t str={'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);
}
}
}</code></pre>
<p> </p>
<p><span style="font-size:16px;"> 2.3、程序源码</span></p>
<p> </p>
<p> </p>
<p><span style="font-size:16px;"> <strong>三、执行结果</strong></span></p>
<p> <br />
<span style="font-size:16px;"> 3.1、CAN收发测试</span></p>
<p> </p>
<p> </p>
<p><span style="font-size:16px;"> 3.2、串口输出CAN收到的数据</span></p>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> 感谢分享 <table cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>感谢分享</td>
</tr>
</tbody>
</table>
页:
[1]