STM32单片机芯片与内部54 AT24C02读写 硬件IIC 标准库 HAL库 ...

打印 上一主题 下一主题

主题 1020|帖子 1020|积分 3060

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
目次
一、AT24C02硬件IIC-标准库
1、I2C_InitTypeDef
2、GPIO
3、数据写入
4、数据读取
二、AT24C02硬件IIC-HAL库
1、I2C_HandleTypeDef与I2C_InitTypeDef
2、GPIO
3、数据写入
4、数据读取

一、AT24C02硬件IIC-标准库

1、I2C_InitTypeDef

        硬件IIC就要和I2C的代码打交道了。I2C_InitTypeDef在stm32f10x_i2c.h中,重要设置I2C通信速率,I2C模式,SCL时钟线占空比,自身IIC地址,I2C相应使能,I2C寻址模式长度等。
  1. typedef struct
  2. {
  3.   uint32_t I2C_ClockSpeed;          /*!< Specifies the clock frequency.
  4.                                          This parameter must be set to a value lower than 400kHz */
  5.   uint16_t I2C_Mode;                /*!< Specifies the I2C mode.
  6.                                          This parameter can be a value of @ref I2C_mode */
  7.   uint16_t I2C_DutyCycle;           /*!< Specifies the I2C fast mode duty cycle.
  8.                                          This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */
  9.   uint16_t I2C_OwnAddress1;         /*!< Specifies the first device own address.
  10.                                          This parameter can be a 7-bit or 10-bit address. */
  11.   uint16_t I2C_Ack;                 /*!< Enables or disables the acknowledgement.
  12.                                          This parameter can be a value of @ref I2C_acknowledgement */
  13.   uint16_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.
  14.                                          This parameter can be a value of @ref I2C_acknowledged_address */
  15. }I2C_InitTypeDef;
复制代码
2、GPIO

        保持为复用开漏输出。
  1.   /* I2C_SCL、I2C_SDA*/
  2.   GPIO_InitStructure.GPIO_Pin = EEPROM_I2C_SCL_PIN;
  3.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  4.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;               // 开漏输出
  5.   GPIO_Init(EEPROM_I2C_SCL_PORT, &GPIO_InitStructure);
  6.        
  7.   GPIO_InitStructure.GPIO_Pin = EEPROM_I2C_SDA_PIN;
  8.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  9.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;               // 开漏输出
  10.   GPIO_Init(EEPROM_I2C_SDA_PORT, &GPIO_InitStructure);       
