stm32驱动MCP2515芯片,项目已通过测试

这篇具有很好参考价值的文章主要介绍了stm32驱动MCP2515芯片,项目已通过测试。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

最近公司做一个项目,需要3路can通道,但是stm32看了很久,最多也就只有2个can,所以找到了一款MCP2515芯片,可以用spi驱动can。
已经实现了can的发送和接收,接收采用的是外部中断接收的方式。和单片机本身带的can功能一样。
还有就是要注意的是CAN的接收是通过外部中断引脚下拉来确定接收缓冲器接收到数据,可以通过设置接收屏蔽和滤波。
spi的初始化就不介绍 直接放出MCP2515的关键代码
首先是MCP2515的头文件 主要是一些宏定义和API

#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模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 芯原第二代面向汽车应用的ISP系列IP已通过ISO 26262 ASIL B和ASIL D认证

    ISP8200-FS系列IP可满足快速增长的汽车市场持续演进的需求 2024年1月8日,美国拉斯维加斯——芯原股份(芯原,股票代码:688521.SH)今日宣布其专为高性能汽车应用而设计的图像信号处理器(ISP)IP ISP8200-FS和ISP8200L-FS已通过汽车功能安全标准ISO 26262认证,达到随机故障安全等级

    2024年01月23日
    浏览(48)
  • MCP4725介绍和STM32模拟IC2驱动

    简单总结为下面几个特点。 1路DAC输出 12位分辨率 I2C 接口(标准,快速,高速支持) 供电电压2.7-5.5 内部EEPROM存储设置 I2C地址可配置(A0)(A1、A2内置,默认为‘00’) MCP4725的管脚定义如图所示,比较简单 官方的电路图如下 一般上拉电阻选择10K就可以了,后级的比较器看

    2024年02月16日
    浏览(33)
  • 【DRV8323】电机驱动芯片寄存器配置指南,通过STM32F407的SPI通信配置

    笔者计划使用一块使用到STM32F407控制芯片与DRV8323s驱动芯片的板子,驱动BLDC。了解到需要使用SPI通信来配置DRV8323s驱动芯片,配置过程中涉及DRV8323数据手册中提及的几个寄存器,故写此文做个记录。 另外,DRV8323芯片和DRV8302、DRV8303、DRV8353都有极大的相似之处,可以相互参考

    2024年02月02日
    浏览(62)
  • rk3566 添加MCP2515模块

    MCP2515是SPI转CAN 模块 目标平台: 泰山派 rk3566(泰山派)的spi3各管脚连接MCP2515的SPI接口,另外MCP2515还需要一个中断引脚,用到的全部管脚如下: 其中GPIO3_A6为中断引脚 `` cd到SDK/kernel 目录下,执行 make ARCH=arm64 menuconfig ,打开相关的配置项,看下图:↓ 即打开CAN_ROCKCHIP和CAN_MCP251X项 save,生成

    2024年04月08日
    浏览(41)
  • 了解SPI总线CAN控制器 MCP2515配置 一文即可

    最近工作中遇到需要6路CAN通信的情况,单片机自带的4路已不满足实际需求,故采用了SPI总线的CAN控制器芯片MCP2515,通过SPI通信的CAN扩展芯片最高可实现 1Mbps 的遵循 CAN 2.0B 的协议通信,配置起来也比较繁琐,故写诞生了这篇文章。本篇中仅对基础功能进行测试,如有疑问可

    2024年02月06日
    浏览(50)
  • STM32外设芯片驱动学习记录 —— (二) PCA9555 IO扩展芯片驱动开发

    一、芯片介绍 二、Datasheet解读 1.硬件说明 2.寄存器说明 3.通信过程 三、驱动代码编写 1.软件I2C驱动 2. PCA9555芯片驱动函数 总结         PCA9555可设置16路输入或输出口,I2C接口,用于IO扩展,3个硬件地址引脚寻址,工作电压:VCC(2.3V 至 5.5V)。 1)框图   INT:中断输出 A0,

    2024年02月11日
    浏览(52)
  • STM32使用HAL库驱动TA6932数码管驱动芯片

    8段16位,支持共阴共阳LED数码管。 推挽配置即可。 1、使用TA6932_Init进行初始化 2、使用TA6932_Disp(uint8_t *_ucPtr,uint8_t _ucLenth)进行显示,_upPtr指针指向段码,_ucLenth为显示长度,不超过16位。

    2024年02月07日
    浏览(45)
  • STM32驱动CH9121网络串口透传芯片

    目录 一.介绍 二.配置 1.搜索配置  基础设置 端口设置 2.串口配置 ①CFG脚进入配置: ②串口协商进入: 三、使用CH9121 四、测试 关于CH9121的资料和配置软件 :提取码:3fxd 网络串口透传芯片CH9121,官网资料:网络串口透传芯片CH9121 这里摘取一些简介 CH9121 集成TCP/IP 协议栈,

    2024年02月10日
    浏览(42)
  • STM32外设芯片驱动学习记录 —— (一) BH1750光照传感器驱动开发

    一、芯片介绍 二、Datasheet解读 1.硬件说明 2.寄存器说明 3.通信过程 三、驱动代码编写 1.软件I2C驱动 2. BH1750芯片驱动函数 总结             BH1750是16位数字输出型,环境光强度传感器集成电路,使用I2C接口通信,工作电压:VCC(2.4~3.6V),I2C电平(1.65~VCC),用于各类消费类LCD屏

    2024年02月02日
    浏览(87)
  • 野火STM32F103驱动GT911触摸芯片

    芯片介绍 GT911 是专为 7”~8”设计的新一代 5 点电容触控 方案,拥有 26 个驱动通道和 14 个感 应通道,以满足更高的 touch 精度要求。 GT911 可同时识别 5 个触摸点位的 实时准确位置 , 移动轨迹 及 触摸面积。 并可根据主控需要,读取相应点数的触摸信息。 芯片原理图 管脚定

    2024年02月06日
    浏览(54)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包