资源描述
整理版
程序
/********************************************************************
* 文件名:I2C读写串行EEPROM
* 功能描述*实现字节写读,页写读操作显示
* 创作人:KEVIN 2011-09-07
********************************************************************/
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit scl=P1^5;
sbit sda=P3^6;
sbit key1=P3^2;
sbit key2=P3^3;
sbit key3=P3^4;
uchar a[10]={0xff};
/*****************************延时10ms*del**************************/
void Delay_10ms(uint del)
{
uint i,j;
for(i=0; i<del; i++)
for(j=0; j<1827; j++)
;
}
/*****************************起始信号******************************/
void start()
{
sda=1;
_nop_();
scl=1;
_nop_();
sda=0;
_nop_();
scl=0;
_nop_();
}
/*****************************终止信号******************************/
void stop()
{
sda=0;
_nop_();
scl=1;
_nop_();
sda=1;
_nop_();
scl=0;
_nop_();
}
/*****************************非应答信号*****************************/
void nack()
{
sda=1;
_nop_();
scl=1;
_nop_();
scl=0;
_nop_();
sda=0;
}
/*******************************应答信号*****************************/
void ack()
{
sda=0;
_nop_();
scl=1;
_nop_();
scl=0;
_nop_();
sda=1;
}
/*****************************应答位检查信号**************************/
void ack_test()
{
// uchar i;
scl=1;
_nop_();
sda=1;
while(1)
{
if(sda==1)
;
else
break;
}
scl=0;
_nop_();
}
/***************************写一个字节子程序**************************/
void writebyte(uchar input)
{
uchar i,temp;
temp=input;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
_nop_();
sda=CY;
_nop_();
scl=1;
_nop_();
scl=0;
_nop_();
}
}
/*****************************读一个字节子程序**************************/
uchar readbyte()
{
uchar i,rbyte;
for(i=0;i<8;i++)
{
scl=1;
_nop_();
rbyte=(rbyte<<1)|sda;
scl=0;
_nop_();
}
return rbyte;
}
/*******************************页写程序*********************************/
void Page_write(uchar add,uchar ddata)
{
uchar i;
Delay_10ms(10); //延时不能缺少,因为stop()函数后马上启动start()要延时
start();
writebyte(0xa0);
ack_test();
writebyte(add); //要写入的地址单元
ack_test();
for(i=0;i<10;i++)
{
writebyte(ddata);
ack_test();
ddata=_cror_(ddata,1); //循环右移
}
stop();
}
/*******************************字节写程序*******************************/
void Byte_write(uchar add,uchar ddata)
{
Delay_10ms(10); //延时不能缺少
start();
writebyte(0xa0);
ack_test();
writebyte(add); //要写入的地址单元
ack_test();
writebyte(ddata); //写入的数据
ack_test();
stop();
}
void main()
{
uchar i;
Byte_write(0x00,0x7e); //字节写入数据0x7e到地址单元0x00
Byte_write(0x01,0xe7); //字节写入数据0xe7到地址单元0x01
Page_write(0x10,0x7f); //页写入首个数据为0x7f到首地址为0x10单元
while(1)
{
while(key1==0)
{
start(); //随时读
writebyte(0xa0);//器件地址与写入指令
ack_test();
writebyte(0x00);//写入器件单元的首地址
ack_test();
start();
writebyte(0xa1);//器件地址与读出指令
ack_test();
P0=readbyte();
nack();
}
while(key2==0)
{
start(); //随时读
writebyte(0xa0);//器件地址与写入指令
ack_test();
writebyte(0x01);//写入器件单元的首地址
ack_test();
start();
writebyte(0xa1);//器件地址与读出指令
ack_test();
P0=readbyte();
nack();
}
while(key3==0)
{
start();
writebyte(0xa0);//先利用选择地址读方式启动页读
ack_test();
writebyte(0x10);//页读数据首地址
ack_test();
start();
writebyte(0xa1);
ack_test();
for(i=0;i<8;i++)
{
a[i]=readbyte();
ack();
}
nack();
for(i=0;i<8;i++)
{
P0=a[i];
Delay_10ms(50);//延时0.5S
}
}
}
}
接线图
该程序能实现字节写,字节读,页写,页读功能
EEPROM接线图
独立按键控制字节写、页写接线图
.
展开阅读全文