药无尘 发表于 2023-2-24 15:21

灵动微 MM32F103CxT6硬件IIC

static void Config_I2cDevice ( uint32_t ulSpeed )

{

      GPIO_InitTypeDef xGPIO_InitStruct;

      I2C_InitTypeDef xI2C_InitStruct;

      

      /* Enable clock. */

      RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );      

      RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );

      

      /* Configure scl & sda GPIO parameters. */

      xGPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;

      xGPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

      xGPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;

      GPIO_Init( GPIOB, &xGPIO_InitStruct );

      

      /* Configure I2C parameters. */

      xI2C_InitStruct.I2C_ClockSpeed = ulSpeed;

      xI2C_InitStruct.I2C_Mode = I2C_Mode_MASTER;

      xI2C_InitStruct.I2C_OwnAddress = 0x0;

      if ( ulSpeed <= 100000 ) { /* Configure speed in standard mode */

                xI2C_InitStruct.I2C_Speed = I2C_Speed_STANDARD;

      } else { /* Configure speed in fast mode */

                xI2C_InitStruct.I2C_Speed = I2C_Speed_FAST;

      }

      I2C_Init( I2C1, &xI2C_InitStruct );

      

      /* Enable i2c1 port. */

      I2C_Cmd( I2C1, ENABLE );

}

I2C 硬件驱动
(a)、 根据官方提供源码修改,并且将其封装在 HAL_i2c 库文件内;
(b)、 该OLED 仅需要 写操作;
(c)、 在发送 STOP 停止信号前需延时 5us 才能正常显示(后续再找原因)。

复制
/**

* @briefWrites buffer of data to the I2C EEPROM.

* @parampxIO :typedef struct { GPIO_TypeDef* xPort; uint16_t usScl; uint16_t usSda; }I2C_GPIO_t;

* @parampBuffer : pointer to the buffercontaining the data to be written

*         to the EEPROM.

* @paramWriteAddr : EEPROM's internal address to write to.

* @paramNumByteToWrite : number of bytes to write to the EEPROM.

* @retval None

*/

ErrorStatus I2C_WriteBuffer( const I2C_GPIO_t* pxIO, I2C_TypeDef* I2Cx, uint8_t DeviceAddr, uint8_t WriteAddr, uint16_t NumByteToWrite, uint8_t* pBuffer)

{

      uint32_t I2C_Timeout;

      uint16_t write_Num;

      

      GPIO_InitTypeDef xGPIO_InitStruct;

      xGPIO_InitStruct.GPIO_Pin= pxIO->usScl | pxIO->usSda;

      xGPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;

      xGPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;

      GPIO_Init( pxIO->xPort, &xGPIO_InitStruct );

      I2C_Cmd(I2Cx, DISABLE);

      I2C_Send7bitAddress( I2Cx, DeviceAddr, I2C_Direction_Transmitter );

      I2C_Cmd(I2Cx, ENABLE);

      

      xGPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;

      GPIO_Init( pxIO->xPort, &xGPIO_InitStruct );

      

      I2C_SendData( I2Cx, WriteAddr );

      I2C_Timeout = 0x1000;

      

      while ( ( I2C_GetFlagStatus( I2Cx, I2C_STATUS_FLAG_TFE ) ) == RESET ) {

                if ( ( I2C_Timeout-- ) == 0 ) {

                        return ERROR;

                }

      }

      

      for ( write_Num=0; write_Num<NumByteToWrite; write_Num++ ) {

                I2C_Timeout = 0x1000;

      

                while ( ( I2C_GetFlagStatus( I2Cx, I2C_STATUS_FLAG_TFE ) ) == RESET ) {

                        if ( ( I2C_Timeout-- ) == 0 ) {

                              return ERROR;

                        }

                }

                I2C_SendData( I2Cx, *( pBuffer + write_Num ) );

      }

      

      /* 96MHz 工作频率下,延时5us. */

      for ( uint16_t i=480; i>0; i-- );

      

      I2C_GenerateSTOP( I2Cx, ENABLE );

      I2C_Timeout = 0x1000;

      

      while ( ( I2C_GetITStatus( I2Cx, I2C_IT_STOP_DET ) ) == RESET ) {

                if ( ( I2C_Timeout-- ) == 0 ) {

                        return ERROR;

                }

      }

      

      /* If all operations OK, return sEE_OK (0) */

return SUCCESS;

}


mmbs 发表于 2023-3-2 16:05

使用硬件iic读取sht20怎么清除所有标志位

hearstnorman323 发表于 2023-3-2 16:33

硬件I2C稳定吗               

robertesth 发表于 2023-3-2 16:44

硬件I2C稳定吗               

yeates333 发表于 2023-3-3 21:57

硬件II2C和软件I2C有什么区别

houjiakai 发表于 2023-3-3 22:15

硬件iic应答失败如何自动恢复            

wwppd 发表于 2023-3-3 22:24

如何直接利用MM32F103CxT6的硬件I2C操控

adolphcocker 发表于 2023-3-4 21:40

硬件I2C 到底是不是个坑            

iyoum 发表于 2023-3-4 21:56

IIC从机地址为什么右移一位?

robincotton 发表于 2023-3-4 22:07

硬件I2C中断方式和查询方式有什么区别

houjiakai 发表于 2023-3-4 22:13

模拟IIc总线如何设置总线传输速率

fengm 发表于 2023-3-4 22:33

硬件IIC需要外围上拉电阻吗               

sheflynn 发表于 2023-3-5 10:19

模拟I2C的最大速率是多少               

sanfuzi 发表于 2023-3-5 10:48

硬件IIC需要外围上拉电阻吗               

hearstnorman323 发表于 2023-3-5 11:51

硬件I2C中断方式和查询方式有什么区别

pentruman 发表于 2023-3-5 11:57

IIC从机地址为什么右移一位?

abotomson 发表于 2023-3-5 12:09

硬件IIC怎么接收不同字节的长度的数据

alvpeg 发表于 2023-3-5 12:16

硬件I2C 到底是不是个坑            

chenci2013 发表于 2023-3-5 12:45

I2c总线一般拿来实现什么功能?

hearstnorman323 发表于 2023-3-7 19:36

I2C总线的地址是如何确定的?
页: [1] 2
查看完整版本: 灵动微 MM32F103CxT6硬件IIC