CAN初始化部分,波特率和模式可以根据参数设定
#include "bsp_can.h"
/************************************************************
* CAN波特率计算公式:
* Baud = 32000000/BRP/(tSJW+tBS1+tBS2)
*
*
* 1M kbps
* tSJW = CAN_SJW_1tq
* tBS1 = CAN_BS1_3tq
* tBS2 = CAN_BS2_2tq
* BRP = 6
* 800 kbps
* tSJW = CAN_SJW_1tq
* tBS1 = CAN_BS1_5tq
* tBS2 = CAN_BS2_3tq
* BRP = 5
* 500 kbps
* tSJW = CAN_SJW_1tq
* tBS1 = CAN_BS1_8tq
* tBS2 = CAN_BS2_9tq
* BRP = 4
* 250 kbps
* tSJW = CAN_SJW_1tq
* tBS1 = CAN_BS1_3tq
* tBS2 = CAN_BS2_2tq
* BRP = 24
* 200 kbps
* tSJW = CAN_SJW_1tq
* tBS1 = CAN_BS1_3tq
* tBS2 = CAN_BS2_2tq
* BRP = 30
*************************************************************/
void CAN_Config(unsigned char mode,unsigned char tSJW,unsigned char tBS1,unsigned char tBS2,unsigned char BRP)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能 PORTB 时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //使能AFIO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); //使能 CAN1 时钟
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE); //CAN1部分映射到PB8和PB9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
//CAN 单元设置
CAN_InitStructure.CAN_TTCM=DISABLE; //非时间触发通信模式
CAN_InitStructure.CAN_ABOM=DISABLE; //软件自动离线管理
CAN_InitStructure.CAN_AWUM=DISABLE; //睡眠模式通过软件唤醒
CAN_InitStructure.CAN_NART=ENABLE; //禁止报文自动传送
CAN_InitStructure.CAN_RFLM=DISABLE; //报文不锁定,新的覆盖旧的
CAN_InitStructure.CAN_TXFP=DISABLE; //优先级由报文标识符决定
CAN_InitStructure.CAN_Mode= CAN_Mode_LoopBack; //模式设置: 0,普通模式;1,回环模式;
//设置波特率
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //重新同步跳跃宽度(Tsjw)
CAN_InitStructure.CAN_BS1=CAN_BS1_3tq; //时间段 1 占用时间单位
CAN_InitStructure.CAN_BS2=CAN_BS2_2tq ; // 时间段 2 占用时间单位
CAN_InitStructure.CAN_Prescaler=6; //分频系数(Fdiv)为 brp+1
CAN_Init(CAN1, &CAN_InitStructure); // 初始化 CAN1
//CAN过滤器设置
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=0x0000; //32 位 ID
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000; //32 位 MASK
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;// FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //激活过滤器 0
CAN_FilterInit(&CAN_FilterInitStructure); //滤波器初始
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE); //FIFO0 消息挂号中断允许.
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 主优先级为 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 次优先级为 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
|