最近公司做一个项目,需要3路can通道,但是stm32看了很久,最多也就只有2个can,所以找到了一款MCP2515芯片,可以用spi驱动can。
已经实现了can的发送和接收,接收采用的是外部中断接收的方式。和单片机本身带的can功能一样。
还有就是要注意的是CAN的接收是通过外部中断引脚下拉来确定接收缓冲器接收到数据,可以通过设置接收屏蔽和滤波。
spi的初始化就不介绍 直接放出MCP2515的关键代码
首先是MCP2515的头文件 主要是一些宏定义和API文章来源:https://www.toymoban.com/news/detail-534654.html
#ifndef MCP2515_H_
#define MCP2515_H_
#include "stm32f10x.h"
#define USE_INT_STAT 1 // use INT pin indicate recived data
//MCP2515 RX/TX buffers selection
#define MCP2515_RX_BUF_0 0
#define MCP2515_RX_BUF_1 1
#define MCP2515_TX_BUF_0 0
#define MCP2515_TX_BUF_1 1
#define MCP2515_TX_BUF_2 2
//MCP2515 SPI instructions
#define MCP2515_RESET 0xC0
#define MCP2515_READ 0x03
#define MCP2515_READ_BUF_RXB0SIDH 0x90 //read data from begin address of Standard ID (MSB) of RX buffer 0
#define MCP2515_READ_BUF_RXB0D0 0x92 //read data from begin address of data byte 0 of RX buffer 0
#define MCP2515_READ_BUF_RXB1SIDH 0x94 //...RX buffer 1
#define MCP2515_READ_BUF_RXB1D0 0x96 //...RX buffer 1
#define MCP2515_WRITE 0x02
#define MCP2515_LOAD_BUF_TXB0SIDH 0x40 //load data from begin address of Standard ID (MSB) of TX buffer 0
#define MCP2515_LOAD_BUF_TXB0D0 0X41 //load data from begin address of data byte 0 of TX buffer 0
#define MCP2515_LOAD_BUF_TXB1SIDH 0x42 //...TX buffer 1
#define MCP2515_LOAD_BUF_TXB1D0 0X43 //...TX buffer 1
#define MCP2515_LOAD_BUF_TXB2SIDH 0x44 //...TX buffer 2
#define MCP2515_LOAD_BUF_TXB2D0 0X45 //...TX buffer 2
#define MCP2515_RTS_TXB0 0x81 //activate the RTS of TX buffer 0.
#define MCP2515_RTS_TXB1 0x82 //...TX buffer 1.
#define MCP2515_RTS_TXB2 0x84 //...TX buffer 2.
#define MCP2515_RTS_ALL 0x87 //...TX buffer 0, 1 and 2.
#define MCP2515_READ_RXTX_STATUS 0xA0
#define MCP2515_RX_STATUS 0xB0
#define MCP2515_BIT_MODIFY 0x05
//MCP2515 control settings register address.
//All can be used with the Bit Modify command, except the CANSTAT register.
#define MCP2515_BFPCTRL 0x0C //RXnBF pin control/status
#define MCP2515_TXRTSCTRL 0x0D //TXnRTS pin control/status
#define MCP2515_CANSTAT 0x0E //CAN status. any addr of MSB will give the same info???
#define MCP2515_CANCTRL 0x0F //CAN control status. any addr of MSB will give the same info???
#define MCP2515_TEC 0x1C //Transmit Error Counter
#define MCP2515_REC 0x1D //Receive Error Counter
#define MCP2515_CNF3 0x28 //Phase segment 2
#define MCP2515_CNF2 0x29 //Propagation segment & Phase segment 1 & n sample setting
#define MCP2515_CNF1 0x2A //Baud rate prescaler & Sync Jump Width
#define MCP2515_CANINTE 0x2B //CAN interrupt enable
#define MCP2515_CANINTF 0x2C //Interrupt flag
#define MCP2515_EFLG 0x2D //Error flag
#define MCP2515_TXB0CTRL 0x30 //TX buffer 0 control
#define MCP2515_TXB1CTRL 0x40 //TX buffer 1 control
#define MCP2515_TXB2CTRL 0x50 //TX buffer 2 control
#define MCP2515_RXB0CTRL 0x60 //RX buffer 0 control
#define MCP2515_RXB1CTRL 0x70 //RX buffer 1 control
//MCP2515 relate to CAN settings/status register address.
//Only the most used are listed below. please see the datasheet MCP2515.pdf p61 for complete info.
#define MCP2515_RXF0SIDH 0x00 //RX standard ID (High bits) filter 0
#define MCP2515_RXF0SIDL 0x01 //RX standard ID (Low bits) filter 0
#define MCP2515_RXF0EID8 0x02 //RX Extended ID (High bits) filter 0 ->can be reached by bust read/write Standard ID then Ext. ID
#define MCP2515_RXF0EID0 0x03 //RX Extended ID (Low bits) filter 0
#define MCP2515_RXF1SIDH 0x04 //RX standard ID (High bits) filter 1
#define MCP2515_RXF1SIDL 0x05 //RX standard ID (Low bits) filter 1
#define MCP2515_RXF1EID8 0x06 //RX Extended ID (High bits) filter 1
#define MCP2515_RXF1EID0 0x07 //RX Extended ID (Low bits) filter 1
#define MCP2515_RXF2SIDH 0x08 //RX standard ID (High bits) filter 2
#define MCP2515_RXF2SIDL 0x09 //RX standard ID (Low bits) filter 2
#define MCP2515_RXF2EID8 0x0A //RX Extended ID (High bits) filter 2
#define MCP2515_RXF2EID0 0x0B //RX Extended ID (Low bits) filter 2
#define MCP2515_RXF3SIDH 0x10 //RX standard ID (High bits) filter 3
#define MCP2515_RXF3SIDL 0x11 //RX standard ID (Low bits) filter 3
#define MCP2515_RXF3EID8 0x12 //RX Extended ID (High bits) filter 3
#define MCP2515_RXF3EID0 0x13 //RX Extended ID (Low bits) filter 3
#define MCP2515_RXF4SIDH 0x14 //RX standard ID (High bits) filter 4
#define MCP2515_RXF4SIDL 0x15 //RX standard ID (Low bits) filter 4
#define MCP2515_RXF4EID8 0x16 //RX Extended ID (High bits) filter 4
#define MCP2515_RXF4EID0 0x17 //RX Extended ID (Low bits) filter 4
#define MCP2515_RXF5SIDH 0x18 //RX standard ID (High bits) filter 5
#define MCP2515_RXF5SIDL 0x19 //RX standard ID (Low bits) filter 5
#define MCP2515_RXF5EID8 0x1A //RX Extended ID (High bits) filter 5
#define MCP2515_RXF5EID0 0x1B //RX Extended ID (Low bits) filter 5
#define MCP2515_RXM0SIDH 0x20 //RX standard ID (High bits) mask filter 0
#define MCP2515_RXM0SIDL 0x21 //RX standard ID (Low bits) mask filter 0
#define MCP2515_RXM0EID8 0x22 //RX Extended ID (High bits) mask filter 0
#define MCP2515_RXM0EID0 0x23 //RX Extended ID (Low bits) mask filter 0
#define MCP2515_RXM1SIDH 0x24 //RX standard ID (High bits) mask filter 1
#define MCP2515_RXM1SIDL 0x25 //RX standard ID (Low bits) mask filter 1
#define MCP2515_RXM1EID8 0x26 //RX Extended ID (High bits) mask filter 1
#define MCP2515_RXM1EID0 0x27 //RX Extended ID (Low bits) mask filter 1
#define MCP2515_RXB0SIDH 0x61 //RX buffer 0 standard ID (High bits)
#define MCP2515_RXB0SIDL 0x62 //RX buffer 0 standard ID (Low bits)
#define MCP2515_RXB0EID8 0x63 //RX buffer 0 Extended ID (High bits) ->can be reached by bust read/write Standard ID then Ext. ID
#define MCP2515_RXB0EID0 0x64 //RX buffer 0 Extended ID (Low bits)
#define MCP2515_RXB0DLC 0x65 //RX buffer 0 DLC ->can be reached by bust read/write Standard ID, Ext. ID then DLC
#define MCP2515_RXB0D0 0x66 //RX buffer 0 data byte0
#define MCP2515_RXB1SIDH 0x71 //RX buffer 1 standard ID (High bits)
#define MCP2515_RXB1SIDL 0x72 //RX buffer 1 standard ID (Low bits)
#define MCP2515_RXB1EID8 0x73 //RX buffer 1 Extended ID (High bits)
#define MCP2515_RXB1EID0 0x74 //RX buffer 1 Extended ID (Low bits)
#define MCP2515_RXB1DLC 0x75 //RX buffer 1 DLC
#define MCP2515_RXB1D0 0x76 //RX buffer 1 data byte0
#define MCP2515_TXB0SIDH 0x31 //TX buffer 0 standard ID (High bits)
#define MCP2515_TXB0SIDL 0x32 //TX buffer 0 standard ID (Low bits)
#define MCP2515_TXB0EID8 0x33 //TX buffer 0 Extended ID (High bits) ->can be reached by bust read/write Standard ID then Ext. ID
#define MCP2515_TXB0EID0 0x34 //TX buffer 0 Extended ID (Low bits)
#define MCP2515_TXB0DLC 0x35 //TX buffer 0 DLC ->can be reached by bust read/write Standard ID, Ext. ID then DLC
#define MCP2515_TXB0D0 0x36 //TX buffer 0 data byte0
#define MCP2515_TXB1SIDH 0x41 //TX buffer 1 standard ID (High bits)
#define MCP2515_TXB1SIDL 0x42 //TX buffer 1 standard ID (Low bits)
#define MCP2515_TXB1EID8 0x43 //TX buffer 1 Extended ID (High bits)
#define MCP2515_TXB1EID0 0x44 //TX buffer 1 Extended ID (Low bits)
#define MCP2515_TXB1DLC 0x45 //TX buffer 1 DLC
#define MCP2515_TXB1D0 0x46 //TX buffer 1 data byte0
#define MCP2515_TXB2SIDH 0x51 //TX buffer 2 standard ID (High bits)
#define MCP2515_TXB2SIDL 0x52 //TX buffer 2 standard ID (Low bits)
#define MCP2515_TXB2EID8 0x53 //TX buffer 2 Extended ID (High bits)
#define MCP2515_TXB2EID0 0x54 //TX buffer 2 Extended ID (Low bits)
#define MCP2515_TXB2DLC 0x55 //TX buffer 2 DLC
#define MCP2515_TXB2D0 0x56 //TX buffer 2 data byte0
//MCP2515 limit values
#define MCP2515_MIN_TQ 0x07 //7 = Minimum TQ in 1 bit of CAN bus time
#define MCP2515_MAX_TQ 0x19 //25 = Maximum TQ in 1 bit of CAN bus time
#define MCP2515_MIN_BRP 0x00 //0 = Minimum baud rate prescaler clock
#define MCP2515_MAX_BRP 0x3F //63 = Maximum baud rate prescaler clock
#define MCP2515_MAX_SJW 0x03 //4 = Maximum Synchronization Jump Width. 4-1 = 3 actually
#define MCP2515_MAX_BYTE_CANFRM 0x08//8 = Maximun bytes in a CAN frame
//MCP2515 register values: CANCTRL register
#define MCP2515_MODE_NORMAL 0x00
#define MCP2515_MODE_SLEEP 0x20
#define MCP2515_MODE_LOOPBACK 0x40
#define MCP2515_MODE_LISTENONLY 0x60
#define MCP2515_MODE_CONFIG 0x80
#define MCP2515_ABORT_TX 0x10
#define MCP2515_MODE_MASK 0xE0
#define MCP2515_MODE_ONESHOT 0x08
#define MCP2515_CLKOUT_ENABLE 0x04
#define MCP2515_CLKOUT_PS1 0x00 //Set CLK out prescaler to 1. Note: not the same as the CAN CLK prescaler.
#define MCP2515_CLKOUT_PS2 0x01 //... to 2
#define MCP2515_CLKOUT_PS4 0x02 //... to 4
#define MCP2515_CLKOUT_PS8 0x03 //... to 8
//MCP2515 CAN Status Register bits (CANSTAT 0xXE, ICOD)
#define MCP2515_CANSTAT_NO_INT 0x00
#define MCP2515_CANSTAT_ERR_INT 0x02
#define MCP2515_CANSTAT_WAK_INT 0x04 //Wake-up Interrupt
#define MCP2515_CANSTAT_TXB0_INT 0x06
#define MCP2515_CANSTAT_TXB1_INT 0x08
#define MCP2515_CANSTAT_TXB2_INT 0x0A
#define MCP2515_CANSTAT_RXB0_INT 0x0C
#define MCP2515_CANSTAT_RXB1_INT 0x0E
//MCP2515 CAN Interrupt Flag Define
#define MCP2515_CANINTF_MERRF 0x80
#define MCP2515_CANINTF_WAKIF 0x40
#define MCP2515_CANINTF_ERRIF 0x20
#define MCP2515_CANINTF_TX2IF 0x10
#define MCP2515_CANINTF_TX1IF 0x08
#define MCP2515_CANINTF_TX0IF 0x04
#define MCP2515_CANINTF_RX1IF 0x02
#define MCP2515_CANINTF_RX0IF 0x01
#define MCP2515_CANINT_MERR 0x80
#define MCP2515_CANINT_WAKI 0x40
#define MCP2515_CANINT_ERRI 0x20
#define MCP2515_CANINT_TX2I 0x10
#define MCP2515_CANINT_TX1I 0x08
#define MCP2515_CANINT_TX0I 0x04
#define MCP2515_CANINT_RX1I 0x02
#define MCP2515_CANINT_RX0I 0x01
#define MCP2515_CAN_Standard (0) /*!< Standard Id */
#define MCP2515_CAN_Extended (8) /*!< Extended Id */
/**
* @brief MCP2515_CAN Tx message structure definition
*/
typedef struct
{
uint32_t StdId; /*!< Specifies the standard identifier.
This parameter can be a value between 0 to 0x7FF. */
uint32_t ExtId; /*!< Specifies the extended identifier.
This parameter can be a value between 0 to 0x1FFFFFFF. */
unsigned char IDE; /*!< Specifies the type of identifier for the message that
will be transmitted. This parameter can be a value
of @ref CAN_identifier_type */
unsigned char RTR; /*!< Specifies the type of frame for the message that will
be transmitted. This parameter can be a value of
@ref CAN_remote_transmission_request */
unsigned char DLC; /*!< Specifies the length of the frame that will be
transmitted. This parameter can be a value between
0 to 8 */
unsigned char Data[8]; /*!< Contains the data to be transmitted. It ranges from 0
to 0xFF. */
}MCP2515_CanTxMsg;
/**
* @brief MCP2515_CAN Rx message structure definition
*/
typedef struct
{
uint32_t StdId; /*!< Specifies the standard identifier.
This parameter can be a value between 0 to 0x7FF. */
uint32_t ExtId; /*!< Specifies the extended identifier.
This parameter can be a value between 0 to 0x1FFFFFFF. */
unsigned char IDE; /*!< Specifies the type of identifier for the message that
will be received. This parameter can be a value of
@ref CAN_identifier_type */
unsigned char RTR; /*!< Specifies the type of frame for the received message.
This parameter can be a value of
@ref CAN_remote_transmission_request */
unsigned char DLC; /*!< Specifies the length of the frame that will be received.
This parameter can be a value between 0 to 8 */
unsigned char Data[8]; /*!< Contains the data to be received. It ranges from 0 to
0xFF. */
unsigned char FMI; /*!< Specifies the index of the filter the message stored in
the mailbox passes through. This parameter can be a
value between 0 to 0xFF */
}MCP2515_CanRxMsg;
extern MCP2515_CanRxMsg MCP2515_Rxmsg;
extern MCP2515_CanTxMsg MCP2515_Txmsg;
/*KEY API*/
void MCP2515_CAN_Init(unsigned char brp,unsigned char ps1,unsigned char ps2);
unsigned char MCP2515_CAN_ReceiveData(MCP2515_CanRxMsg * rxmsg);
void MCP2515_CAN_SendData(MCP2515_CanTxMsg *txmsg);
void CANINT_GPIO_Init(void);
/*General API*/
void MCP2515_Reset(void);
unsigned char MCP2515_ReadByte(unsigned char addr);
void MCP2515_WriteByte(unsigned char addr,unsigned char data);
void MCP2515_BitModify(unsigned char Addr, unsigned char MASK, unsigned char data);
uint8_t MCP2515_Get_ReadStatus(void);
uint8_t MCP2515_Get_RxStatus(void);
void MCP2515_RTS(unsigned char TxBn);
/*Set MCP2515*/
void MCP2515_SetCNF1(unsigned char data);
void MCP2515_SetCNF2(unsigned char data);
void MCP2515_SetCNF3(unsigned char data);
void MCP2515_SetMode(unsigned char data);
unsigned char MCP2515_GetCanStatus(void);
/*TX API*/
void MCP2515_SetTXID(unsigned char TxBnID,MCP2515_CanTxMsg *txmsg);
void MCP2515_SetTXDLC(unsigned char TxBnDLC,MCP2515_CanTxMsg *txmsg);
void MCP2515_SetTXDATA(unsigned char TxBnDm,MCP2515_CanTxMsg *txmsg);
/*RX API*/
#endif /*MCP2515_H_*/
接下来就是MCP2515的源文件代码 和头文件是对应的文章来源地址https://www.toymoban.com/news/detail-534654.html
#include "mcp2515.h"
#include "spi.h"
#include "System/Utils.h"
#include "GPIO/GPIO.h"
#include <stdio.h>
MCP2515_CanRxMsg MCP2515_Rxmsg;
MCP2515_CanTxMsg MCP2515_Txmsg;
void EXTI9_5_IRQHandler(void)
{
if(!GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_9))
{
/*can中断*/
MCP2515_CAN_ReceiveData(&MCP2515_Rxmsg);
}
EXTI_ClearITPendingBit(EXTI_Line9); //清除LINE9上的中断标志位
}
void CANINT_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
GPIO_Init(GPIOD, &GPIO_InitStructure);
//中断线以及中断初始化配置 下降沿触发
GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource9);
EXTI_InitStructure.EXTI_Line=EXTI_Line9;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*********
* @brief MCP2515_CAN_Init
* @param brp:0~63
* @param ps1:0~7
* @param ps2:1~7
* Baudrate=Fosc/( 2+ (ps1+1) + (ps2+1) ) * 2*(1+brp) )
* eg:brp=1 ps1=3 ps2=1
Baudrate=8Mhz/(2+(3+1)+(1+1))*2*(1+1) =250Khz
* eg:brp=0 ps1=3 ps2=1
Baudrate=8Mhz/(2+(1+1)+(1+1))*2*(1+1) =250Khz
* @retval none
*/
void MCP2515_CAN_Init(unsigned char brp,unsigned char ps1,unsigned char ps2)
{
这个是CAN初始化的代码 主要是配置CAN的波特率 模式 通道等
具体代码可以小窗找我
}
/*********
* @brief Receive n bytes from MCP2515 RXB0
* @param rxmsg:RxDataStruct
* @retval Data:return the effectively data from RXB0
*/
unsigned char MCP2515_CAN_ReceiveData(MCP2515_CanRxMsg * rxmsg)
{
这个是CAN的接收函数 可以在中断中来读取这个API 得到接收的can数据
具体代码可以小窗找我
}
/*********
* @brief Send n bytes to MCP2515 txb0
* @param txmsg: data to be sent
* @retval None
*/
void MCP2515_CAN_SendData(MCP2515_CanTxMsg *txmsg)
{
这个是CAN的发送函数 代入CAN的结构体参数就可以
具体代码可以小窗找我
}
//功能:复位MCP2515
void MCP2515_Reset(void)
{
MC2515_CS(0);
SPI_MCP2515_Write_Read(MCP2515_RESET);
MC2515_CS(1);
}
//功能:读MCP2515的数据
//入口参数:Addr:寄存器地址
//返回值:寄存器地址中的数据
unsigned char MCP2515_ReadByte(unsigned char addr)
{
unsigned char ReadData;
MC2515_CS(0);
SPI_MCP2515_Write_Read(MCP2515_READ);
SPI_MCP2515_Write_Read(addr);
ReadData=SPI_MCP2515_Write_Read(0xff);
MC2515_CS(1);
return ReadData;
}
//功能:读MCP2515的寄存器
//入口参数:Addr:寄存器地址 data:数据字节
void MCP2515_WriteByte(unsigned char addr,unsigned char data)
{
MC2515_CS(0);
SPI_MCP2515_Write_Read(MCP2515_WRITE);
SPI_MCP2515_Write_Read(addr);
SPI_MCP2515_Write_Read(data);
MC2515_CS(1);
}
//功能:只修改寄存器中的某些位
//入口参数:Addr:寄存器地址 MASK:屏蔽字 data:数据字节
void MCP2515_BitModify(unsigned char Addr, unsigned char MASK, unsigned char data)
{
MC2515_CS(0);
SPI_MCP2515_Write_Read(MCP2515_BIT_MODIFY);
SPI_MCP2515_Write_Read(Addr);
SPI_MCP2515_Write_Read(MASK);
SPI_MCP2515_Write_Read(data);
MC2515_CS(1);
}
//功能:获取读状态指令中的数据
//返回值:读状态指令中的数据
uint8_t MCP2515_Get_ReadStatus(void)
{
unsigned char ReadData;
MC2515_CS(0);
ReadData=SPI_MCP2515_Write_Read(MCP2515_READ_RXTX_STATUS);
MC2515_CS(1);
return ReadData;
}
//功能:获取读状态指令中的数据
//返回值:读状态指令中的数据
uint8_t MCP2515_Get_RxStatus(void)
{
unsigned char ReadData;
MC2515_CS(0);
ReadData=SPI_MCP2515_Write_Read(MCP2515_RX_STATUS);
MC2515_CS(1);
return ReadData;
}
//功能:请求发送功能
//入口参数:TxBn 发送缓冲器序号
//MCP2515_RTS_TXB0/MCP2515_RTS_TXB1/MCP2515_RTS_TXB2/MCP2515_RTS_ALL
void MCP2515_RTS(unsigned char TxBn)
{
MC2515_CS(0);
SPI_MCP2515_Write_Read(TxBn);
MC2515_CS(1);
}
/*
bit 7-6 SJW:同步跳转宽度位 <1:0>
11 = 长度 = 4 x TQ
10 = 长度 = 3 x TQ
01 = 长度 = 2 x TQ
00 = 长度 = 1 x TQ
bit 5-0 BRP:波特率预分频比位 <5:0>
TQ = 2 x (BRP + 1)/FOSC
*/
void MCP2515_SetCNF1(unsigned char data)
{
MCP2515_WriteByte(MCP2515_CNF1,data);
}
/*
bit 7 BTLMODE:相位缓冲段 PS2 位时间长度位
1 = PS2 时间长度由 CNF3 的 PHSEG22:PHSEG20 位决定
0 = PS2 时间长度为 PS1 和 IPT (2 TQ)两者的较大值
bit 6 SAM:采样点配置位
1 = 在采样点对总线进行三次采样
0 = 在采样点对总线进行一次采样
bit 5-3 PHSEG1:相位缓冲段 PS1 位时间长度位 <2:0>
(PHSEG1 + 1) x TQ
bit 2-0 PRSEG:传播段长度位 <2:0>
(PRSEG + 1) x TQ
*/
void MCP2515_SetCNF2(unsigned char data)
{
MCP2515_WriteByte(MCP2515_CNF2,data);
}
/*
bit 7 SOF:起始帧信号位
如果 CANCTRL.CLKEN = 1:
1 = CLKOUT 引脚使能为 SOF 信号
0 = CLKOUT 引脚使能为时间输出功能
如果 CANCTRL.CLKEN = 0,该位为任意状态 .
bit 6 WAKFIL:唤醒滤波使能位
1 = 唤醒滤波器使能
0 = 唤醒滤波器禁止
bit 2-0 PHSEG2:相位缓冲段 PS2 长度 <2:0>
(PHSEG2 + 1) x TQ 注: PS2 的最小有效值为 2 TQ
*/
void MCP2515_SetCNF3(unsigned char data)
{
MCP2515_WriteByte(MCP2515_CNF3,data);
}
/*
bit 7-5 REQOP<2:0>:请求工作模式的位
000 = 设定为正常工作模式
001 = 设定为休眠模式
010 = 设定为环回模式
011 = 设定为仅监听模式
100 = 设定为配置模式
REQOP 位不应设置为其他值,因为这些值都是无效的。
注: 上电时, REQOP = b’111’
bit 4 ABAT:中止所有当前报文发送的位
1 = 请求中止所有当前报文发送的缓冲器
0 = 终止对所有报文发送中止的请求
bit 3 OSM:单触发模式位
1 = 使能。报文仅尝试发送一次
0 = 禁止。如有需要,报文会重新发送。
bit 2 CLKEN:CLKOUT 引脚使能位
1 = CLKOUT 引脚使能
0 = CLKOUT 引脚禁止 (引脚处于高阻态)
bit 1-0 CLKPRE<1:0>:CLKOUT 引脚预分频比位
00 =FCLKOUT = 系统时钟频率 /1
01 =FCLKOUT = 系统时钟频率 /2
10 =FCLKOUT = 系统时钟频率 /4
11 =FCLKOUT = 系统时钟频率 /8
*/
void MCP2515_SetMode(unsigned char data)
{
MCP2515_WriteByte(MCP2515_CANCTRL,data);
}
/*
bit 7-5 OPMOD:工作模式位 <2:0>
000 = 器件处于正常工作模式
001 = 器件处于休眠模式
010 = 器件处于环回模式
011 = 器件处于仅监听模式
100 = 器件处于配置模式
bit 4 未用:读为 0
bit 3-1 ICOD:中断标志代码位 <2:0>
000 = 无中断
001 = 出错中断
010 = 唤醒中断
011 = TXB0 中断
100 = TXB1 中断
101 = TXB2 中断
110 = RXB0 中断
111 = RXB1 中断
bit 0 未用:读为 0
*/
unsigned char MCP2515_GetCanStatus(void)
{
return MCP2515_ReadByte(MCP2515_CANSTAT);
}
void MCP2515_SetTXID(unsigned char TxBnID,MCP2515_CanTxMsg *txmsg)
{
unsigned char HSID,LSID,EID8,EID0;
unsigned int SendId[2];
if(txmsg->IDE==MCP2515_CAN_Standard)
{
SendId[0]=txmsg->StdId;
HSID=(unsigned char)(SendId[0]>>3);
LSID=(unsigned char)((SendId[0]<<5)&0xE0);
MCP2515_WriteByte(MCP2515_TXB0SIDH,HSID);
MCP2515_WriteByte(MCP2515_TXB0SIDL,LSID);
MCP2515_WriteByte(MCP2515_TXB0DLC,txmsg->DLC);
}
else if(txmsg->IDE==MCP2515_CAN_Extended)
{
SendId[0]=(txmsg->ExtId)>>18;
SendId[1]=(txmsg->ExtId)&0x03FFFF;
HSID=(unsigned char)(SendId[0]>>3);
LSID=((unsigned char)((SendId[0]<<5)&0xE0) | MCP2515_CAN_Extended | (unsigned char)((SendId[1]>>16)&0x03) );
EID8=(unsigned char)(SendId[1]>>8);
EID0=(unsigned char)SendId[1];
// printf("\r\nHSID=%x HSID=%x EID8=%x EID0=%x",HSID,LSID,EID8,EID0);
MCP2515_WriteByte(MCP2515_TXB0SIDH,HSID);
MCP2515_WriteByte(MCP2515_TXB0SIDL,LSID);
MCP2515_WriteByte(MCP2515_TXB0EID8,EID8);
MCP2515_WriteByte(MCP2515_TXB0EID0,EID0);
}
}
void MCP2515_SetTXDLC(unsigned char TxBnDLC,MCP2515_CanTxMsg *txmsg)
{
if(txmsg->DLC>=8)txmsg->DLC=8;
MCP2515_WriteByte(TxBnDLC,txmsg->DLC|0x40);//默认数据帧
}
/*
TXBnDM7:TXBnDM0:发送缓冲器 n 数据字段字节 m
*/
void MCP2515_SetTXDATA(unsigned char TxBnDm,MCP2515_CanTxMsg *txmsg)
{
for(unsigned char i=0;i<txmsg->DLC;i++)
{
MCP2515_WriteByte(TxBnDm+i,txmsg->Data[i]);
}
}
到了这里,关于stm32驱动MCP2515芯片,项目已通过测试的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!