One, CAN bus communication initialization configuration
1. CAN bus communication initialization configuration process
- CAN bus GPIO and clock configuration
- CAN bus RX0 interrupt priority configuration
- CAN bus initialization configuration
2. CAN bus GPIO and clock configuration
CAN pin configuration:
CAN clock configuration: the
specific code is as follows:
/*CAN GPIO 和时钟配置 */
void CAN_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 复用前先初始化GPIOB端口时钟,白的查了两天电路
/* 复用功能和GPIOB端口时钟使能*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE);
/* CAN1 模块时钟使能 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
/* Configure CAN pin: RX */ // PB8
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN pin: TX */ // PB9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
//#define GPIO_Remap_CAN GPIO_Remap1_CAN1
GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE);
}
3. CAN bus RX0 interrupt priority configuration
The specific code is as follows:
/*CAN RX0 中断优先级配置 */
void CAN_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* enabling interrupt */
NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN1_RX0_IRQn;;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
4. CAN bus initialization configuration
The specific code is as follows:
void CAN_INIT(void)
{
CAN_GPIO_Config(); //CAN管脚初始化
CAN_NVIC_Configuration(); //CAN中断初始化
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN register init */
CAN_DeInit(CAN1); //将外设CAN的全部寄存器重设为缺省值
CAN_StructInit(&CAN_InitStructure);//把CAN_InitStruct中的每一个参数按缺省值填入
/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE;//没有使能时间触发模式
CAN_InitStructure.CAN_ABOM=DISABLE;//没有使能自动离线管理
CAN_InitStructure.CAN_AWUM=DISABLE;//没有使能自动唤醒模式
CAN_InitStructure.CAN_NART=DISABLE;//没有使能非自动重传模式
CAN_InitStructure.CAN_RFLM=DISABLE;//没有使能接收FIFO锁定模式
CAN_InitStructure.CAN_TXFP=DISABLE;//没有使能发送FIFO优先级
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal; //CAN设置为正常模式
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //重新同步跳跃宽度1个时间单位
CAN_InitStructure.CAN_BS1=CAN_BS1_3tq; //时间段1为3个时间单位
CAN_InitStructure.CAN_BS2=CAN_BS2_2tq; //时间段2为2个时间单位
CAN_InitStructure.CAN_Prescaler=24; //时间单位长度为24 250k
CAN_Init(CAN1,&CAN_InitStructure);
//波特率为:36M/24(1+3+2)=0.25 即250K
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=1;//指定过滤器为1
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//指定过滤器为标识符屏蔽位模式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;//过滤器位宽为32位
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;// 过滤器标识符的高16位值
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;// 过滤器标识符的低16位值
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//过滤器屏蔽标识符的高16位值
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;// 过滤器屏蔽标识符的低16位值
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;// 设定了指向过滤器的FIFO为0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;// 使能过滤器
CAN_FilterInit(&CAN_FilterInitStructure);// 按上面的参数初始化过滤器
/* CAN FIFO0 message pending interrupt enable */
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE); //使能FIFO0消息挂号中断
}
Two, CAN bus communication-baud rate calculation
1. CAN bus time characteristics
- Synchronization segment (SYNC_SEG) : It is usually expected that the bit change occurs in this time period. Its value is fixed at 1 time unit (1 x tCAN).
- Time period 1 (BS1) : Define the location of the sampling point. It includes PROP_SEG and PHASE_SEG1 in the CAN standard. Its value can be programmed from 1 to 16 time units, but it can also be automatically extended to compensate for the positive shift in phase caused by the frequency difference of different nodes in the network.
- Time period 2 (BS2) : Define the location of the sending point. It represents PHASE_SEG2 in the CAN standard. Its value can be programmed from 1 to 8 time units, but it can also be automatically shortened to compensate for negative phase shifts.
2. Baud rate calculation formula
CAN baud rate = APB bus frequency/BRP divider/(1+tBS1+tBS2)
3. Registers related to CAN baud rate