admin 管理员组

文章数量: 887007

编程器提取eeprom

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

我来传一下我的I2C 24CXX的控制器的代码

看看有没有什么要改的地方

#include

#include

#define AT24C02_WriteDeviceAddress 0xa0

#define AT24C02_ReadDeviceAddress 0xa1

sbit SCL=P1^0;

sbit SDA=P1^1;

unsigned int addr=0;

void Delay()

{

unsigned char i, j;

_nop_();

i = 2;

j = 199;

do

{

while (--j);

} while (--i);

}

void Start()

{

SDA=1;

Delay();

SCL=1;

Delay();

SDA=0;

Delay();

SCL=0;

Delay();

}

void Stop()

{

SCL=0;

SDA=0;

Delay();

SCL=1;

Delay();

SDA=1;

Delay();

}

void Ack()

{

SDA=0;

Delay();

SCL=1;

Delay();

SCL=0;

Delay();

SDA=1;

Delay();

}

void NoAck()

{

SDA=1;

Delay();

SCL=1;

Delay();

SCL=0;

Delay();

}

bit TestAck()

{

bit ErrorBit;

SDA=1;

Delay();

SCL=1;

Delay();

ErrorBit=SDA;

Delay();

SCL=0;

Delay();

return (ErrorBit);

}

bit Write8Bit(unsigned char input)

{

unsigned char temp;

for(temp=8;temp!=0;temp--)

{

SDA=(bit)(input&0x80);

Delay();

SCL=1;

Delay();

SCL=0;

Delay();

input=input<<1;

}

return 1;

}

void WriteI2C(unsigned char *Wdata,unsigned char RomAddress,unsigned char number)

{

Start();

Write8Bit(AT24C02_WriteDeviceAddress);

TestAck();

Write8Bit(RomAddress);

TestAck();

for(;number!=0;number--)

{

Write8Bit(*Wdata);

TestAck();

Wdata++;

}

Stop();

Delay();

}

unsigned char Read8Bit()

{

unsigned char temp,rbyte=0;

for(temp=8;temp!=0;temp--)

{

SCL=1;

Delay();

rbyte=rbyte<<1;

Delay();

rbyte=rbyte|((unsigned char)(SDA));

SCL=0;

Delay();

}

return (rbyte);

}

void ReadI2C(unsigned char *Wdata,unsigned char RomAddress,unsigned char bytes)

{

Start();

Write8Bit(AT24C02_WriteDeviceAddress);

TestAck();

Write8Bit(RomAddress);

TestAck();

Start();

Write8Bit(AT24C02_ReadDeviceAddress);

TestAck();

while(bytes!=1)

{

*Wdata=Read8Bit();

Ack();

Wdata++;

bytes--;

}

*Wdata=Read8Bit();

NoAck();

Stop();

}

void Main(void)

{

unsigned char ch[]={0,0,0,0,0,0,0,0};

unsigned char i=0;

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

{

WriteI2C(ch,addr,8);

addr=addr+8;

}

}

本文标签: 编程器提取eeprom