[MM32软件] 灵动微 MM32F103CxT6硬件IIC

[复制链接]
1307|24
 楼主| 药无尘 发表于 2023-2-24 15:21 | 显示全部楼层 |阅读模式
  1. static void Config_I2cDevice ( uint32_t ulSpeed )

  2. {

  3.         GPIO_InitTypeDef xGPIO_InitStruct;

  4.         I2C_InitTypeDef xI2C_InitStruct;

  5.         

  6.         /* Enable clock. */

  7.         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );        

  8.         RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );

  9.         

  10.         /* Configure scl & sda GPIO parameters. */

  11.         xGPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;

  12.         xGPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

  13.         xGPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;

  14.         GPIO_Init( GPIOB, &xGPIO_InitStruct );

  15.         

  16.         /* Configure I2C parameters. */

  17.         xI2C_InitStruct.I2C_ClockSpeed = ulSpeed;

  18.         xI2C_InitStruct.I2C_Mode = I2C_Mode_MASTER;

  19.         xI2C_InitStruct.I2C_OwnAddress = 0x0;

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

  21.                 xI2C_InitStruct.I2C_Speed = I2C_Speed_STANDARD;

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

  23.                 xI2C_InitStruct.I2C_Speed = I2C_Speed_FAST;

  24.         }

  25.         I2C_Init( I2C1, &xI2C_InitStruct );

  26.         

  27.         /* Enable i2c1 port. */

  28.         I2C_Cmd( I2C1, ENABLE );

  29. }

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

  34. 复制
  35. /**

  36.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Writes buffer of data to the I2C EEPROM.

  37.   * @param  pxIO :typedef struct { GPIO_TypeDef* xPort; uint16_t usScl; uint16_t usSda; }I2C_GPIO_t;

  38.   * @param  pBuffer : pointer to the buffer  containing the data to be written

  39.   *         to the EEPROM.

  40.   * @param  WriteAddr : EEPROM's internal address to write to.

  41.   * @param  NumByteToWrite : number of bytes to write to the EEPROM.

  42.   * @retval None

  43.   */

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

  45. {

  46.         uint32_t I2C_Timeout;

  47.         uint16_t write_Num;

  48.         

  49.         GPIO_InitTypeDef xGPIO_InitStruct;

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

  51.         xGPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;

  52.         xGPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;

  53.         GPIO_Init( pxIO->xPort, &xGPIO_InitStruct );

  54.         I2C_Cmd(I2Cx, DISABLE);

  55.         I2C_Send7bitAddress( I2Cx, DeviceAddr, I2C_Direction_Transmitter );

  56.         I2C_Cmd(I2Cx, ENABLE);

  57.         

  58.         xGPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;

  59.         GPIO_Init( pxIO->xPort, &xGPIO_InitStruct );

  60.         

  61.         I2C_SendData( I2Cx, WriteAddr );

  62.         I2C_Timeout = 0x1000;

  63.         

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

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

  66.                         return ERROR;

  67.                 }

  68.         }

  69.         

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

  71.                 I2C_Timeout = 0x1000;

  72.         

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

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

  75.                                 return ERROR;

  76.                         }

  77.                 }

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

  79.         }

  80.         

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

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

  83.         

  84.         I2C_GenerateSTOP( I2Cx, ENABLE );

  85.         I2C_Timeout = 0x1000;

  86.         

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

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

  89.                         return ERROR;

  90.                 }

  91.         }

  92.         

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

  94.   return SUCCESS;

  95. }


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总线的地址是如何确定的?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

79

主题

623

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部