I2C (二) 硬件操作

只要能产生I2C时序即可,至于通过什么硬件产生,无所谓,目前有两种方式
1. GPIO
2. I2C控制器

GPIO 模拟I2C

  • 51单片机GPIO模拟I2C总线

    51单片机上没有i2c控制器,所以在51单片机上只能用gpio来模拟i2c控制器,拉高拉低gpio 来模拟 SCL SDA
    GPIO实现了I2C时序,那么这两根线就叫做I2C总线

  • stm32单片机GPIO模拟I2C总线

  • hi3521a 也有用GPIO来模拟I2C的实例

I2C控制器

  • 51单片机没有I2C控制器,所以只能通过GPIO来模拟I2C
  • stm32单片机I2C总线控制器代码
  • stm32单片机I2C总线控制器代码2

i2c控制器操作代码

  • 宏定义
#define I2C_COM_PORT                    I2C1
#define I2C_COM_CLK                    	RCC_APB1Periph_I2C1

#define I2C_COM_SCL_PIN                 GPIO_Pin_8
#define I2C_COM_SCL_GPIO_PORT           GPIOB
#define I2C_COM_SCL_GPIO_CLK            RCC_AHB1Periph_GPIOB
#define I2C_COM_SCL_SOURCE              GPIO_PinSource8
#define I2C_COM_SCL_AF                  GPIO_AF_I2C1

#define I2C_COM_SDA_PIN                 GPIO_Pin_9
#define I2C_COM_SDA_GPIO_PORT           GPIOB
#define I2C_COM_SDA_GPIO_CLK            RCC_AHB1Periph_GPIOB
#define I2C_COM_SDA_SOURCE              GPIO_PinSource9
#define I2C_COM_SDA_AF                  GPIO_AF_I2C1
  • 配置过程
1. 配置gpio复用
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_AHB1PeriphClockCmd(I2C_COM_SCL_GPIO_CLK, ENABLE);
	RCC_AHB1PeriphClockCmd(I2C_COM_SDA_GPIO_CLK, ENABLE);

	RCC_APB1PeriphClockCmd(I2C_COM_CLK, ENABLE);

	I2C_Cmd(I2C_COM_PORT, DISABLE);
	I2C_DeInit(I2C_COM_PORT);

	 /* Configure I2C pins: SCL and SDA ---------------------------------------*/
	GPIO_InitStructure.GPIO_Pin = I2C_COM_SCL_PIN | I2C_COM_SDA_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
	GPIO_Init(I2C_COM_SCL_GPIO_PORT, &GPIO_InitStructure);

	GPIO_PinAFConfig(I2C_COM_SCL_GPIO_PORT, I2C_COM_SCL_SOURCE, I2C_COM_SCL_AF);
	GPIO_PinAFConfig(I2C_COM_SDA_GPIO_PORT, I2C_COM_SDA_SOURCE, I2C_COM_SDA_AF);
2.

	I2C_InitTypeDef  I2C_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	 /* I2C configuration -----------------------------------------------------*/
	I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
	I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
	I2C_InitStructure.I2C_OwnAddress1 = rw_com;
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitStructure.I2C_ClockSpeed = 100000;
	I2C_Init(I2C_COM_PORT, &I2C_InitStructure);
	
	/* Configure and enable I2Cx event interrupt -------------------------------*/
	NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	 
	/* Configure and enable I2C1 error interrupt -------------------------------*/
	NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_Init(&NVIC_InitStructure);
	
	/* Enable I2C1 event and buffer interrupts */
	I2C_ITConfig(I2C1, I2C_IT_EVT | I2C_IT_BUF, ENABLE);
	/* Enable I2C1 Error interrupts */
	I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE);

	// 使能I2C
	I2C_Cmd(I2C_COM_PORT, ENABLE);


3.I2C_SendData(I2C1, I2C_Rx_DATA);
4. 收
	I2C_ReceiveData

你可能感兴趣的:(驱动)