Voi_d 2015-11-02 13:45 采纳率: 0%
浏览 5977
已结题

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。这是为什么?


  • 写回答

2条回答

  • 耗子0_0 2015-11-16 08:43
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题