1. stm32 CAN通信标准库函数
//CAN通信初始化函数
uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct);
//筛选器初始化函数
void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct);
//发送信息函数
uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage);
//发送信息状态函数
uint8_t CAN_TransmitStatus(CAN_TypeDef* CANx, uint8_t TransmitMailbox);
//接收信息函数
void CAN_Receive(CAN_TypeDef* CANx, uint8_t FIFONumber, CanRxMsg* RxMessage);
//CAN通信中断使能函数
void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState);
//状态有关的函数
FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG);
void CAN_ClearFlag(CAN_TypeDef* CANx, uint32_t CAN_FLAG);
ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT);
void CAN_ClearITPendingBit(CAN_TypeDef* CANx, uint32_t CAN_IT);
2. 利用标准库进行初始化
/*
CAN_TX:PB_13
CAN_RX:PB_12
*/
void CAN_Config(void)
{
//定义初始化所需结构体变量
CAN_InitTypeDef CAN_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
//开启所用的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
//引脚复用
GPIO_PinAFConfig(GPIOB,GPIO_Pin_13,GPIO_AF_CAN2);
GPIO_PinAFConfig(GPIOB,GPIO_Pin_12,GPIO_AF_CAN2);
//GPIO初始化
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;//复用模式
GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;//推挽输出
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_13;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;//默认上拉
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;//输出速率
GPIO_Init(GPIOB,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
//CAN初始化
CAN_InitStruct.CAN_ABOM=ENABLE;//是否使能ABOM自动离线管理功能
CAN_InitStruct.CAN_AWUM=ENABLE;//是否使能AWUM自动唤醒功能
CAN_InitStruct.CAN_NART=ENABLE;//是否使能NART自动重传功能
CAN_InitStruct.CAN_RFLM=DISABLE;//是否使能RFLM锁定功能
CAN_InitStruct.CAN_TTCM=DISABLE;//是否使能TTCM时间触发功能
CAN_InitStruct.CAN_TXFP=DISABLE;//配置TXFP报文优先级的判定方法
CAN_InitStruct.CAN_Mode=CAN_Mode_LoopBack;//回环模式
CAN_InitStruct.CAN_Prescaler=6;//配置CAN外设的时钟分频,可设置1-1024
CAN_InitStruct.CAN_SJW=CAN_SJW_2tq;//配置SJW极限值
CAN_InitStruct.CAN_BS1=CAN_BS1_4tq;//配置BS1段长度
CAN_InitStruct.CAN_BS2=CAN_BS2_2tq;//配置BS2段长度
CAN_Init(CAN2,&CAN_InitStruct);
//开启中断使能
CAN_ITConfig(CAN2,CAN_IT_FMP0,ENABLE);
//配置中断向量
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStruct.NVIC_IRQChannel=CAN2_RX0_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitStruct);
}
3. 筛选器初始化
void CAN_Filter(void)
{
CAN_FilterInitTypeDef CAN_FilterInitStruct;
CAN_FilterInitStruct.CAN_FilterNumber=14;//筛选器编号
CAN_FilterInitStruct.CAN_FilterScale=CAN_FilterScale_32bit;//设置筛选器的尺度
CAN_FilterInitStruct.CAN_FilterMode=CAN_FilterMode_IdMask;//掩码模式
CAN_FilterInitStruct.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//设置经过筛选后数据存储到那个接收器
CAN_FilterInitStruct.CAN_FilterActivation=ENABLE;//是否使能筛选器
CAN_FilterInitStruct.CAN_FilterIdHigh= (((uint32_t)0x1314<<3|CAN_Id_Extended|CAN_RTR_Data)&0xFFFF0000);//CAN_FxR1寄存器的高16位
CAN_FilterInitStruct.CAN_FilterIdLow=(((uint32_t)0x1314<<3|CAN_Id_Extended|CAN_RTR_Data)&0x0000FFFF);//CAN_FxR1寄存器的低16位
CAN_FilterInitStruct.CAN_FilterMaskIdHigh=0xFFFF;CAN_FxR2寄存器的高16位
CAN_FilterInitStruct.CAN_FilterMaskIdLow=0xFFFF;CAN_FxR2寄存器的低16位
CAN_FilterInit(&CAN_FilterInitStruct);
}
4. 发送数据
uint8_t mail_box;
CanTxMsg TxMessage;
TxMessage.StdId=0;//标准格式为0 ,使用扩展格式
TxMessage.ExtId=0x1314;//使用扩展格式
TxMessage.IDE=CAN_Id_Extended;//配置使用标准格式还是扩展格式
TxMessage.RTR=CAN_RTR_Data;//配置是数据帧还是遥控帧
TxMessage.DLC=8;//发送的数据长度
for(i=0;i<8;i++)
{
TxMessage.Data[i]=data[i];//存放数据
}
mail_box = CAN_Transmit(CAN2,&TxMessage);
5. 接收数据
//在终端中进行接收
void CAN2_RX0_IRQHandler(void)
{
if(CAN_GetITStatus(CAN2,CAN_IT_FMP0)==SET)
{
CAN_Receive(CAN2, CAN_FIFO0,&RxMessage);
printf("RxMessage.StdId:%x\r\n" ,RxMessage.StdId);
printf("RxMessage.ExtId:%x\r\n" ,RxMessage.ExtId);
printf("RxMessage.IDE:%x\r\n" ,RxMessage.IDE);
printf("RxMessage.RTR:%x\r\n" ,RxMessage.RTR);
printf("RxMessage.DLC:%x\r\n" ,RxMessage.DLC);
for(i=0;i<RxMessage.DLC;i++)
{
printf("RxMessage.Data[i]:%x\r\n",RxMessage.Data[i]);
}
}
}
标签:NVIC,void,通信,STM32,RxMessage,初学,InitStruct,GPIO,CANx
From: https://www.cnblogs.com/lyk-23/p/18091029