查看: 9781|回复: 1

STM32f0 i2c 从机如何配置,中断如何做?

[复制链接]

该用户从未签到

发表于 2013-8-8 15:54:39 | 显示全部楼层 |阅读模式
分享到:
   最近弄stm32f0的I2C,在网上查找资料,只找到作主机的,但想弄从机的,自己试了几天,一直弄不出,求各位大侠指点一下!!!

  GPIO_InitTypeDef  GPIO_InitStruct;
  I2C_InitTypeDef   I2C_InitStruct;
  NVIC_InitTypeDef NVIC_InitStructure;

/* Enable  GPIOA clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
//  /* Configure the I2C clock source. The clock is derived from the HSI */
//   RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);      
  /*!< sEE_I2C Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE);

  /*!< GPIO configuration */  
  /*!< Configure sEE_I2C pins: SCL */

  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
  GPIO_Init(GPIOB , &GPIO_InitStruct);
  
  /*!< Configure sEE_I2C pins: SDA */
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
  GPIO_Init(GPIOB , &GPIO_InitStruct);

/* Connect PXx to I2C_SCL*/
   GPIO_PinAFConfig( GPIOB , GPIO_PinSource8, GPIO_AF_1);
  /* Connect PXx to I2C_SDA*/
   GPIO_PinAFConfig( GPIOB ,GPIO_PinSource9, GPIO_AF_1);
  /* USE FOR SLAVE FOR IT*/
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_Init(GPIOB , &GPIO_InitStruct);
  

  /* I2C configuration */
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStruct.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
  I2C_InitStruct.I2C_DigitalFilter = 0x00;
  I2C_InitStruct.I2C_OwnAddress1 =0x02;
  I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStruct.I2C_Timing = ??;
  /* I2C Peripheral Enable */

  I2C_Cmd(I2C1, ENABLE);
  /* Apply I2C configuration after enabling it */
  I2C_Init(I2C1, &I2C_InitStruct);

//I2C_SlaveByteControlCmd(I2C1,ENABLE);

I2C_ITConfig(I2C1,I2C_IT_TXI | I2C_IT_RXI | I2C_IT_ADDRI | I2C_IT_NACKI | I2C_IT_STOPI | I2C_IT_TCI|I2C_IT_ERRI ,ENABLE );

  
  /* Reconfigure and enable I2C1 error interrupt to have the higher priority */
  NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
回复

使用道具 举报

  • TA的每日心情
    奋斗
    2015-11-4 19:13
  • 签到天数: 281 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2013-8-8 16:45:22 | 显示全部楼层
    这个代码看的头大啊,楼主用什么做主机控制,地址对么?
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-5-29 20:07 , Processed in 0.114261 second(s), 19 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.