当前位置:网站首页>X24cxx series EEPROM chip C language universal reading and writing program
X24cxx series EEPROM chip C language universal reading and writing program
2022-06-23 04:36:00 【wanglong3713】
One 、 summary
Prior to 3 In this article , It introduces x24C01~x24C512 Read and write programs for , Related articles are as follows :
1.IIC drive :4 Bit nixie tube display module TM1637 chip C Language driver
2.AT24C01、AT24C02 Reading and writing :AT24C01/AT24C02 series EEPROM Chip MCU read and write driver
3.AT24C04、AT24C08、AT24C16 Reading and writing :AT24C04、AT24C08、AT24C16 series EEPROM Chip MCU read and write driver
4.AT24C32、AT24C64、AT24C128、AT24C256、AT24C512 Reading and writing :AT24C32、AT24C64、AT24C128、AT24C256、AT24C512 series EEPROM Chip MCU read and write driver
This blog post will synthesize the above articles , take x24C01-x24C512 Write a universal program , To facilitate different capacities EEPROM Chip transplantation . So , I spent a lot of money to buy x24C01-x24C512 common 10 individual EEPROM Chip and read / write module , The family photo is shown below :

All of the following procedures have been verified on all models of chips .
Two 、 Chip comparison
| model | Capacity bit | Capacity byte | the number of pages | byte / page | Device addressing bit | Number of addressable devices | WordAddress digit / Number of bytes | remarks |
|---|---|---|---|---|---|---|---|---|
| AT24C01 | 1k | 128 | 16 | 8 | A2A1A0 | 8 | 7/1 | WordAddress1 Bytes |
| AT24C02 | 2k | 256 | 32 | 8 | A2A1A0 | 8 | 8/1 | WordAddress1 Bytes |
| AT24C04 | 4k | 512 | 32 | 16 | A2A1 | 4 | 9/1 | WordAddress1 Bytes +P0 position |
| AT24C08 | 8k | 1024 | 64 | 16 | A2 | 2 | 10/1 | WordAddress 1 Bytes +P0、P1 position |
| AT24C16 | 16k | 2048 | 128 | 16 | - | 1 | 11/1 | WordAddress1 Bytes +P0、P1、P2 position |
| AT24C32 | 32k | 4k | 128 | 32 | A2A1A0 | 8 | 12/2 | WordAddress2 Bytes |
| AT24C64 | 64k | 8k | 256 | 32 | A2A1A0 | 8 | 13/2 | WordAddress2 Bytes |
| AT24C128 | 128k | 16k | 256 | 64 | A1A0 | 4 | 14/2 | WordAddress2 Bytes |
| AT24C256 | 256k | 32k | 512 | 64 | A1A0 | 4 | 15/2 | WordAddress2 Bytes |
| AT24C512 | 512k | 64k | 512 | 128 | A2A1A0 | 8 | 16/2 | WordAddress2 Bytes |
Through the above table , Before combination 3 Article introduction , We can conclude that ,x24C01/x24C02 Reading and writing is the most basic , On this basis , because x24C04/x24C08/x24C16 The number of storage addresses increases , You need to address the storage address , A few more , These figures are added to P0/P1/P2 namely “ Page selection bit ” On ; Still on this basis ,x24C32/x24C64/x24C128/x24C256/x24X512 More storage addresses for ,1 Bytes + Page selection bits are not enough , So simply put WordAddress Make it 2 Bytes of , No more page selection bits .
3、 ... and 、 Main program code
After the above analysis , We still use macro definition , First define the device model , And then in the way of conditional compilation , Compile different code for different chips .
/******************************************************************************* model Byte Capacity the number of pages Number of bytes in the page WORD_ADDR digit WORD_ADDR Number of bytes x24C01 128Byte 16 page 8Byte 7bit 1Byte x24C02 256Byte 32 page 8Byte 8bit 1Byte x24C04 512Byte 32 page 16Byte 9bit 1Byte x24C08 1024Byte 64 page 16Byte 10bit 1Byte x24C16 2048Byte 128 page 16Byte 11bit 1Byte x24C32 4096Byte 128 page 32Byte 12bit 2Byte x24C64 8192Byte 256 page 32Byte 13bit 2Byte x24C128 16384Byte 256 page 64Byte 14bit 2Byte x24C256 32768Byte 512 page 64Byte 15bit 2Byte x24C512 65536Byte 512 page 128Byte 16bit 2Byte *******************************************************************************/
#define READ_CMD 1
#define WRITE_CMD 0
#define x24C01// Device name ,x24C01~x24C512
#define DEV_ADDR 0xA0 // Device hardware address
#ifdef x24C01
#define PAGE_NUM 16 // the number of pages
#define PAGE_SIZE 8 // Page size ( byte )
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) // Total capacity ( byte )
#define ADDR_BYTE_NUM 1 // Number of address bytes
#endif
#ifdef x24C02
#define PAGE_NUM 32 // the number of pages
#define PAGE_SIZE 8 // Page size ( byte )
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) // Total capacity ( byte )
#define ADDR_BYTE_NUM 1 // Number of address bytes
#endif
#ifdef x24C04
#define PAGE_NUM 32 // the number of pages
#define PAGE_SIZE 16 // Page size ( byte )
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) // Total capacity ( byte )
#define ADDR_BYTE_NUM 1 // Number of address bytes
#endif
#ifdef x24C08
#define PAGE_NUM 64 // the number of pages
#define PAGE_SIZE 16 // Page size ( byte )
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) // Total capacity ( byte )
#define ADDR_BYTE_NUM 1 // Number of address bytes
#endif
#ifdef x24C16
#define PAGE_NUM 128 // the number of pages
#define PAGE_SIZE 16 // Page size ( byte )
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) // Total capacity ( byte )
#define ADDR_BYTE_NUM 2 // Number of address bytes
#endif
#ifdef x24C32
#define PAGE_NUM 128 // the number of pages
#define PAGE_SIZE 32 // Page size ( byte )
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) // Total capacity ( byte )
#define ADDR_BYTE_NUM 2 // Number of address bytes
#endif
#ifdef x24C64
#define PAGE_NUM 256 // the number of pages
#define PAGE_SIZE 32 // Page size ( byte )
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) // Total capacity ( byte )
#define ADDR_BYTE_NUM 2 // Number of address bytes
#endif
#ifdef x24C128
#define PAGE_NUM 256 // the number of pages
#define PAGE_SIZE 64 // Page size ( byte )
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) // Total capacity ( byte )
#define ADDR_BYTE_NUM 2 // Number of address bytes
#endif
#ifdef x24C256
#define PAGE_NUM 512 // the number of pages
#define PAGE_SIZE 64 // Page size ( byte )
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) // Total capacity ( byte )
#define ADDR_BYTE_NUM 2 // Number of address bytes
#endif
#ifdef x24C512
#define PAGE_NUM 512 // the number of pages
#define PAGE_SIZE 128 // Page size ( byte )
#define CAPACITY_SIZE (PAGE_NUM * PAGE_SIZE) // Total capacity ( byte )
#define ADDR_BYTE_NUM 2 // Number of address bytes
#endif
/******************************************************************************* * Function name :x24Cxx_WriteByte * work can : Write a byte * ginseng Count :u16Addr Address to write to u8Data The data to be written * Return value : nothing * say bright : Device address ( Contains the write command ) -> 1 or 2 Bytes WORD ADDR -> data *******************************************************************************/
void x24Cxx_WriteByte(uint16_t u16Addr, uint8_t u8Data)
{
x24Cxx_WriteEnable();// Enable writing
IIC_Start();// Start signal
#if (ADDR_BYTE_NUM == 1)// The address is 1 Bytes
{
IIC_WriteByte(DEV_ADDR | WRITE_CMD | (((uint8_t)((u16Addr >> 8) & 0x07)) << 1));// Device addressing + Write + Page selection bit
IIC_WaitAck();// Waiting for an answer
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));// Only take the low byte of the address
IIC_WaitAck();// Waiting for an answer
}
#endif
#if (ADDR_BYTE_NUM == 2)// The address is 2 Bytes
{
IIC_WriteByte(DEV_ADDR | WRITE_CMD);// Device addressing + Write
IIC_WaitAck();// Waiting for an answer
IIC_WriteByte((uint8_t)((u16Addr >> 8) & 0xFF));// Address high byte
IIC_WaitAck();// Waiting for an answer
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));// Address low byte
IIC_WaitAck();// Waiting for an answer
}
#endif
IIC_WriteByte(u8Data);
IIC_WaitAck();// Waiting for an answer
IIC_Stop();
x24Cxx_WriteDisble();// Writing is prohibited
}
/******************************************************************************* * Function name :x24Cxx_ReadByte * work can : Read a byte * ginseng Count :u16Addr Address to read * Return value :u8Data Read out data * say bright : nothing *******************************************************************************/
uint8_t x24Cxx_ReadByte(uint16_t u16Addr)
{
uint8_t u8Data = 0;
IIC_Start();// Start signal
#if (ADDR_BYTE_NUM == 1)// The address is 1 Bytes
{
IIC_WriteByte(DEV_ADDR | WRITE_CMD | (((uint8_t)((u16Addr >> 8) & 0x07)) << 1));// Device addressing + Write + Page selection bit
IIC_WaitAck();// Waiting for an answer
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));// Only take the low byte of the address
IIC_WaitAck();// Waiting for an answer
}
#endif
#if (ADDR_BYTE_NUM == 2)// The address is 2 Bytes
{
IIC_WriteByte(DEV_ADDR | WRITE_CMD);// Device addressing + Write
IIC_WaitAck();// Waiting for an answer
IIC_WriteByte((uint8_t)((u16Addr >> 8) & 0xFF));// Address high byte
IIC_WaitAck();// Waiting for an answer
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));// Address low byte
IIC_WaitAck();// Waiting for an answer
}
#endif
IIC_Start();// Start signal
IIC_WriteByte(DEV_ADDR | READ_CMD);// Device addressing + read
IIC_WaitAck();// Waiting for an answer
u8Data = IIC_ReadByte();
IIC_NoAck();
IIC_Stop();
return u8Data;
}
/******************************************************************************* * Function name :x24Cxx_WritePage * work can : Page writing * ginseng Count :u16Addr First address to write ; u8Len Number of data bytes written , The maximum is PAGE_SIZE pData First address of data to be written * Return value : nothing * say bright : Maximum write 1 page , Prevent roll over , If the address is spread across pages, remove the spread part *******************************************************************************/
void x24Cxx_WritePage(uint16_t u16Addr, uint8_t u8Len, uint8_t *pData)
{
uint8_t i;
if (u8Len > PAGE_SIZE)// The length is greater than the length of the page
{
u8Len = PAGE_SIZE;
}
if ((u16Addr + (uint16_t)u8Len) > CAPACITY_SIZE)// Excess capacity
{
u8Len = (uint8_t)(CAPACITY_SIZE - u16Addr);
}
if (((u16Addr % PAGE_SIZE) + (uint16_t)u8Len) > PAGE_SIZE)// Determine whether to spread pages
{
u8Len -= (uint8_t)((u16Addr + (uint16_t)u8Len) % PAGE_SIZE);// Cross page , Cut off the spread
}
x24Cxx_WriteEnable();// Enable writing
IIC_Start();// Start signal
#if (ADDR_BYTE_NUM == 1)// The address is 1 Bytes
{
IIC_WriteByte(DEV_ADDR | WRITE_CMD | (((uint8_t)((u16Addr >> 8) & 0x07)) << 1));// Device addressing + Write + Page selection bit
IIC_WaitAck();// Waiting for an answer
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));// Only take the low byte of the address
IIC_WaitAck();// Waiting for an answer
}
#endif
#if (ADDR_BYTE_NUM == 2)// The address is 2 Bytes
{
IIC_WriteByte(DEV_ADDR | WRITE_CMD);// Device addressing + Write
IIC_WaitAck();// Waiting for an answer
IIC_WriteByte((uint8_t)((u16Addr >> 8) & 0xFF));// Address high byte
IIC_WaitAck();// Waiting for an answer
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));// Address low byte
IIC_WaitAck();// Waiting for an answer
}
#endif
for (i = 0; i < u8Len; i++)
{
IIC_WriteByte(*(pData + i));
IIC_WaitAck();// Waiting for an answer
}
IIC_Stop();
x24Cxx_WriteDisble();// Writing is prohibited
}
/******************************************************************************* * Function name :x24Cxx_ReadPage * work can : Page reading * ginseng Count :u16Addr The first address to read ; u8Len Number of data bytes read , The maximum is PAGE_SIZE pBuff Read the cache in which the data is stored * Return value : nothing * say bright : nothing *******************************************************************************/
void x24Cxx_ReadPage(uint16_t u16Addr, uint8_t u8Len, uint8_t *pBuff)
{
uint8_t i;
if (u8Len > PAGE_SIZE)// The length is greater than the length of the page
{
u8Len = PAGE_SIZE;
}
if ((u16Addr + (uint16_t)u8Len) > CAPACITY_SIZE)// Excess capacity
{
u8Len = (uint8_t)(CAPACITY_SIZE - u16Addr);
}
if (((u16Addr % PAGE_SIZE) + (uint16_t)u8Len) > PAGE_SIZE)// Determine whether to spread pages
{
u8Len -= (uint8_t)((u16Addr + (uint16_t)u8Len) % PAGE_SIZE);// Cross page , Cut off the spread
}
IIC_Start();// Start signal
#if (ADDR_BYTE_NUM == 1)// The address is 1 Bytes
{
IIC_WriteByte(DEV_ADDR | WRITE_CMD | (((uint8_t)((u16Addr >> 8) & 0x07)) << 1));// Device addressing + Write + Page selection bit
IIC_WaitAck();// Waiting for an answer
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));// Only take the low byte of the address
IIC_WaitAck();// Waiting for an answer
}
#endif
#if (ADDR_BYTE_NUM == 2)// The address is 2 Bytes
{
IIC_WriteByte(DEV_ADDR | WRITE_CMD);// Device addressing + Write
IIC_WaitAck();// Waiting for an answer
IIC_WriteByte((uint8_t)((u16Addr >> 8) & 0xFF));// Address high byte
IIC_WaitAck();// Waiting for an answer
IIC_WriteByte((uint8_t)(u16Addr & 0xFF));// Address low byte
IIC_WaitAck();// Waiting for an answer
}
#endif
IIC_Start();// Start signal
IIC_WriteByte(DEV_ADDR | READ_CMD);// Device addressing + read
IIC_WaitAck();// Waiting for an answer
for (i = 0; i < (u8Len - 1); i++)
{
*(pBuff + i) = IIC_ReadByte();
IIC_Ack();
}
*(pBuff + u8Len - 1) = IIC_ReadByte();
IIC_NoAck();// The last one doesn't answer
IIC_Stop();
}
Four 、 summary
1. The device address must be the same as A2/A1/A0 The hardware connection of the pin corresponds to ;
2. This procedure , Address written or read , Do not exceed the capacity of the chip , Otherwise, the result will be abnormal , Readers can modify the program by themselves , Check the compliance of the address with more comprehensive functions ;
3.x24Cxx_WriteEnable() Function is WP The write protect function is enabled , It is defined according to the pin configuration of the single chip microcomputer ;
4. Call the writer ( Whether it's a single byte write or a page write ), Delay required 10ms( namely twr, Some chip manuals say 5ms) Then operate the device , Otherwise, the device will not respond to the command during this time ;
边栏推荐
猜你喜欢

svg d3. JS generate tree tree view

深度学习 TensorFlow入门

A summary of PostgreSQL data types. All the people are here

It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!

What is metadata

Monitoring artifact ZABBIX, from deployment to application, goes deep layer by layer

Twitter与Shopify合作 将商家产品引入Twitter购物当中

② cocoapods原理及 PodSpec 文件上传操作

Leetcode 1208. 尽可能使字符串相等(终于解决,晚安)

无线网络安全的12个优秀实践
随机推荐
PTA:7-86 集合的模拟实现(函数模板)
在线文本过滤小于指定长度工具
PTA:6-29 虚基类的应用-人与教师学生
[从零开始学习FPGA编程-40]:进阶篇 - 设计-竞争与风险Risk或冒险
Introduction to deep learning
语料库数据处理个案实例(分词和分句、词频统计、排序)
Twitter cooperates with Shopify to introduce merchant products into twitter shopping
【多模态】UNIMO
[advanced binary tree] AVLTree - balanced binary search tree
Sessions and Daemons
Implementation of VGA protocol based on FPGA
Pytoch --- pytoch customizes the dataset
无线网络安全的12个优秀实践
静态查找表和静态查找表
Online JSON to CSharp (c) class tool
【深度学习】深度学习推理框架 TensorRT MNN OpenVINO ONNXRuntime
会话和守护进程
Flutter怎么实现不同缩放动画效果
Similar to RZ / SZ, trzsz supporting TMUX has released a new version
Redis启动有问题