I2C_24c02实验

一、RCC初始化

  1. /* Setup the microcontroller system. Initialize the Embedded Flash Interface,
  2.      initialize the PLL and update the SystemFrequency variable. */
  3.   SystemInit();

  4. /* Enable peripheral clocks --------------------------------------------------*/
  5.   /* GPIOB Periph clock enable */
  6.   //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);

  7.     //启动GPIO模块时钟
  8.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
  9.     //把雕饰设置普通IO口
  10.     GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);

  11.   /* I2C1 Periph clock enable */
  12.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

二、I2C_初始化


  1. /* Configure I2C1 pins: SCL and SDA */
  2.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  3.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  4.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  5.   GPIO_Init(GPIOB, &GPIO_InitStructure);

  6.   I2C_InitTypeDef I2C_InitStructure;

  7.   /* I2C configuration */
  8.   I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  9.   I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  10.   I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;
  11.   I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  12.   I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  13.   I2C_InitStructure.I2C_ClockSpeed = 100000;
  14.   
  15.   /* I2C Peripheral Enable */
  16.   I2C_Cmd(I2C1, ENABLE);
  17.   /* Apply I2C configuration after enabling it */
  18.   I2C_Init(I2C1, &I2C_InitStructure);

三、USART初始化

  1.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  2.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  3.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  4.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  5.     
  6.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  7.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  8.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  9.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  10.     USART_InitStructure.USART_BaudRate = 115200;
  11.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  12.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  13.     USART_InitStructure.USART_Parity = USART_Parity_No;
  14.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  15.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  16.     USART_Init(USART1, &USART_InitStructure);
  17.     USART_Cmd(USART1, ENABLE);

四、main函数测试程序

  1. while (1)
  2. {    
  3.     I2C_EE_BufferWrite(ReadBuff, 0, 3);
  4.     I2C_EE_BufferRead(SendBuff, 0, 3);
  5.     Delay(0xfffff);
  6.     fPutString(SendBuff, 15);
  7. }


遇到的问题,我用以前的RCC初始化程序怎么都无法启动I2C。一直在  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));  中跑。
现在还没有搞清楚,I2C需要怎么设置时钟。




转载于:https://www.cnblogs.com/ch122633/p/7363268.html

你可能感兴趣的:(I2C_24c02实验)