stm32F105的can2问题

最近在做can方面的要用到can2,调试了好久没有调通,一直是能发送数据不能正常接收,检查了滤波配置开始都没有发现问题,后面仔细看stm32的使用手册,最后发现在单独使用can2的时候必须开启can1的时钟,否则收不到数据,且过滤器也要设置从14开始,在stm32参考手册有这样一段话

CAN
CAN1:是主bxCAN,它负责管理在从bxCAN512字节的SRAM存储器之间的通信
CAN2:是从bxCAN,它不能直接访问SRAM存储器
● 这2bxCAN模块共享512字节的SRAM存储器(见图195)

可见必须要开启can1的时钟这是依据,下面贴一下配置的源码,方便初学者参考,有什么不对的请指正,用的是stm32f105的芯片,3.5的库函数

void CAN2_Config(void)
{
      GPIO_InitTypeDef        GPIO_InitStructure;

     CAN_InitTypeDef CAN_InitStructure;

     CAN_FilterInitTypeDef  CAN_FilterInitStructure;

//can时钟配置
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//can管脚复用
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
        GPIO_PinRemapConfig(GPIO_Remap_CAN2,ENABLE);
//引脚配置
       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
       GPIO_Init(PORTX(CAN_RX), &GPIO_InitStructure);

      GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_Init(GPIOB, &GPIO_InitStructure);   

//波特率配置

CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //1-4
        CAN_InitStructure.CAN_BS1=CAN_BS1_9tq; //1-16
        CAN_InitStructure.CAN_BS2=CAN_BS2_2tq; //1-8
        CAN_InitStructure.CAN_Prescaler=6;          //500k
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=ENABLE;
CAN_InitStructure.CAN_AWUM=ENABLE;
CAN_InitStructure.CAN_NART=DISABLE;
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
CAN_Init(CAN2,&CAN_InitStructure);

//过滤器配置
CAN_FilterInitStructure.CAN_FilterNumber=14;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_16bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=ID;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xFFFC;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=Mask;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}

你可能感兴趣的:(STM32,CAN,CAN,STM32F105,CAN2,STM32)