复制代码
3、数据写入

        可以看到硬件IIC的时序就是必要不断的判定I2C的各类寄存器,例如EV6、EV8等,而非模拟IIC直接判定IO是否拉高、拉低即可。
  1. uint32_t I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)
  2. {
  3.   I2CTimeout = I2CT_LONG_TIMEOUT;
  4.   while(I2C_GetFlagStatus(EEPROM_I2Cx, I2C_FLAG_BUSY))   
  5.   {
  6.     if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(4);
  7.   }
  8.   
  9.   /* Send START condition */
  10.   I2C_GenerateSTART(EEPROM_I2Cx, ENABLE);
  11.   
  12.   I2CTimeout = I2CT_FLAG_TIMEOUT;
  13.   /* Test on EV5 and clear it */
  14.   while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_MODE_SELECT))  
  15.   {
  16.     if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(5);
  17.   }
  18.   
  19.   /* Send EEPROM address for write */
  20.   I2C_Send7bitAddress(EEPROM_I2Cx, EEPROM_ADDRESS, I2C_Direction_Transmitter);
  21.   
  22.   I2CTimeout = I2CT_FLAG_TIMEOUT;
  23.   /* Test on EV6 and clear it */
  24.   while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))  
  25.   {
  26.     if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(6);
  27.   }
  28.   
  29.   /* Send the EEPROM's internal address to write to */   
  30.   I2C_SendData(EEPROM_I2Cx, WriteAddr);  
  31.   I2CTimeout = I2CT_FLAG_TIMEOUT;
  32.   /* Test on EV8 and clear it */
  33.   while(! I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  34.   {
  35.     if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(7);
  36.   }
  37.   /* While there is data to be written */
  38.   while(NumByteToWrite--)  
  39.   {
  40.     /* Send the current byte */
  41.     I2C_SendData(EEPROM_I2Cx, *pBuffer);
  42.     /* Point to the next byte to be written */
  43.     pBuffer++;
  44.   
  45.     I2CTimeout = I2CT_FLAG_TIMEOUT;
  46.     /* Test on EV8 and clear it */
  47.     while (!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  48.     {
  49.       if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(8);
  50.     }
  51.   }
  52.   /* Send STOP condition */
  53.   I2C_GenerateSTOP(EEPROM_I2Cx, ENABLE);
  54.   
  55.   return 1;
  56. }
复制代码
4、数据读取

  1. uint32_t I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
  2. {  
  3.   
  4.   I2CTimeout = I2CT_LONG_TIMEOUT;
  5.   
  6.   //*((u8 *)0x4001080c) |=0x80;
  7.   while(I2C_GetFlagStatus(EEPROM_I2Cx, I2C_FLAG_BUSY))
  8.   {
  9.     if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(9);
  10.    }
  11.   
  12.   /* Send START condition */
  13.   I2C_GenerateSTART(EEPROM_I2Cx, ENABLE);
  14.   //*((u8 *)0x4001080c) &=~0x80;
  15.   
  16.   I2CTimeout = I2CT_FLAG_TIMEOUT;
  17.   /* Test on EV5 and clear it */
  18.   while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
  19.   {
  20.     if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(10);
  21.    }
  22.   
  23.   /* Send EEPROM address for write */
  24.   I2C_Send7bitAddress(EEPROM_I2Cx, EEPROM_ADDRESS, I2C_Direction_Transmitter);
  25.   I2CTimeout = I2CT_FLAG_TIMEOUT;
  26.   /* Test on EV6 and clear it */
  27.   while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  28.   {
  29.     if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(11);
  30.    }
  31.    
  32.   /* Clear EV6 by setting again the PE bit */
  33.   I2C_Cmd(EEPROM_I2Cx, ENABLE);
  34.   /* Send the EEPROM's internal address to write to */
  35.   I2C_SendData(EEPROM_I2Cx, ReadAddr);  
  36.    
  37.   I2CTimeout = I2CT_FLAG_TIMEOUT;
  38.   /* Test on EV8 and clear it */
  39.   while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  40.   {
  41.     if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(12);
  42.    }
  43.    
  44.   /* Send STRAT condition a second time */  
  45.   I2C_GenerateSTART(EEPROM_I2Cx, ENABLE);
  46.   
  47.   I2CTimeout = I2CT_FLAG_TIMEOUT;
  48.   /* Test on EV5 and clear it */
  49.   while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
  50.   {
  51.     if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(13);
  52.    }
  53.    
  54.   /* Send EEPROM address for read */
  55.   I2C_Send7bitAddress(EEPROM_I2Cx, EEPROM_ADDRESS, I2C_Direction_Receiver);
  56.   
  57.   I2CTimeout = I2CT_FLAG_TIMEOUT;
  58.   /* Test on EV6 and clear it */
  59.   while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
  60.   {
  61.     if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(14);
  62.    }
  63.   
  64.   /* While there is data to be read */
  65.   while(NumByteToRead)  
  66.   {
  67.     if(NumByteToRead == 1)
  68.     {
  69.       /* Disable Acknowledgement */
  70.       I2C_AcknowledgeConfig(EEPROM_I2Cx, DISABLE);
  71.       
  72.       /* Send STOP Condition */
  73.       I2C_GenerateSTOP(EEPROM_I2Cx, ENABLE);
  74.     }
  75.     /* Test on EV7 and clear it */   
  76.     I2CTimeout = I2CT_LONG_TIMEOUT;
  77.    
  78.                 while(I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)==0)  
  79.                 {
  80.                         if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(3);
  81.                 }
  82.     {      
  83.       /* Read a byte from the EEPROM */
  84.       *pBuffer = I2C_ReceiveData(EEPROM_I2Cx);
  85.       /* Point to the next location where the byte read will be saved */
  86.       pBuffer++;
  87.       
  88.       /* Decrement the read bytes counter */
  89.       NumByteToRead--;        
  90.     }   
  91.   }
  92.   /* Enable Acknowledgement to be ready for another reception */
  93.   I2C_AcknowledgeConfig(EEPROM_I2Cx, ENABLE);
  94.   
  95.     return 1;
  96. }
复制代码


二、AT24C02硬件IIC-HAL库

