1、硬件电路
写地址:0xA0
读地址:0xA1
存储容量:256Byte
2、设置RCC,选择高速外部时钟HSE,时钟设置为180MHz
3、配置IIC
4、生成工程配置
5、部分代码
#define IIC_WRITE_ADDR 0xA0 // IIC写地址
#define IIC_READ_ADDR 0xA1 // IIC读地址uint8_t write_buff[256] = {0}; // 发送数据缓冲区
uint8_t recv_buff[256] = {0}; // 接收数据缓冲区
/*** 发送数据函数* device_addr:EEPROM设备地址* data_addr:要写入EEPROM地址(地址范围0x00~0xff)小心越界* pData:发送数据首地址* Size:发送数据个数*/
void iic_Send_Data(I2C_HandleTypeDef *hi2c, uint8_t device_addr, uint8_t data_addr, uint8_t *pData, uint16_t Size)
{HAL_I2C_Mem_Write(hi2c,device_addr,data_addr,1,pData,Size,500);HAL_Delay(20); //延时一定要加不然数据可能写入不进去
}/*** 接收数据函数* device_addr:EEPROM设备地址* data_addr:要读取的EEPROM地址(地址范围0x00~0xff),小心越界* pData:用于存储数据的缓冲区的指针* Size:要读取的字节数*/
void iic_Receive_Data(I2C_HandleTypeDef *hi2c, uint8_t device_addr, uint8_t data_addr, uint8_t *pData, uint16_t Size)
{HAL_I2C_Mem_Read(hi2c,device_addr,data_addr,1,pData,Size,500);
}
/* USER CODE BEGIN 2 */printf("AT2402 readwrite test!!!\r\n");for(int i=0;i<256;i++){write_buff[i] = i;}for(int i=0;i<32;i++) //AT24C02页写入只支持8个byte,所以需要分32次写入。{iic_Send_Data(&hi2c1,IIC_WRITE_ADDR,8*i,&write_buff[8*i],8);}iic_Receive_Data(&hi2c1,IIC_READ_ADDR,0x00,recv_buff,256);for(int i=0;i<256;i++){printf("recv_buff[%03d] = 0x%02x\t",i,recv_buff[i]);if(0 == (i+1)%8 && (i+1)>=8){printf("\r\n");}}/* USER CODE END 2 */
6、实验现象