51单片机通过IIC向EEPROM存储和读取数据

我用51单片机通过IIC向EEPROM存储数据,然后再把数据读出来,再通过串口发送出去,
再用串口调试工具(eaglecom)查看数据。。。最后发现一次只能发送或者读取12个
字节,大于12个字节之后的数据全是0xff。不知道原因是什么。

```#include
#include "delay.h"
#include "Uart.h"
#define ERROR 0
#define SUCCESS 1
#define MAX 50

sbit SDA = P1^7;
sbit SCK = P1^6;

unsigned char ack = 0;

void IIC_Start()
{
SDA = 1;
SCK = 1;
delay_us(1);
SDA = 0;
delay_us(1);
SCK = 0;
}

void IIC_Stop()
{
SDA = 0;
SCK = 1;
delay_us(1);
SDA = 1;
delay_us(1);
SCK = 0;
}

void IIC_ACK()
{
SDA = 0;
SCK = 1;
delay_us(1);
SCK = 0;
}

void IIC_NOACK()
{
SDA = 1;
SCK = 1;
delay_us(1);
SCK = 0;
}

void IIC_SendByte(unsigned char temp)
{
unsigned char i;
for(i = 0; i < 8; i++)
{
SDA = temp & 0x80;
SCK = 1;
delay_us(1);
SCK = 0;
delay_us(1);
temp <<= 1;
}
SCK = 1;
SDA = 1;
delay_us(1);
if(0 == SDA)
{
ack = 1;
}
else
{
ack = 0;
}

SCK = 0;

}

unsigned char IIC_RecvByte()
{
unsigned char i, temp;

SDA = 1;
for(i = 0; i < 8; i++)
{
    SCK = 0;
    delay_us(1);
    SCK = 1;
    temp <<= 1;
    if(1 == SDA)
    {
        temp = temp + 1;
    }
}
SCK = 0;
return temp;

}

unsigned char AT24C02_SendStr(unsigned char deceviceaddr, unsigned char romaaddr, unsigned char *s, unsigned char num)
{
unsigned char i;

IIC_Start();
IIC_SendByte(deceviceaddr);
if(0 == ack)
{
    return ERROR;
}

IIC_SendByte(romaaddr);
if(0 == ack)
{
    return ERROR;
}

for(i = 0; i < num; i++)
{
    IIC_SendByte(*s);
    if(0 == ack)
    {
        return ERROR;
    }
    s++;
}
IIC_Stop();

return SUCCESS;

}

unsigned char AT24C02_RecvStr(unsigned char deceviceaddr, unsigned char romaaddr, unsigned char *s, unsigned char num)
{
unsigned char i;

IIC_Start();
IIC_SendByte(deceviceaddr);
if(0 == ack)
{
    return ERROR;
}
IIC_SendByte(romaaddr);
if(0 == ack)
{
    return ERROR;
}
IIC_Start();
IIC_SendByte(deceviceaddr + 1);
if(0 == ack)
{
    return ERROR;
}

for(i = 0; i < num - 1; i++)
{
    *s = IIC_RecvByte();
    IIC_ACK();
    s++;
}
*s = IIC_RecvByte();
IIC_NOACK();
IIC_Stop();

return SUCCESS;

}

void main()
{
unsigned char i = 0;
unsigned char temp[MAX + 1];
unsigned char str[2];

Uart_init();

for(i = 0; i < MAX; i++)
{
    temp[i] = 7;
}

if(!AT24C02_SendStr(0xae, 100, temp, MAX))
{
    return;
}
delay_ms(200);

for(i = MAX; i < (MAX + 1) ;i++)
{
    temp[i] = 0;
}
delay_ms(200);

if(!AT24C02_RecvStr(0xae, 100, temp, MAX))
{
    return;
}
delay_ms(200);
Uart_SendStr(temp);

while(1);

}

如果用IICSendByte()和RecvByte(),一个字节一个字节发送和读取的话是不会出错的,但是封装成IICSendStr()和RecvByte()的话,发送或者是接收的字符串长度不能超过12。这是为什么?


程序上应该没什么问题,你的 Flash 规定一次最多可以写几个数据?

e2prom写入有个时间tWR要求,一般是5ms,如果写的间隔过小可能写不进去。为留有一定余量,最好设成10ms。