第十章作业

对于can的驱动函数文件加注释。在can(加注释).c中标了“//2024.6”的语句加以理解并写出注释。放入博客中。 实验完成后,将结果截图、连线照片、实验原理图和CAN原理介绍,放入博客中。

一、代码

main.c:

#define GLOBLE_VAR
#include "includes.h"      //包含总头文件
int main(void)
{
	vuint32_t mMainLoopCount;  //主循环次数变量
	uint8_t  mFlag;           //灯的状态标志
	uint32_t mLightCount;     //灯的状态切换次数
	uint32_t localMsgID;
	uint32_t txMsgID;
	uint32_t BitRate;


	//(1.2)【不变】关总中断
	DISABLE_INTERRUPTS;

	//(1.3)给主函数使用的局部变量赋初值
    mMainLoopCount=0;    //主循环次数变量
	mFlag='A';           //灯的状态标志
	mLightCount=0;       //灯的闪烁次数
	localMsgID = 0x0AU;
	txMsgID = 0x0BU;
	BitRate = 36;

	//(1.4)给全局变量赋初值

	//(1.5)用户外设模块初始化
	gpio_init(LIGHT_RED,GPIO_OUTPUT,LIGHT_ON);	//初始化蓝灯
    emuart_init(UART_User,115200);
    uart_init(UART_3,115200);
    //【***CAN模块初始化***】
    can_init(CAN_1,localMsgID,BitRate);

    //(1.6)使能模块中断
    uart_enable_re_int(UART_User);
    uart_enable_re_int(UART_3);
    //【***使能CAN模块中断***】
    can_enable_recv_int(CAN_1);
    //(1.7)【不变】开总中断
	ENABLE_INTERRUPTS;
	for(;;)   //for(;;)(开头)
	{
		//(2.1)主循环次数变量+1
        mMainLoopCount++;
        //(2.2)未达到主循环次数设定值,继续循环
		if (mMainLoopCount<=12889000)  continue;
		//(2.3)达到主循环次数设定值,执行下列语句,进行灯的亮暗处理
		//(2.3.1)清除循环次数变量
		mMainLoopCount=0;
		//(2.3.2)如灯状态标志mFlag为'L',灯的闪烁次数+1并显示,改变灯状态及标志
		if (mFlag=='L')                    //判断灯的状态标志
		{
			mLightCount++;
			printf("灯的闪烁次数 mLightCount = %d\n",mLightCount);
			mFlag='A';                       //灯的状态标志
			gpio_set(LIGHT_RED,LIGHT_ON);  //灯“亮”
			printf(" LIGHT_RED:ON--\n");   //串口输出灯的状态
			//【***CAN模块发送一帧数据***】
			if(can_send(CAN_1, txMsgID, 8, (uint8_t*)"qiuzheng") != 0) printf("failed\r\n");
		}
		//(2.3.3)如灯状态标志mFlag为'A',改变灯状态及标志
		else
		{
			mFlag='L';                       //灯的状态标志
			gpio_set(LIGHT_RED,LIGHT_OFF); //灯“暗”
			printf(" LIGHT_RED:OFF--\n");  //串口输出灯的状态
		}
	}  //for(;;)结尾
	
}   //main函数(结尾)

can.c注释:

//======================================================================
//文件名称:can.c
//功能概要:uart底层驱动构件源文件
//版权所有:苏州大学嵌入式系统与物联网研究所(sumcu.suda.edu.cn)
//更新记录:2021-02-03 V1.0  JJL
//======================================================================
#include "can.h"

CAN_TypeDef *CAN_ARR[] = {(CAN_TypeDef*)CAN1_BASE};
IRQn_Type table_irq_can[2] = {CAN1_RX0_IRQn, CAN1_RX1_IRQn};

