arduino i2c EEPROM(AT24C02、AT24C08、AT24C16、AT24C32、AT24C64)驱动

说明

数据手册
使用arduino的Wire库来读写at24cxx,可以支持at24c02、at24c08、at24c16、at24c32、at24c64。支持24cxx的随机读写以及顺序读写功能

代码

at24cxx.c

#include 
#include 
#include "at24cxx.h"

void at24cxx_init(void)
{
    Wire.begin();
}

static void at24cxx_wait(int slave)
{
    int resault = 0;
    do
    {
        Wire.beginTransmission(slave);
        resault = Wire.endTransmission();
    } while (resault != 0);
}

void at24cxx_write_byte(int slave, int addr, int data)
{
    at24cxx_wait(slave);
    Wire.beginTransmission(slave);
    Wire.write(addr);  /*发送写地址*/
    Wire.write(data);  /*写数据*/
    Wire.endTransmission();
}

int at24cxx_read_byte(int slave, int addr)
{
    int  ret = 0xFF;
    at24cxx_wait(slave);
    Wire.beginTransmission(slave);
    Wire.write(addr);  /*发送读地址*/
    Wire.endTransmission();   
    /*读数据*/
    Wire.requestFrom(slave, 1);
    ret = Wire.read();
    return ret;
}

int at24cxx_write(int slave, int addr, const uint8_t *buf, int buflen)
{
    int left_len = buflen;
    int send_len = PAGE_SIZE-(addr%PAGE_SIZE);
    send_len = (left_len>send_len) ? send_len:left_len;
    while(left_len)
    {
        at24cxx_wait(slave);
        Wire.beginTransmission(slave);
        Wire.write(addr);  /*发送写地址*/
        Wire.write(buf, send_len);  /*写数据*/
        Wire.endTransmission();
        left_len -= send_len;
        addr += send_len;
        buf += send_len;
        send_len = (left_len>PAGE_SIZE) ? PAGE_SIZE:left_len;
    }
    return buflen;
}


int at24cxx_read(int slave, int addr, uint8_t *buf, int buflen)
{
    int left_len = buflen;
    int get_len = 0;
    while(left_len)
    {
        if(left_len > 32)
        {
            get_len = 32;
        }
        else
        {
            get_len = left_len;
        }
        at24cxx_wait(slave);
        Wire.beginTransmission(slave);
        Wire.write(addr);  /*发送读地址*/
        Wire.endTransmission();   
        /*读数据*/
        Wire.requestFrom(slave, get_len);
        for(int i=0; i<get_len; i++)
        {
            buf[i] = Wire.read();
        }
        left_len -= get_len;
        buf += get_len;
        addr += get_len;
    }
    return buflen;
}

at24cxx.h

#ifndef AT24CXX_H
#define AT24CXX_H

#define AT24C16

#ifdef AT24C02
#define PAGE_SIZE   (8)
#endif
#if defined (AT24C04) || defined (AT24C08) || defined (AT24C16)
#define PAGE_SIZE   (16)
#endif
#if defined(AT24C32) || defined(AT24C64)
#define PAGE_SIZE   (32)
#endif


void at24cxx_init(void);
int at24cxx_read_byte(int slave, int addr);
void at24cxx_write_byte(int slave, int addr, int data);
int at24cxx_write(int slave, int addr, const uint8_t *buf, int buflen);
int at24cxx_read(int slave, int addr, uint8_t *buf, int buflen);
#endif /*AT24C16_H*/

test.ino

#include "at24cxx.h"

void setup()
{
	Serial.begin(9600);
	while (!Serial)
		; // Leonardo: wait for serial monitor
	at24cxx_init();
}

void loop()
{
	int rcvlen = 0;
	int sendlen = 0;
	uint8_t data[129] = 
"https://gitee.com/two_salted_eggs/xcmd\r\n\
https://gitee.com/two_salted_eggs/xcmd\r\n\
https://gitee.com/two_salted_eggs/xcmd";
	sendlen = strlen((char*)data);
	Serial.print("at24cxx page rw test, msg size=");
	Serial.println(sendlen);
	at24cxx_write(0x50, 0, data, sendlen);
	data[0] = '\0';
	rcvlen = at24cxx_read(0x50, 0, data, sendlen);
	data[sendlen] = '\0';
	Serial.println((char*)data);

	Serial.print("at24cxx byte rw test, msg size=");
	Serial.println(sendlen);
	for(int i=0; i<128; i++)
	{
		at24cxx_write_byte(0x50, i, data[i]);
	}
	data[0] = '\0';
	for(int i=0; i<128; i++)
	{
		 data[i] = at24cxx_read_byte(0x50, i);
	}
	data[sendlen] = '\0';
	Serial.println((char*)data);
	Serial.println();
	delay(1000);
}```

你可能感兴趣的:(单片机,eeprom,arduino,单片机)