新闻中心

EEPW首页>嵌入式系统>设计应用> AT24C1024 proteus 2-wire Serial EEPROM 1

AT24C1024 proteus 2-wire Serial EEPROM 1

作者: 时间:2016-11-13 来源:网络 收藏
第i地址写入i

注意,读数据时候 if((TWSR & 0xF8) != 0x10)//0x10 重复START 已发。。。不是0x08.还有2次读写之间稍微延时一会。否则不出结果

本文引用地址://m.amcfsurvey.com/article/201611/316380.htm

avr的数据手册写得很好,好多程序源码都有,怪不得这么流行

1

#include
#include " 1602.c"

#define uint unsigned int
#define uchar unsigned char

#define START TWCR=(1< #define STOP TWCR=(1<

voidiic_init()
{
TWBR=32;//8mhz/80= 100khz
TWCR=0;
TWSR=0;
//DDRC|=0X03;
//PORTC|=0X03;
}

uint write_B(uchar page,uint addr,uchar data )//page 0 . 1. 代表17位地址最高位
{
uchar s=0xa0|(page<<1);//at24 从地址
START;
while(!(TWCR & (1< if((TWSR & 0xF8) != 0x08)
return 0xffff;
////////////////
TWDR=s;
TWCR = (1< while(!(TWCR & (1< if((TWSR & 0xF8) != 0x18)
return 0xffff;
//// /////////
TWDR = (addr>>8); //先发高地址
TWCR = (1< while(!(TWCR & (1<
if((TWSR & 0xF8) != 0x28)
return 0xffff;
////////////////
TWDR = (addr&0xff); //地址低8位
TWCR = (1< while(!(TWCR & (1<
if((TWSR & 0xF8) != 0x28)//数据已发送接收到ACK

return 0xffff;
/////////////////
TWDR = data; //数据
TWCR = (1< while(!(TWCR & (1<
if((TWSR & 0xF8) != 0x28)//数据已发送接收到ACK

return 0xffff;

STOP;

}

uint read_B(uchar page,uint addr)
{
uchar data,s=0xa0|(page<<1);//at24 从地址.。@@写最后一位为0

START;
while(!(TWCR & (1<

if((TWSR & 0xF8) != 0x08)
return 0xffff;
////////////////
TWDR=s;
TWCR =(1< while (!(TWCR & (1< if((TWSR & 0xF8) != 0x18)
return 0xffff;
//// /////////
TWDR = (addr>>8); //先发高地址
TWCR = (1< while(!(TWCR & (1<
if((TWSR & 0xF8) != 0x28)
return 0xffff;
////////////////
TWDR = (addr&0xff); //地址低8位
TWCR = (1< while(!(TWCR & (1<
if((TWSR & 0xF8) != 0x28)//数据已发送接收到ACK

return 0xffff;
//重复START
START;
while(!(TWCR & (1<

if((TWSR & 0xF8) != 0x10)//0x10 重复START 已发
return 0xffff;

TWDR=s|(1);//最后一位1,读
TWCR = (1< while(!(TWCR & (1< if((TWSR & 0xF8) != 0x40)//SLA+R 已发送接收到ACK

return 0xffff;
//DDRC&=~(0X03);

TWCR = (1< while(!(TWCR & (1< data=TWDR;
STOP;
//DDRC|=(0X03);

return data;

}

void main()
{
uint i,j,k,r;
uchar temp[4], temp1[7]="t[ ]";
uchar table[255];

iic_init();
init_1602();

for(k=0;k<255;k++)
table[k]=k;

for(k=0;k<255;k++)
{
write_B(0,k,k);//第k地址写k
for(i=0;i<10;i++)//一定要延时
for(j=0;j<1141;j++);
}



for(i=0;i<1000;i++)//延时
for(j=0;j<1141;j++);

for(k=0;k<255;k++)
{
r=read_B(0,k);
r=r&0xff;

temp[0]=r/100+0;
temp[1]=r%100/10+0;
temp[2]=r%10+0;
temp[3]=0;

LCD_write_string(1,1,temp);


temp1[2]=k/100+0;
temp1[3]=k%100/10+0;
temp1[4]=k%10+0;
temp1[6]=0;



LCD_write_string(2,0,temp1);


for(i=0;i<1000;i++)
for(j=0;j<1141;j++);


}

while(1);
}



评论


技术专区

关闭