uint8_t can_send_once(uint8_t canNo, uint32_t DestID, uint16_t len ,uint8_t* buff);
uint8_t CAN_HWInit(uint8_t CANChannel);
uint8_t CAN_SWInit_Entry(uint8_t canNo);
void CAN_SWInit_CTLMode(uint8_t canNo);
void CAN_SWInit_BT(uint8_t canNo, uint32_t CANMode, uint32_t Prescaler);
uint8_t CAN_SWInit_Quit(uint8_t canNo);
uint8_t CANFilterConfig(uint8_t canNo, uint32_t canID, uint32_t FilterBank, uint32_t Can_Rx_FifoNo, uint8_t IsActivate, uint32_t FilterMode, uint32_t FilterScale);

//=====================================================================
//函数名称:can_init
//函数返回:无
//参数说明:canNo:模块号,本芯片只有CAN_1
//		    canID:自身CAN节点的唯一标识,例如按照CANopen协议给出
//          BitRate:位速率
//功能概要:初始化CAN模块
//=====================================================================
void can_init(uint8_t canNo, uint32_t canID, uint32_t BitRate)
{
	//声明Init函数使用的局部变量
	uint32_t CANMode;
	uint32_t CANFilterBank;
	uint32_t CANFiltermode;
	uint32_t CAN_Filterscale;

	//给Init函数使用的局部变量赋初值
	CANMode = CAN_MODE_NORMAL;                //2024.6 CAN总线设为正常模式
	CANFilterBank = CANFilterBank0;
	CANFiltermode = CAN_FILTERMODE_IDMASK;
	CAN_Filterscale = CAN_FILTERSCALE_32BIT;

	//(1)CAN总线硬件初始化
	CAN_HWInit(CAN_CHANNEL);
	//(2)CAN总线进入软件初始化模式
	CAN_SWInit_Entry(canNo);
	//(3)CAN总线模式设置
	CAN_SWInit_CTLMode(canNo);
	//(4)CAN总线位时序配置
	CAN_SWInit_BT(canNo,CANMode,BitRate);
	//(5)CAN总线过滤器初始化
    CANFilterConfig(canNo, canID, CANFilterBank, CAN_RX_FIFO0, 1, CANFiltermode, CAN_Filterscale);
    //(6)CAN总线退出软件初始化模式,进入正常模式
    CAN_SWInit_Quit(canNo);
}

//=====================================================================
//函数名称:can_send
//函数返回:0=正常,1=错误
//参数说明:canNo:模块号,本芯片只有CAN_1
//          DestID:目标CAN节点的唯一标识,例如按照CANopen协议给出
//          len:待发送数据的字节数
//          buff:待发送数据发送缓冲区首地址
//功能概要:CAN模块发送数据
//=====================================================================
uint8_t can_send(uint8_t canNo, uint32_t DestID, uint16_t len ,uint8_t* buff)
{
	if(DestID > 0x1FFFFFFFU) return 1;
	uint8_t send_length;
	for(int i = len; i > 0; i = i-8)
	{
		send_length = (i>8)?8:i;
		if(can_send_once(canNo,DestID,send_length,buff+len-i) == 1)   //2024.6 can模块尝试发送一次数据
		{
			return 1;
		}
	}
	return 0;
}

