文章图片
u8 CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
{
GPIO_InitTypeDefGPIO_InitStructure;
CAN_InitTypeDefCAN_InitStructure;
CAN_FilterInitTypeDefCAN_FilterInitStructure;
#if CAN_RX0_INT_ENABLE
NVIC_InitTypeDefNVIC_InitStructure;
#endif/* CAN Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO ,ENABLE);
//| RCC_APB2Periph_GPIOD
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);
/* Configure CAN pin: RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN pin: TX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//CAN单元设置
CAN_InitStructure.CAN_TTCM=DISABLE;
//非时间触发通信模式
CAN_InitStructure.CAN_ABOM=DISABLE;
//软件自动离线管理
CAN_InitStructure.CAN_AWUM=DISABLE;
//睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
CAN_InitStructure.CAN_NART=ENABLE;
//禁止报文自动传送
CAN_InitStructure.CAN_RFLM=DISABLE;
//报文不锁定,新的覆盖旧的
CAN_InitStructure.CAN_TXFP=DISABLE;
//优先级由报文标识符决定
CAN_InitStructure.CAN_Mode= mode;
//模式设置: mode:0,普通模式;
1,回环模式;
//设置波特率
CAN_InitStructure.CAN_SJW=tsjw;
//重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位CAN_SJW_1tqCAN_SJW_2tq CAN_SJW_3tq CAN_SJW_4tq
CAN_InitStructure.CAN_BS1=tbs1;
//Tbs1=tbs1+1个时间单位CAN_BS1_1tq ~CAN_BS1_16tq
CAN_InitStructure.CAN_BS2=tbs2;
//Tbs2=tbs2+1个时间单位CAN_BS2_1tq ~ CAN_BS2_8tq
CAN_InitStructure.CAN_Prescaler=brp;
//分频系数(Fdiv)为brp+1
CAN_Init(CAN1, &CAN_InitStructure);
//初始化CAN1 CAN时钟是36M CAN_FilterInitStructure.CAN_FilterNumber=0;
//过滤器0
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
//屏蔽位模式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
//32位宽
CAN_FilterInitStructure.CAN_FilterIdHigh=0x01 <<5;
//32位ID
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x1f <<5;
//32位MASK
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;
//过滤器0关联到FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
//激活过滤器0 CAN_FilterInit(&CAN_FilterInitStructure);
//滤波器初始化
#if CAN_RX0_INT_ENABLE
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
//FIFO0消息挂号中断允许. NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
// 主优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
// 次优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
return 0;
}
【stm32f107的can1的1m波特率设置】
推荐阅读
- stm32|基于STM32和freeRTOS智能门锁设计方案
- 日常分享|共享充电宝方案原理,具体部件组成以及主控MUC参数
- #|ARM裸机开发(汇编LED灯实验(I.MX6UL芯片))
- STM32|STM32的四种IO输出模式
- STM32 远程升级(ISP / IAP)
- stm32|stm32f103can总线过滤器配置
- STM32CubeMX配置SDIO模式(非DMA方式)
- STM32|如何建一个STM32F030标准库工程模板
- STM32 时钟RCC相关配置参考stm32f10x_rcc.h
- STM32 NVIC