51单片机eeprom读取和写
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#define uint unsigned int
#define uchar unsigned char
sbit scl = P2^1;
sbit sda = P2^0;
void delay(){
;;
}
void delay1ms(uchar z){
uchar i,j;
for(i=0;i
}
void init(){
scl=1;
delay();
sda=1;
delay();
}
void start(){
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop(){
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons(){
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250)) i++;
scl=0;
delay();
}
void writebyte(uchar c){
uchar i,temp;
temp=c;
scl=0;
delay();
for(i=0;i<8;i++){
temp=temp<<1;
sda=CY;
delay();
scl=1;
delay();
scl=0;
delay();
}
sda=1;
delay();
}
uchar readbyte(){
uchar i,j,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++){
scl=1;
delay();
j=sda;
k=(k<<1)|j;
scl=0;
delay();
}
sda=1;
delay();
return k;
}
void i2cwrite(uchar addr,uchar ch){
start();
writebyte(0xa0);
respons();
writebyte(addr);
respons();
writebyte(ch);
respons();
stop();
}
uchar i2cread(uchar addr){
uchar temp;
start();
writebyte(0xa0);
respons();
writebyte(addr);
respons();
start();
writebyte(0xa1);
respons();
temp=readbyte();
stop();
return temp;
}
void main(){
init();
i2cwrite(10,0x85);
delay1ms(100);
P0=i2cread(10);
while(1);
}