我课程设计使用的EEPROM型号为X24C16P(2048字节),使用的X24C16P EEPROM读写程序为AT24C02C修改来的,两者读写原理上基本一致。我先放上一个经过PROTEUS仿真的程序,可用,我课设的程序在同学电脑上,一时拿不到:#include
#include
#define uchar unsigned char
#define uint unsigned int
#define DELAY5US _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
sbit VSDA = P1^0;
sbit VSCL = P1^1;
sbit LED = P1^7;
uchar SLAW;
void STA() //发现:24C02C 是 256字节*8位的串行EEPROM存储器芯片,注意是ROM只读的
{ //但是这个例子将0-255这些数据写入到了ROM当中,怎么写的,是烧进去的,
VSDA = 1; //这个不是很理解啊
VSCL = 1;
DELAY5US
VSDA = 0;
DELAY5US
VSCL = 0;
}
void STOP()
{
VSDA = 0;
VSCL = 1;
DELAY5US
VSDA = 1;
VSCL = 1;
DELAY5US
}
void MACK()
{
VSDA = 0;
VSCL = 1;
DELAY5US
VSCL = 0;
}
void MNACK()
{
VSDA = 1;
VSCL = 1;
DELAY5US
VSCL = 0;
}
void CACK()
{
VSDA = 1;
VSCL = 1;
F0 = 0;
if ( 1 == VSDA )
{
F0 = 1;
}
VSCL = 0;
}
void WRBYTE(uchar idata *p)
{
uchar idata n = 8, temp;
temp = *p;
while(n--)
{
if ( 0x80 == (temp&0x80) )
{
VSDA = 1;
VSCL = 1;
DELAY5US
VSCL = 0;
}
else
{
VSDA = 0;
VSCL = 1;
DELAY5US
VSCL = 0;
}
temp = temp<<1;
}
}
void RDBYTE(uchar idata *p)
{
uchar idata n = 8, temp = 0;
while(n--)
{
VSDA = 1;
VSCL = 1;
temp = temp<<1;
if ( 1 == VSDA )
temp = temp|0x01;
else
temp = temp&0xfe;
VSCL = 0;
}
*p = temp;
}
void delayMoreThan5ms()
{
uint i;
for ( i = 0; i < 1000; i++ )
{
DELAY5US
}
}
int main()
{
uchar ch, *p;
uint i;
SLAW = 0xA2; //0xA0是slave write地址字节(写),A是1010是器件地址,由厂家决定
for ( i = 0; i <= 255; i++ ) //1010(A)是24C02C所属系列的器件地址
{ //0000是自己决定的,前3个0由24C02C芯片的A0A1A2决定
STA(); //最后一个0是写的意思,1则为读
p = &SLAW; //4个(器件地址),3个(引脚地址),一个(读写选择位)。
WRBYTE(p); //应答信号是关键啊
CACK(); //发送对应写, 接收对应读
if ( 1 == F0 )
{
LED = 0;
while(1)
;
}
ch = i;
p = &ch;
WRBYTE(p);
CACK();
if ( 1 == F0 )
{
LED = 0;
while(1)
;
}
WRBYTE(p);
CACK();
STOP();
delayMoreThan5ms();
}
while(1)
;
return 0;
}