fm24c16c语言程序,24CXX读写驱动C51程序

原标题:24CXX读写驱动C51程序

/**************************

文件所用资源

1.:P0.2,P0.3

2.调用delay_ms函数

**************************/

/************************

端口定义

************************/

sbit _dat =P0^2;

sbit i2c_clk =P0^3;

#IIC_TIME 10 //IIC操作延时

//========在此设定芯片地址=============

#define W_ADD_COM 0xa0 //写字节命令及器件地址(根据地址实际情况改变), 1010 A2 A1 A0 0

#define R_ADD_COM 0xa1 //读命令字节及器件地址(根据地址实际情况改变), 1010 A2 A1 A0 1

//=======在此设定芯片型号, 1代表24C01; 16代表24C16; 512代表24C512

#define e2prom 256 //

#if e2prom==1

#define PAGE_SIZE 8

#define SIZE 0x00f

#elif e2prom==2

#define PAGE_SIZE 8

#define SIZE 0x00ff

#elif e2prom==4

#define PAGE_SIZE 16

#define SIZE 0x01ff

#elif e2prom==8

#define PAGE_SIZE 16

#define SIZE 0x03ff

#elif e2prom==16

#define PAGE_SIZE 16

#define SIZE 0x07ff

#elif e2prom==32

#define PAGE_SIZE 32

#define SIZE 0x0fff

#elif e2prom==64

#define PAGE_SIZE 32

#define SIZE 0x1fff

#elif e2prom==128

#define PAGE_SIZE 64

#define SIZE 0x3fff

#elif e2prom==256

#define PAGE_SIZE 64

#define SIZE 0x7fff

#elif e2prom==512

#define PAGE_SIZE 128

#define SIZE 0xffff

#endif

/************************

2456 start

************************/

start()

{

i2c_dat=1; //数据

i2c_clk=1; //时钟

i2c_dat=0; //数据

i2c_clk=0; //时钟

}

/************************

24c256 stop

************************/

void stop()

{

i2c_dat=0; //数据

i2c_clk=1; //时钟

i2c_dat=1; //数据

}

/************************

24c256 mack

************************/

void mack()

{

i2c_dat=0; //数据

i2c_clk=1; //时钟

i2c_clk=0; //时钟

}

/************************

24c256 mnack

************************/

void mnack()

{

i2c_dat=1; //数据

i2c_clk=1; //时钟

i2c_clk=0; //时钟

}

/************************

24c256 ack

************************/

voisk()

{

uint i;

i2c_dat=1; //数据

i2c_clk=1; //时钟

for(i=0;i<200;i++)

{

if(!i2c_dat)break;

}

i2c_clk=0; //时钟

}

/************************

24c256写一个字节

************************/

void write_i2c_byte(uchar dat)

{

uchar i;

for(i=0;i<8;i++)

{

if(dat & 0x80)

i2c_dat=1; //数据

ei2c_dat=0;

i2c_clk=1; //时钟

dat = dat << 1;

i2c_clk=0; //时钟

}

}

/************************

24c256写一个字

************************/

void write_i2c_word(uint x)

{

uchar i,j;

for(j=0;j<2;j++)

{

for(i=0;i<8;i++)

{

if(x & 0x8000)

i2c_dat=1; //数据

else i2c_dat=0;

i2c_clk=1; //时钟

x = x << 1;

i2c_clk=0; //时钟

}

ask();

}

}

/************************

24c256读一个字节

************************/

uchar read_i2c_byte()

{

uchar i,dat=0;

for(i=0;i<8;i++)

{

i2c_dat=1; //数据

i2c_clk=1; //时钟

dat=dat<<1;

if(i2c_dat)dat=dat|0x01;

i2c_clk=0; //时钟

}

urn(dat);

}

/*****************************

读24c256指定地址的一个字节

*****************************/

uchar read_i2c_dat(uint addrr)

{

uchar dat;

start();

write_i2c_byte(W_ADD_COM);

ask();

write_i2c_word(addrr);

start();

write_i2c_byte(R_ADD_COM);

ask();

dat=read_i2c_byte();

mnack();

stop();

return (dat);

}

/*****************************

写24c256指定地址的一个字节

*****************************/

void write_i2c_dat(uint addrr,uchar dat)

{

start();

write_i2c_byte(W_ADD_COM);

ask();

write_i2c_word(addrr);

write_i2c_byte(dat);

ask();

stop();

}

/***********************************

读24c256指定地址连续读取N个字节

***********************************/

void read_i2c_n_dat(uint addrr,uint num,uchar *p)

{

uint i;

start();

write_i2c_byte(W_ADD_COM);

ask();

write_i2c_word(addrr);

start();

write_i2c_byte(R_ADD_COM);

ask();

for(i=0;i

{

*p++=read_i2c_byte();

mack();

}

*p++=read_i2c_byte();

mnack();

stop();

}

/*********************************

从24c256指定地址连续写N个字节

*********************************/

void write_i2c_n_dat(uint addrr,uint number,uchar *p)

{

uint i;

start();

write_i2c_byte(W_ADD_COM);

ask();

write_i2c_word(addrr);

for(i=0;i

{

write_i2c_byte(*p++);

ask();

addrr++;

if(addrr%PAGE_SIZE==0)

{

stop();

delay_ms(IIC_TIME);

start();

write_i2c_byte(W_ADD_COM);

ask();

write_i2c_word(addrr);

}

}

stop();

}

责任编辑:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值