打印

STM32 双CAN配置,呼叫用过STM32的,

[复制链接]
3878|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
mochou|  楼主 | 2012-4-10 15:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
各位大侠,你们好,我现在STM32F105的板子上用到了两路CAN,PD0,PD1 配置为CAN1,PB5,PB6配置为CAN2(REMAP了),但现在CAN1正常工作,CAN2就不工作,请问是什么问题,谢谢大家了,CAN2初始化函数如下
void SGL_CAN2_Init(void)
{
    NVIC_InitTypeDef       NVIC_InitStructure;
    CAN_InitTypeDef        CAN_InitStructure;
    CAN_FilterInitTypeDef  CAN_FilterInitStructure;
    GPIO_InitTypeDef       GPIO_InitStructure;
    /* CAN register init */
    CAN_DeInit(CAN2);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);
    CAN_StructInit(&CAN_InitStructure);
    /* 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;
    CAN_InitStructure.CAN_TXFP = ENABLE;
    CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
    CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;
    CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
    CAN_InitStructure.CAN_Prescaler = 8;
    CAN_Init(CAN2, &CAN_InitStructure);
    /* CAN filter init */
    CAN_FilterInitStructure.CAN_FilterNumber = 14;
    CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
    CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
    CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000; //(((SGL_CAN1_MaskRcvID<<3)&0xFFFF0000)>>16);
    CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000; //(((SGL_CAN1_MaskRcvID<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF);
    CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000; //0xFFFF;
    CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000; //0xFFFF;
    CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0;
    CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
    CAN_FilterInit(&CAN_FilterInitStructure);
    /* Configure CAN pin: RX */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    /* Configure CAN pin: TX */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    GPIO_PinRemapConfig(GPIO_Remap_CAN2, ENABLE);
    CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

相关帖子

沙发
mochou|  楼主 | 2012-4-10 15:45 | 只看该作者
CAN1初始化部分如下,
void SGL_CAN1_Init(void)
{
       NVIC_InitTypeDef       NVIC_InitStructure;
    CAN_InitTypeDef        CAN_InitStructure;
    CAN_FilterInitTypeDef  CAN_FilterInitStructure;
    GPIO_InitTypeDef       GPIO_InitStructure;
    /* CAN register init */
    CAN_DeInit(CAN1);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
    CAN_StructInit(&CAN_InitStructure);
    /* 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;
    CAN_InitStructure.CAN_TXFP = ENABLE;
    CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
    CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;
    CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
    CAN_InitStructure.CAN_Prescaler = 8;
    CAN_Init(CAN1, &CAN_InitStructure);
    /* CAN filter init */
    CAN_FilterInitStructure.CAN_FilterNumber = 0;
    CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
    CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
    CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000; //(((SGL_CAN1_MaskRcvID<<3)&0xFFFF0000)>>16);
    CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000; //(((SGL_CAN1_MaskRcvID<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF);
    CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000; //0xFFFF;
    CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000; //0xFFFF;
    CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0;
    CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
    CAN_FilterInit(&CAN_FilterInitStructure);
    /* Configure CAN pin: RX */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    /* Configure CAN pin: TX */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    GPIO_PinRemapConfig(GPIO_Remap2_CAN1, ENABLE);
    CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

使用特权

评论回复
板凳
airwill| | 2012-4-10 19:20 | 只看该作者
CAN2就不工作, 什么现象, 出现故障,还是没有数据发出来

使用特权

评论回复
地板
mochou|  楼主 | 2012-4-11 09:51 | 只看该作者
谢谢,楼上哥们,检查了下,是硬件问题,现在可以了。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

58

主题

1426

帖子

8

粉丝