1、对于can的驱动函数文件加注释。
//======================================================================
//文件名称: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工作模式为正常模式,这是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_send_once函数尝试发送一次数据,如果返回1表示发送失败,函数立即返回1表示发送过程中出现错误
{
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.6检查CAN_RX_FIFO0的邮箱是否为空,如果为空,表示没有数据可读,函数返回1
{
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获取接收到的数据长度,通过读取RDTR寄存器中的DLC位来确定
//(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_RX_FIFO0的满标志位,准备接收下一帧数据
}
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设置CAN_IER寄存器的FMPIE0位,开启FIFO0的接收中断,当FIFO0接收到数据时将触发中断
else
SET_BIT(CAN_ARR[canNo-1]->IER,CAN_IER_FMPIE1);
NVIC_EnableIRQ(table_irq_can[Can_Rx_FifoNo]); // 2024.6启用与指定FIFO号对应的CAN中断,确保中断机制正确响应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);
//(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从TSR寄存器中提取当前可用的邮箱号,用于确定哪个邮箱可以用于发送数据
if(transmit_mailbox > 2U)
{
return 1;
}
//(2.1)判断并设置发送帧为标准帧还是扩展帧
if(DestID <= 0x7FFU) // 2024.6检查目标ID是否小于或等于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寄存器,设置发送数据的高4字节
((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请求发送数据,通过设置TXRQ位来启动数据发送过程
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的时钟,这是初始化CAN模块的第一步,确保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配置GPIOA的引脚11和12为复用模式,用于CAN1的通信
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配置GPIOA的引脚11和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清除MCR寄存器中的SLEEP位,从睡眠模式退出,确保CAN模块处于活动状态
i = 0;
while ((CAN_ARR[canNo-1]->MSR & CAN_MSR_SLAK) != 0U) // 2024.6等待SLAK位清零,确认CAN模块已成功从睡眠模式退出
{
if(i++ > 0x30000)
{
return 1;
}
}
SET_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_INRQ); // 2024.6设置MCR寄存器中的INRQ位,进入初始化模式,准备配置CAN模块
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清除MCR寄存器中的TTCM位,禁用时间触发通信模式
CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_ABOM); // 2024.6清除MCR寄存器中的ABOM位,禁用自动离线管理
CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_AWUM); // 2024.6清除MCR寄存器中的AWUM位,禁用自动唤醒模式
SET_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_NART); // 2024.6设置MCR寄存器中的NART位,启用不自动重发模式
CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_RFLM); // 2024.6清除MCR寄存器中的RFLM位,禁用接收FIFO锁定模式
CLEAR_BIT(CAN_ARR[canNo-1]->MCR, CAN_MCR_TXFP); // 2024.6清除MCR寄存器中的TXFP位,禁用发送FIFO优先级
}
//函数名称: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.6配置CAN位时序寄存器BTR,包括预分频器、同步跳转宽度、时间段1和时间段2,以及CAN工作模式
}
//=====================================================================
//函数名称: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清除MCR寄存器中的INRQ位,请求退出初始化模式,进入正常工作模式
i = 0;
while ((CAN_ARR[canNo-1]->MSR & CAN_MSR_INAK) != 0U) // 2024.6等待INAK位清零,确认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是标准标识符(11位),则左移到正确位置
FilterIdHigh = (CanID >> 16) & 0xFFFF; // 2024.6提取高16位的过滤器ID
FilterIdLow = (CanID & 0xFFFF); // 2024.6提取低16位的过滤器ID
FilterMaskIdHigh = 0xFFE0; // 2024.6设置高16位的过滤器掩码,0xFFE0表示一个默认的掩码模式
FilterMaskIdLow = 0x0000; // 2024.6设置低16位的过滤器掩码,0x0000表示一个默认的掩码模式
filternbrbitpos = (uint32_t)1 << (FilterBank & 0x1FU); // 2024.6计算过滤器组的位置位,用于配置具体的过滤器组
//设置过滤器初始化模式 (FINIT=1),在此模式下可以进行过滤器初始化
SET_BIT(CAN_ARR[canNo-1]->FMR, CAN_FMR_FINIT); // 2024.6设置FINIT位,进入过滤器初始化模式
CLEAR_BIT(CAN_ARR[canNo-1]->FA1R, filternbrbitpos); // 2024.6清除指定过滤器组的激活位,禁用过滤器
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设置过滤器为32位模式
CAN_ARR[canNo-1]->sFilterRegister[FilterBank].FR1 = // 2024.6配置第一个32位过滤器寄存器
((0x0000FFFFU & (uint32_t)FilterIdHigh) << 16U) |
(0x0000FFFFU & (uint32_t)FilterIdLow);
CAN_ARR[canNo-1]->sFilterRegister[FilterBank].FR2 = // 2024.6配置第二个32位过滤器寄存器
((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检查接收FIFO号是否为FIFO0
{
CLEAR_BIT(CAN_ARR[canNo-1]->FFA1R, filternbrbitpos); // 2024.6将过滤器关联到FIFO0
}
else
{
SET_BIT(CAN_ARR[canNo-1]->FFA1R, filternbrbitpos);
}
if (IsActivate == 1) // 2024.6检查是否激活过滤器
{
SET_BIT(CAN_ARR[canNo-1]->FA1R, filternbrbitpos); // 2024.6激活过滤器
}
//退出过滤器初始化模式 (FINIT=0)
CLEAR_BIT(CAN_ARR[canNo-1]->FMR, CAN_FMR_FINIT); // 2024.6清除FINIT位,退出过滤器初始化模式
return 0;
}
2、2个或以上同学相互连接,利用CAN通信,向对方发送带有本人姓名的信息。连线方式:按基本原理性电路(不带收发器芯片)连接,参考教材图10-1。
修改main.c:在can模块添加含有个人信息的一帧数据
#define GLOBLE_VAR
#include "includes.h" //包含总头文件
//----------------------------------------------------------------------
//声明使用到的内部函数
//main.c使用的内部函数声明处
//----------------------------------------------------------------------
//主函数,一般情况下可以认为程序从此开始运行(实际上有启动过程,参见书稿)
int main(void)
{
//(1)======启动部分(开头)==========================================
//(1.1)声明main函数使用的局部变量
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;
//(1)======启动部分(结尾)==========================================
//(2)======主循环部分(开头)========================================
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*)"I am CQL \r\n") != 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(;;)结尾
//(2)======主循环部分(结尾)========================================
} //main函数(结尾)
//======以下为主函数调用的子函数===========================================
//========================================================================
/*
知识要素:
(1)main.c是一个模板,该文件所有代码均不涉及具体的硬件和环境,通过调用构件
实现对硬件的干预。
(2)本文件中对宏GLOBLE_VAR进行了定义,所以在包含"includes.h"头文件时,会定
义全局变量,在其他文件中包含"includes.h"头文件时,
编译时会自动增加extern
*/
连接图如下:
更新串口结果:
连接对象的电脑中收到并打印本人名字缩写