//=====================================================================
//函数名称:can_recv
//函数返回:接收到的字节数
//参数说明:canNo:模块号,本芯片只有CAN_1
//          buff:接收到的数据存放的内存区首地址
//功能概要:在CAN模块接收中断中调用本函数接收已经到达的数据
//=====================================================================
uint8_t can_recv(uint8_t canNo, uint8_t *buff)
{
	uint8_t len;
	uint32_t RxFifo = CAN_RX_FIFO0;
	//(1)判断哪个邮箱收到了报文信息
	if(RxFifo == CAN_RX_FIFO0)
	{
		if ((CAN_ARR[canNo-1]->RF0R & CAN_RF0R_FMP0) == 0U)   //2024.6can接收FIFO寄存器判断can接收状态
		{
			return 1;
		}
	}
	else
	{
		if ((CAN_ARR[canNo-1]->RF1R & CAN_RF1R_FMP1) == 0U)
		{
			return 1;
		}
	}
	//(2)获取数据长度
    len = (CAN_RDT0R_DLC & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_DLC_Pos;  //2024.6从CAN接收FIFO邮箱中获取数据长度 
    //(3)获取数据帧中的数据
    buff[0] = (uint8_t)((CAN_RDL0R_DATA0 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA0_Pos);
    buff[1] = (uint8_t)((CAN_RDL0R_DATA1 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA1_Pos);
    buff[2] = (uint8_t)((CAN_RDL0R_DATA2 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA2_Pos);
    buff[3] = (uint8_t)((CAN_RDL0R_DATA3 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA3_Pos);
    buff[4] = (uint8_t)((CAN_RDH0R_DATA4 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA4_Pos);
    buff[5] = (uint8_t)((CAN_RDH0R_DATA5 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA5_Pos);
    buff[6] = (uint8_t)((CAN_RDH0R_DATA6 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA6_Pos);
    buff[7] = (uint8_t)((CAN_RDH0R_DATA7 & CAN_ARR[canNo-1]->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA7_Pos);
    //(4)清除标志位,等待接收下一帧数据
    if (RxFifo == CAN_RX_FIFO0)
    {
      SET_BIT(CAN_ARR[canNo-1]->RF0R, CAN_RF0R_RFOM0);  //2024.6 can接收FIFO寄存器清除标志位
    }
    else
    {
      SET_BIT(CAN_ARR[canNo-1]->RF1R, CAN_RF1R_RFOM1);
    }
	return len;
}

//=====================================================================
//函数名称:CAN_enable_re_int
//函数返回:无
//参数说明:canNo:模块基地址号,Can_Rx_FifoNo:中断使用的邮箱号
//功能概要:CAN接收中断开启
//=====================================================================
void can_enable_recv_int(uint8_t canNo)
{
	uint8_t Can_Rx_FifoNo;
	Can_Rx_FifoNo = CAN_RX_FIFO0;
	if(Can_Rx_FifoNo == CAN_RX_FIFO0)
		SET_BIT(CAN_ARR[canNo-1]->IER, CAN_IER_FMPIE0);    //2024.6		fifo0接收中断使能
	else
		SET_BIT(CAN_ARR[canNo-1]->IER,CAN_IER_FMPIE1);
	NVIC_EnableIRQ(table_irq_can[Can_Rx_FifoNo]);     //2024.6 	can接收中断使能	 
}

//=====================================================================
//函数名称:can_disable_recv_int
//函数返回:无
//参数说明:canNo:模块号,本芯片只有CAN_1
//功能概要:关闭CAN接收中断
//=====================================================================
void can_disable_recv_int  (uint8_t canNo)
{
	uint8_t Can_Rx_FifoNo;
	Can_Rx_FifoNo = CAN_RX_FIFO0;
	if(Can_Rx_FifoNo == CAN_RX_FIFO0)
		CLEAR_BIT(CAN_ARR[canNo-1]->IER, CAN_IER_FMPIE0);
	else
		CLEAR_BIT(CAN_ARR[canNo-1]->IER,CAN_IER_FMPIE1);
	NVIC_DisableIRQ(table_irq_can[Can_Rx_FifoNo]);
}

//=====================================================================
//函数名称:can_send_once
//函数返回:0=正常,1=错误
//参数说明:canNo:模块号,本芯片只有CAN_1
//          DestID:目标CAN节点的唯一标识,例如按照CANopen协议给出
//          len:待发送数据的字节数
//          buff:待发送数据发送缓冲区首地址
//功能概要:CAN模块发送一次数据
//=====================================================================
uint8_t can_send_once(uint8_t canNo, uint32_t DestID, uint16_t len ,uint8_t* buff)
{
	//(1)定义Can发送函数所需要用到的变量
	uint32_t transmit_mailbox;
	uint32_t register_tsr;
	uint32_t rtr;
	rtr = CAN_RTR_DATA;
	register_tsr = READ_REG(CAN_ARR[canNo-1]->TSR); 	//CAN模块状态寄存器

	//(2)判断3个邮箱中是否有空闲邮箱,若有,选取其中一个进行发送,选取顺序为1,2,3
    if (((register_tsr & CAN_TSR_TME0) != 0U) ||    
        ((register_tsr & CAN_TSR_TME1) != 0U) ||
        ((register_tsr & CAN_TSR_TME2) != 0U))
    {
    	transmit_mailbox = (register_tsr & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos;     //2024.6	从CAN控制器的传输状态寄存器(TSR)中提取出传输的邮箱数量
    	if(transmit_mailbox > 2U)
    	{
    		return 1;
    	}

    	//(2.1)判断并设置发送帧为标准帧还是扩展帧
    	if(DestID <= 0x7FFU)    //2024.6 DestID范围在	0x000~0x7FF
    	{
    		CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TIR = ((DestID << CAN_TI0R_STID_Pos)|CAN_ID_STD|rtr);  //2024.6设置发送帧为标准帧
    	}
    	else
    	{
    		CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TIR = ((DestID << CAN_TI0R_EXID_Pos)|CAN_ID_EXT|rtr);  //2024.6设置发送帧为扩展帧
    	}
    	//(2.2)设置发送帧的数据长度
    	CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TDTR = len;
        //SET_BIT(CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TDTR, CAN_TDT0R_TGT);
        //(2.3)设置发送帧的数据
        WRITE_REG(CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TDHR,   //2024.6 将数据写入TDHR高位寄存器 
                  ((uint32_t)buff[7] << CAN_TDH0R_DATA7_Pos) |
                  ((uint32_t)buff[6] << CAN_TDH0R_DATA6_Pos) |
                  ((uint32_t)buff[5] << CAN_TDH0R_DATA5_Pos) |
                  ((uint32_t)buff[4] << CAN_TDH0R_DATA4_Pos));
        WRITE_REG(CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TDLR,
                  ((uint32_t)buff[3] << CAN_TDL0R_DATA3_Pos) |
                  ((uint32_t)buff[2] << CAN_TDL0R_DATA2_Pos) |
                  ((uint32_t)buff[1] << CAN_TDL0R_DATA1_Pos) |
                  ((uint32_t)buff[0] << CAN_TDL0R_DATA0_Pos));
        //(2.4)发送Can数据报
        SET_BIT(CAN_ARR[canNo-1]->sTxMailBox[transmit_mailbox].TIR, CAN_TI0R_TXRQ);   //2024.6将数据写入TDHR低位寄存器 
        return 0;
    }
    else
    {
    	return 1;
    }
}

//=====================================================================
//函数名称:CAN_HWInit
//函数返回:0=正常,1=错误
//参数说明:CANChannel:硬件引脚组号,共有3组,分别为PTA11&PTA12(CAN_CHANNEL0),PTB8&PTB9(CAN_CHANNEL1),PTD0&PTD1(2)
//功能概要:CAN模块引脚初始化
//=====================================================================
uint8_t CAN_HWInit(uint8_t CANChannel)
{
	if(CANChannel < 0 || CANChannel > 2)
	{
		return 1;
	}
	if(CANChannel == 0)
	{
		RCC->APB1ENR1 |= RCC_APB1ENR1_CAN1EN;   //2024.6 使能CAN1时钟
		RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
		GPIOA->MODER &= ~(GPIO_MODER_MODE11|GPIO_MODER_MODE12);
		GPIOA->MODER |= (GPIO_MODER_MODE11_1|GPIO_MODER_MODE12_1);  //2024.6   Gpio11和12引脚为复用模式
		GPIOA->AFR[1] &= ~(GPIO_AFRH_AFSEL11|GPIO_AFRH_AFSEL12);
		GPIOA->AFR[1] |= (GPIO_AFRH_AFSEL11_0|GPIO_AFRH_AFSEL11_3)|(GPIO_AFRH_AFSEL12_0|GPIO_AFRH_AFSEL12_3);   //2024.6  Gpio11和12引脚复用为CAN1
	}
	else if(CANChannel == 1)
	{
		RCC->APB1ENR1 |= RCC_APB1ENR1_CAN1EN;
		RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
		GPIOB->MODER &= ~(GPIO_MODER_MODE8|GPIO_MODER_MODE9);
		GPIOB->MODER |= (GPIO_MODER_MODE8_1|GPIO_MODER_MODE9_1);
		GPIOB->AFR[1] &= ~(GPIO_AFRH_AFSEL8|GPIO_AFRH_AFSEL9);
		GPIOB->AFR[1] |= ((GPIO_AFRH_AFSEL8_0|GPIO_AFRH_AFSEL8_3)|
						  (GPIO_AFRH_AFSEL9_0|GPIO_AFRH_AFSEL9_3));
	}
	else
	{
		RCC->APB1ENR1 |= RCC_APB1ENR1_CAN1EN;
		RCC->AHB2ENR |= RCC_AHB2ENR_GPIODEN;
		GPIOD->MODER &= ~(GPIO_MODER_MODE0|GPIO_MODER_MODE1);
		GPIOD->MODER |= (GPIO_MODER_MODE0_1|GPIO_MODER_MODE1_1);
		GPIOD->AFR[0] &= ~(GPIO_AFRL_AFSEL0|GPIO_AFRL_AFSEL1);
		GPIOD->AFR[0] |= ((GPIO_AFRL_AFSEL0_0 | GPIO_AFRL_AFSEL0_3)|
						  (GPIO_AFRL_AFSEL1_0 | GPIO_AFRL_AFSEL1_3));
	}
	return 0;
}

//=====================================================================
//函数名称:CAN_SWInit_Entry
//函数返回:0=正常,1=错误
//参数说明:canNo:模块基地址号,本芯片只有CAN_1,
//功能概要:进入初始化模式
//=====================================================================
uint8_t CAN_SWInit_Entry(uint8_t canNo)
{
	int i;
	CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_SLEEP);    //2024.6 清除CAN主控制寄存器的睡眠模式 
	i = 0;
	while ((CAN_ARR[canNo-1]->MSR & CAN_MSR_SLAK) != 0U)   //2024.6 等待CAN主控制寄存器退出睡眠状态
	{
		if(i++ > 0x30000)
		{
			return 1;
		}
	}

	SET_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_INRQ);    //2024.6 进入初始化模式  
	i = 0;
	while ((CAN_ARR[canNo-1]->MSR & CAN_MSR_INAK) == 0U)
	{
		if(i++ > 0x30000)
		{
			return 1;
		}
	}
	return 0;
}

//=====================================================================
//函数名称:CAN_SWInit_CTLMode
//函数返回:无
//参数说明:canNo:模块基地址号,本芯片只有CAN_1,
//功能概要:CAN总线模式设置
//=====================================================================
void CAN_SWInit_CTLMode(uint8_t canNo)
{
	CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_TTCM);   //2024.6 关闭时间触发模式
	CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_ABOM);   //2024.6  关闭自动离线管理
	CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_AWUM);   //2024.6  关闭自动唤醒模式
	SET_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_NART);     //2024.6设置报文自动重传功能
	CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_RFLM);    //2024.6关闭FIFO锁定模式
	CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_TXFP);    //2024.6关闭报文发送优先级的判定方法
}

//函数名称:CAN_SWInit_CTLMode
//函数返回:无
//参数说明:canNo:模块基地址号,本芯片只有CAN_1,
//			CANMode:CAN总线工作模式,分别为正常模式(CAN_MODE_NORMAL)、回环模式(CAN_MODE_LOOPBACK)、
//										    静默模式(CAN_MODE_SILENT)以及回环与静默组合模式(CAN_MODE_SILENT_LOOPBACK)
//功能概要:CAN总线位时序配置
void CAN_SWInit_BT(uint8_t canNo, uint32_t CANMode, uint32_t Prescaler)
{
	CAN_ARR[canNo-1]->BTR |= ((uint32_t)(Prescaler-1)|CAN_SJW_1TQ|CAN_BTR_TS1_1|CAN_BTR_TS1_0|CAN_BTR_TS2_2|CANMode);    //2024.6CAN总线位时序和模式配置
}

//=====================================================================
//函数名称:CAN_SWInit_Quit
//函数返回:0=正常,1=错误
//参数说明:canNo:模块基地址号
//功能概要:退出初始化模式,进入正常模式
//=====================================================================
uint8_t CAN_SWInit_Quit(uint8_t canNo)
{
	int i;
	CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_INRQ);    //2024.6退出初始化模式
	i = 0;
    while ((CAN_ARR[canNo-1]->MSR & CAN_MSR_INAK) != 0U)    //2024.6 等待CAN主控制寄存器退出初始化模式
    {
      if (i++ > 0x30000)
      {
        return 1;
      }
    }
    return 0;
}

//=====================================================================
//函数名称: CANFilterConfig
//函数返回:0=正常,1=错误
//参数说明: canNo:模块基地址号,
//		    canID:自身CAN节点的唯一标识,例如按照CANopen协议给出
//		    Can_Rx_FifoNo:中断使用的邮箱号,
//			IsActivate:是否激活过滤器
//			CANFilterBank:CAN总线过滤器组选择,共有28个,(CANFilterBank0~CANFilterBank27)
//			CANFiltermode:CAN总线过滤器模式,分别为掩码模式(CAN_FILTERMODE_IDMASK)和列表模式(CAN_FILTERMODE_IDLIST)
//			CAN_Filterscale:CAN总线过滤器位数,分别为32位(CAN_FILTERSCALE_32BIT)和16位(CAN_FILTERSCALE_16BIT)
//功能概要:CAN接收中断开启
//=====================================================================
uint8_t CANFilterConfig(uint8_t canNo, uint32_t CanID, uint32_t FilterBank, uint32_t Can_Rx_FifoNo, uint8_t IsActivate, uint32_t FilterMode, uint32_t FilterScale)
{
	uint32_t FilterIdHigh, FilterIdLow, FilterMaskIdHigh, FilterMaskIdLow, filternbrbitpos;
	if(CanID <= 0x7FFU) CanID = CanID << CAN_TI0R_STID_Pos;    //2024.6 CANID左移16位

	FilterIdHigh = (CanID >> 16) & 0xFFFF;    //2024.6存储CanID高 16 位
	FilterIdLow = (CanID & 0xFFFF);    //2024.6存储CanID低 16 位
	FilterMaskIdHigh = 0xFFE0;    //2024.6设置过滤器掩码高16位
	FilterMaskIdLow = 0x0000;    //2024.6设置过滤器掩码低16位
	filternbrbitpos = (uint32_t)1 << (FilterBank & 0x1FU);    //2024.6选择CAN总线过滤器组

	//设置过滤器初始化模式 (FINIT=1),在此模式下可以进行过滤器初始化
	SET_BIT(CAN_ARR[canNo-1]->FMR, CAN_FMR_FINIT);    //2024.6can过滤器工作模式设为初始化模式
	CLEAR_BIT(CAN_ARR[canNo-1]->FA1R, filternbrbitpos);    //2024.6can过滤器激活寄存器清除总线过滤器组
	if (FilterScale == CAN_FILTERSCALE_16BIT)
	{
	  CLEAR_BIT(CAN_ARR[canNo-1]->FS1R, filternbrbitpos);
	  CAN_ARR[canNo-1]->sFilterRegister[FilterBank].FR1 =
		((0x0000FFFFU & (uint32_t)FilterMaskIdLow) << 16U) |
		(0x0000FFFFU & (uint32_t)FilterIdLow);
	  CAN_ARR[canNo-1]->sFilterRegister[FilterBank].FR2 =
		((0x0000FFFFU & (uint32_t)FilterMaskIdHigh) << 16U) |
		(0x0000FFFFU & (uint32_t)FilterIdHigh);
	}
	if (FilterScale == CAN_FILTERSCALE_32BIT)    //2024.6过滤器位数为 32 位
	{
	  SET_BIT(CAN_ARR[canNo-1]->FS1R, filternbrbitpos);    //2024.6配置can过滤器尺度的位
	  CAN_ARR[canNo-1]->sFilterRegister[FilterBank].FR1 =    //2024.6 设置can过滤器寄存器FR1
		((0x0000FFFFU & (uint32_t)FilterIdHigh) << 16U) |
		(0x0000FFFFU & (uint32_t)FilterIdLow);
	  CAN_ARR[canNo-1]->sFilterRegister[FilterBank].FR2 = 
		((0x0000FFFFU & (uint32_t)FilterMaskIdHigh) << 16U) |
		(0x0000FFFFU & (uint32_t)FilterMaskIdLow);
	}
	if (FilterMode == CAN_FILTERMODE_IDMASK)    //2024.6 配置过滤器模式为掩码模式
	{
	  CLEAR_BIT(CAN_ARR[canNo-1]->FM1R, filternbrbitpos);    //2024.6 清除过滤器模式寄存器位
	}
	else
	{
	  SET_BIT(CAN_ARR[canNo-1]->FM1R, filternbrbitpos);
	}
	if (Can_Rx_FifoNo == CAN_FILTER_FIFO0)     //2024.6 分配CAN过滤器FIFO邮箱
	{
	  CLEAR_BIT(CAN_ARR[canNo-1]->FFA1R, filternbrbitpos);   //2024.6清除CAN过滤器FIFO邮箱位
	}
	else
	{
	  SET_BIT(CAN_ARR[canNo-1]->FFA1R, filternbrbitpos);
	}
	if (IsActivate == 1)    //2024.6激活过滤器
	{
	  SET_BIT(CAN_ARR[canNo-1]->FA1R, filternbrbitpos);   //2024.6设置can过滤器激活寄存器位
	}
	//退出过滤器初始化模式 (FINIT=0)
	CLEAR_BIT(CAN_ARR[canNo-1]->FMR, CAN_FMR_FINIT);    //2024.6can过滤器工作模式退出初始化模式

	return 0;
}

二、图片

实验连线图

连线图:

结果:

CAN_nodeA:

CAN_nodeA:

三、CAN介绍

1. 什么是CAN通信?
CAN通信是一种多节点通信协议,最早由Bosch公司开发并在1986年首次推出。它被广泛应用于汽车电子控制系统、工业自动化领域以及其他需求多节点通信的应用场景中。

CAN通信的特点之一是支持多节点之间的高速数据传输,适用于需要高实时性和高可靠性的系统。CAN总线由两根线组成,分别是CAN_H(CAN High)和CAN_L(CAN Low)。CAN总线使用不同的电压电平来表示0和1,并通过差分信号传输来抗干扰。

2. CAN通信原理
CAN通信采用CSMA/CD(Carrier Sense Multiple Access with Collision Detection)的工作原理。简单来说,这意味着每个节点都可以在总线上发送消息,但在发送之前需要先监听总线上的通信情况。

当一个节点要发送消息时,首先会监听总线,如果没有其他节点正在发送消息,它就可以开始发送。如果同时有多个节点尝试发送消息,就会发生冲突。在CAN总线上使用的是非毁坏性冲突检测机制,冲突的节点会立即停止发送,并在发送完自己的消息后再次来检测冲突。

CAN通信中还使用了位定时传输方式,即总线上的每个位都有固定的时间段。发送节点将每个位的电平保持一段时间,接收节点则在相应的时间段内检测位的电平。这种位定时传输方式确保了数据的同步和准确性。

此外,CAN通信还通过帧的优先级来管理消息的传输。较低优先级的帧会在总线上等待较高优先级的帧发送完毕后再发送,确保重要消息的及时传输。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值