1、I2C_HandleTypeDef与I2C_InitTypeDef

        硬件IIC就要和I2C的代码打交道了。I2C_HandleTypeDef在stm32f1xx_hal_i2c.h中,重要设置I2C通信速率,I2C模式,SCL时钟线占空比,自身IIC地址,I2C相应使能,I2C寻址模式长度等。
  1. typedef struct
  2. #endif  /* USE_HAL_I2C_REGISTER_CALLBACKS */
  3. {
  4.   I2C_TypeDef                *Instance;      /*!< I2C registers base address               */
  5.   I2C_InitTypeDef            Init;           /*!< I2C communication parameters             */
  6.   uint8_t                    *pBuffPtr;      /*!< Pointer to I2C transfer buffer           */
  7.   uint16_t                   XferSize;       /*!< I2C transfer size                        */
  8.   __IO uint16_t              XferCount;      /*!< I2C transfer counter                     */
  9.   __IO uint32_t              XferOptions;    /*!< I2C transfer options                     */
  10.   __IO uint32_t              PreviousState;  /*!< I2C communication Previous state and mode
  11.                                                   context for internal usage               */
  12.   DMA_HandleTypeDef          *hdmatx;        /*!< I2C Tx DMA handle parameters             */
  13.   DMA_HandleTypeDef          *hdmarx;        /*!< I2C Rx DMA handle parameters             */
  14.   HAL_LockTypeDef            Lock;           /*!< I2C locking object                       */
  15.   __IO HAL_I2C_StateTypeDef  State;          /*!< I2C communication state                  */
  16.   __IO HAL_I2C_ModeTypeDef   Mode;           /*!< I2C communication mode                   */
  17.   __IO uint32_t              ErrorCode;      /*!< I2C Error code                           */
  18.   __IO uint32_t              Devaddress;     /*!< I2C Target device address                */
  19.   __IO uint32_t              Memaddress;     /*!< I2C Target memory address                */
  20.   __IO uint32_t              MemaddSize;     /*!< I2C Target memory address  size          */
  21.   __IO uint32_t              EventCount;     /*!< I2C Event counter                        */
  22. } I2C_HandleTypeDef;
  23. typedef struct
  24. {
  25.   uint32_t ClockSpeed;       /*!< Specifies the clock frequency.
  26.                                   This parameter must be set to a value lower than 400kHz */
  27.   uint32_t DutyCycle;        /*!< Specifies the I2C fast mode duty cycle.
  28.                                   This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */
  29.   uint32_t OwnAddress1;      /*!< Specifies the first device own address.
  30.                                   This parameter can be a 7-bit or 10-bit address. */
  31.   uint32_t AddressingMode;   /*!< Specifies if 7-bit or 10-bit addressing mode is selected.
  32.                                   This parameter can be a value of @ref I2C_addressing_mode */
  33.   uint32_t DualAddressMode;  /*!< Specifies if dual addressing mode is selected.
  34.                                   This parameter can be a value of @ref I2C_dual_addressing_mode */
  35.   uint32_t OwnAddress2;      /*!< Specifies the second device own address if dual addressing mode is selected
  36.                                   This parameter can be a 7-bit address. */
  37.   uint32_t GeneralCallMode;  /*!< Specifies if general call mode is selected.
  38.                                   This parameter can be a value of @ref I2C_general_call_addressing_mode */
  39.   uint32_t NoStretchMode;    /*!< Specifies if nostretch mode is selected.
  40.                                   This parameter can be a value of @ref I2C_nostretch_mode */
  41. } I2C_InitTypeDef;
复制代码
2、GPIO

        保持为复用开漏输出。
  1.   GPIO_InitStruct.Pin       = I2Cx_SCL_PIN;
  2.   GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;
  3.   GPIO_InitStruct.Pull      = GPIO_NOPULL;
  4.   GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH;
  5.   
  6.   HAL_GPIO_Init(I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);
  7.    
  8.   /* I2C RX GPIO pin configuration  */
  9.   GPIO_InitStruct.Pin = I2Cx_SDA_PIN;   
  10.   HAL_GPIO_Init(I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);
复制代码
3、数据写入

        可以看到硬件IIC的时序就是必要不断的判定I2C的各类寄存器,例如EV6、EV8等,而非模拟IIC直接判定IO是否拉高、拉低即可。
  1. uint32_t I2C_EE_PageWrite(uint8_t* pBuffer, uint8_t WriteAddr, uint8_t NumByteToWrite)
  2. {
  3.         HAL_StatusTypeDef status = HAL_OK;
  4.         /* Write EEPROM_PAGESIZE */
  5.         status=HAL_I2C_Mem_Write(&I2C_Handle, EEPROM_ADDRESS,WriteAddr, I2C_MEMADD_SIZE_8BIT, (uint8_t*)(pBuffer),NumByteToWrite, 100);
  6.         while (HAL_I2C_GetState(&I2C_Handle) != HAL_I2C_STATE_READY)
  7.         {
  8.                
  9.         }
  10.         /* Check if the EEPROM is ready for a new operation */
  11.         while (HAL_I2C_IsDeviceReady(&I2C_Handle, EEPROM_ADDRESS, EEPROM_MAX_TRIALS, I2Cx_TIMEOUT_MAX) == HAL_TIMEOUT);
  12.         /* Wait for the end of the transfer */
  13.         while (HAL_I2C_GetState(&I2C_Handle) != HAL_I2C_STATE_READY)
  14.         {
  15.                
  16.         }
  17.         return status;
  18. }
复制代码
4、数据读取

  1. uint32_t I2C_EE_BufferRead(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead)
  2. {
  3.         HAL_StatusTypeDef status = HAL_OK;
  4.        
  5.         status=HAL_I2C_Mem_Read(&I2C_Handle,EEPROM_ADDRESS,ReadAddr, I2C_MEMADD_SIZE_8BIT, (uint8_t *)pBuffer, NumByteToRead,1000);
  6.         return status;
  7. }
复制代码
        可以看到HAL库下会方便一点,做了底层的深度封装,但是这么写,太受制于人了,和时序完全不打交道了。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

民工心事

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表