你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

FAQ提问:STM32F042F4P6 CAN初始化

[复制链接]
苏柚 提问时间:2015-10-15 17:04 /
收藏 评论2 发布时间:2015-10-15 17:04
2个回答
阿平 回答时间:2016-3-20 23:53:21
我也碰到了, 请大神帮忙分析一下
任风吹吹 回答时间:2016-6-6 12:11:20
本帖最后由 任风吹吹 于 2016-6-6 12:18 编辑

估计是重映射没有成功, SYSCFG的时钟没有打开.
在重映射PA11_PA12脚之前,必须先将SYS CFG的时钟打开,不然会重映射失败的,这个你可以在调试下查看系统配置寄存器的重映射标志状态确定是否重映射成功。

只有在重映射成功后再可以正确的初始化CAN。

如:
  1. #define __HAL_REMAP_PIN_ENABLE(__PIN_REMAP__)          do {assert_param(IS_HAL_REMAP_PIN((__PIN_REMAP__)));                 \
  2.                                                            SYSCFG->CFGR1 |= (__PIN_REMAP__);                                \
  3.                                                          }while(0)
复制代码

  1. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  2. __HAL_REMAP_PIN_ENABLE(HAL_REMAP_PA11_PA12);

  3. /* CAN configuration */
  4.   CAN_Config();
复制代码




CAN_Config()如下所示  :
  1. static void CAN_Config(void)
  2. {
  3.   GPIO_InitTypeDef  GPIO_InitStructure;
  4.   NVIC_InitTypeDef  NVIC_InitStructure;
  5.   CAN_InitTypeDef        CAN_InitStructure;
  6.   CAN_FilterInitTypeDef  CAN_FilterInitStructure;
  7.    
  8.   /* CAN GPIOs configuration **************************************************/

  9.   /* Enable GPIO clock */
  10.   RCC_AHBPeriphClockCmd(CAN_GPIO_CLK, ENABLE);

  11.   /* Connect CAN pins to AF7 */
  12.   GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT);
  13.   GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT);
  14.   
  15.   /* Configure CAN RX and TX pins */
  16.   GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN;
  17.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  18.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  19.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  20.   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  21.   GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure);

  22.   /* NVIC configuration *******************************************************/
  23.   NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn;
  24.   NVIC_InitStructure.NVIC_IRQChannelPriority = 0x0;
  25.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  26.   NVIC_Init(&NVIC_InitStructure);
  27.   
  28.   /* CAN configuration ********************************************************/  
  29.   /* Enable CAN clock */
  30.   RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);
  31.   
  32.   /* CAN register init */
  33.   CAN_DeInit(CANx);
  34.   CAN_StructInit(&CAN_InitStructure);

  35.   /* CAN cell init */
  36.   CAN_InitStructure.CAN_TTCM = DISABLE;
  37.   CAN_InitStructure.CAN_ABOM = DISABLE;
  38.   CAN_InitStructure.CAN_AWUM = DISABLE;
  39.   CAN_InitStructure.CAN_NART = DISABLE;
  40.   CAN_InitStructure.CAN_RFLM = DISABLE;
  41.   CAN_InitStructure.CAN_TXFP = DISABLE;
  42.   CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
  43.   CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
  44.    
  45.   /* CAN Baudrate = 1MBps (CAN clocked at 36 MHz) */
  46.   CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;
  47.   CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
  48.   CAN_InitStructure.CAN_Prescaler = 2;
  49.   CAN_Init(CANx, &CAN_InitStructure);

  50.   /* CAN filter init */
  51.   CAN_FilterInitStructure.CAN_FilterNumber = 0;
  52.   CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
  53.   CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
  54.   CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
  55.   CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
  56.   CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
  57.   CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
  58.   CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
  59.   CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
  60.   CAN_FilterInit(&CAN_FilterInitStructure);
  61.   
  62.   /* Transmit Structure preparation */
  63.   TxMessage.StdId = 0x321;
  64.   TxMessage.ExtId = 0x01;
  65.   TxMessage.RTR = CAN_RTR_DATA;
  66.   TxMessage.IDE = CAN_ID_STD;
  67.   TxMessage.DLC = 1;
  68.   
  69.   /* Enable FIFO 0 message pending Interrupt */
  70.   CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);
  71. }
复制代码




拿走不谢!
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版