打印
[STM32F2]

CAN过滤器设置的问题

[复制链接]
2713|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
zzz2012|  楼主 | 2014-5-13 15:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在设置过滤器时,我是这样设置的:

#define MAKE_ADDR(a)        (((a)&0x7f)<<21)
#define CAN_BOARDCAST_ADDR        0x7f

        tmp2 = MAKE_ADDR(0x7f);
        tmp = MAKE_ADDR(addr);
        //tmp = 0;
        CAN_FilterInitStructure.CAN_FilterNumber = 0;
        CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
        CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
        CAN_FilterInitStructure.CAN_FilterIdHigh = tmp >> 16;
        CAN_FilterInitStructure.CAN_FilterIdLow = tmp & 0x0ffff;

        CAN_FilterInitStructure.CAN_FilterMaskIdHigh = tmp2 >> 16;
        CAN_FilterInitStructure.CAN_FilterMaskIdLow = tmp2 & 0x0ffff;  
        CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FilterFIFO0;

        CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
        CAN_FilterInit(&CAN_FilterInitStructure);

        tmp = MAKE_ADDR(CAN_BOARDCAST_ADDR);
        CAN_FilterInitStructure.CAN_FilterNumber = 1;
        CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
        CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;

        CAN_FilterInitStructure.CAN_FilterIdHigh = tmp >> 16;
        CAN_FilterInitStructure.CAN_FilterIdLow = tmp & 0x0ffff;
        CAN_FilterInitStructure.CAN_FilterMaskIdHigh = tmp >> 16;
        CAN_FilterInitStructure.CAN_FilterMaskIdLow = tmp & 0x0ffff;  
        CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FilterFIFO0;
        CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
        CAN_FilterInit(&CAN_FilterInitStructure);

在发送CAN包时的ID是这样设的:
     //这个包是针对目的地址是0x7f的终端
     tmp = 0x5fe04044;
    CANx->sTxMailBox[mailbox_id].TIR = tmp;
    /* Set up the DLC */
    CANx->sTxMailBox[mailbox_id].TDTR &= (uint32_t)0xFFFFFFF0;
    CANx->sTxMailBox[mailbox_id].TDTR |= (dlc & 0x0f);

    /* Set up the data field */
    CANx->sTxMailBox[mailbox_id].TDLR = data[0];
    CANx->sTxMailBox[mailbox_id].TDHR = data[1];
    /* Request transmission */
    CANx->sTxMailBox[mailbox_id].TIR |= TMIDxR_TXRQ;       

但是问题是:接收端收不到数据
沙发
mmuuss586| | 2014-5-14 15:25 | 只看该作者
void Can1_NVIC_Config(void)
{
  NVIC_InitTypeDef  NVIC_InitStructure;
  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/**
  * @brief  Configures the CAN.
  * @param  None
  * @retval None
  */
void CAN_Config(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  
  //CAN GPIOs configuration **************************************************
  // Connect CAN pins to AF9
  GPIO_PinAFConfig(CAN_GPIO_RX_PORT, CAN_RX_SOURCE, CAN_AF_PORT);
  GPIO_PinAFConfig(CAN_GPIO_TX_PORT, CAN_TX_SOURCE, CAN_AF_PORT);
  
  // Configure CAN RX and TX pins
  GPIO_InitStructure.GPIO_Pin =  CAN_TX_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_Init(CAN_GPIO_TX_PORT, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_Init(CAN_GPIO_RX_PORT, &GPIO_InitStructure);

  // CAN configuration ********************************************************
  // Enable CAN clock
  RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);
  
  // CAN register init
  CAN_DeInit(CANx);

  // 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 = DISABLE;
  CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
   
  // CAN Baudrate = 1 MBps (CAN clocked at 30 MHz)
        //BaudRate = PCLK1/((BS1 + BS2 + 1)*Prescaler)=(168/4)/((5+8+1)*3)=1M
  CAN_InitStructure.CAN_BS1 = CAN_BS1_5tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
  CAN_InitStructure.CAN_Prescaler = 3;
  CAN_Init(CANx, &CAN_InitStructure);

  CAN_FilterInitStructure.CAN_FilterNumber = 0;

  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
  CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
  CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
  CAN_FilterInit(&CAN_FilterInitStructure);
  
  // Transmit Structure preparation
  TxMessage.StdId = 0x321;
  TxMessage.ExtId = 0x01;
  TxMessage.RTR = CAN_RTR_DATA;
  TxMessage.IDE = CAN_ID_STD;
  TxMessage.DLC = 8;
  
  // Enable FIFO 0 message pending Interrupt
  CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);
}

使用特权

评论回复
板凳
mmuuss586| | 2014-5-14 15:25 | 只看该作者
我以前用407,调通过的;
你可以参考下,上面是初始化部分;

使用特权

评论回复
地板
zzz2012|  楼主 | 2014-5-15 11:52 | 只看该作者
mmuuss586 发表于 2014-5-14 15:25
我以前用407,调通过的;
你可以参考下,上面是初始化部分;

非常感谢你的回复,按照你的设置是可以通过,但是这个设置相当于过滤器没有用啊

使用特权

评论回复
5
wyjben| | 2014-5-15 13:32 | 只看该作者
本帖最后由 wyjben 于 2014-5-15 14:52 编辑

        CAN_FilterInitStructure.CAN_FilterMaskIdHigh = tmp >> 16;
        CAN_FilterInitStructure.CAN_FilterMaskIdLow = tmp & 0x0ffff;  
       调整这两个位置的值,如用使用扩展ID,用以下方法:
       CAN_FilterInitStructure.CAN_FilterMaskIdLow设置为你想要接收到的ID左移3位,再和0XF8相与;
       CAN_FilterInitStructure.CAN_FilterMaskIdHigh 设置为你想要接收到的ID左移3位,再和0XFF相与;

使用特权

评论回复
6
zzz2012|  楼主 | 2014-5-16 09:01 | 只看该作者
wyjben 发表于 2014-5-15 13:32
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = tmp >> 16;
        CAN_FilterInitStructure.CA ...

根据你的方法,问题解决了,谢谢,结贴给分

使用特权

评论回复
7
yuanxinfei| | 2014-11-26 10:16 | 只看该作者
mmuuss586 发表于 2014-5-14 15:25
我以前用407,调通过的;
你可以参考下,上面是初始化部分;

407是168的主频,can的时钟为什么是30M的呢,是要设置的吗

使用特权

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

本版积分规则

3

主题

19

帖子

0

粉丝