/******************************************************
本实验将往EEPROM的0存储地址中写入10个数据,然后按S5和S6
来显示分别显示它们,AT24C64的页写大小为32个字节,程序中用
Addr%32来判断是否到达页底。学者可以通过改变或者把写部份
注释掉来验证数据是否存入EEPROM
*******************************************************/
#include "includes.h"
sbit SDA = P2^0;
sbit SCL = P2^1;
sbit Key0 = P3^2;
sbit Key1 = P3^3;
void Start();
void Stop();
unsigned char Ack();
void ReAck();
void NoAck();
unsigned char SendOneByte(unsigned char dat);
unsigned char ReceiveOneByte(unsigned char Respond);
void WriteEEPROM(unsigned char *buff, unsigned int Addr, unsigned char Len);
void ReadEEPROM(unsigned char *buff, unsigned int Addr, unsigned char Len);
extern void Display(unsigned char Number);
extern void SystemInit();
void main(void)
{
unsigned char WriteBuff[10];
unsigned char ReadBuff[10];
unsigned char i;
unsigned char j;
SystemInit();
Display(77);
j = 0;
for(i=0;i<10;i++)
{
WriteBuff[i] = i;
ReadBuff[i] = 0;
}
//WriteEEPROM(WriteBuff, 0, 10);
//ReadEEPROM(ReadBuff,0,10);
P2 = 0x0f;
while(1)
{
if(!Key0)
{
while(!Key0);
if(j<sizeof(ReadBuff))
{
j++;
Display(ReadBuff[j]);
}
}
if(!Key1)
{
while(!Key1);
if(j>0)
{
j--;
Display(ReadBuff[j]);
}
}
}
}
void WriteEEPROM(unsigned char *buff, unsigned int Addr, unsigned char Len)
{
while(Len)
{
//发送启动
Start();
//发送设备从地址
SendOneByte(0xa0);
//发送存储地址高8位
SendOneByte(Addr >> 8);
//发送存储地址低8位
SendOneByte(Addr);
while(Len)
{
//发送数据
SendOneByte(*buff);
//数据指针加1
buff++;
//长度减1
Len--;
//地址指针加1
Addr++;
//是否到达页底
if(Addr%32)
{
//发送停止
Stop();
break;
}
}
}
//发送停止
Stop();
}
void ReadEEPROM(unsigned char *buff, unsigned int Addr, unsigned char Len)
{
//发送启动
Start();
//发送设备从地址
SendOneByte(0xa0);
//发送存储地址的高8位
SendOneByte(Addr >> 8);
//发送存储地址的低8位
SendOneByte(Addr);
while(Len)
{
if(Len>1)
*buff = ReceiveOneByte(true);
else
*buff = ReceiveOneByte(false);
}
//发送停止
Stop();
}
void Start()
{
//SCL拉高
SCL = 1;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
//SDA产生下降沿
SDA = 0;
_nop_();_nop_();_nop_();_nop_();
}
void Stop()
{
//SCL拉高
SCL = 1;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
//SDA产生上升沿
SDA = 1;
_nop_();_nop_();_nop_();_nop_();
}
unsigned char Ack()
{
unsigned char ack;
SCL = 1;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
if(SDA)
ack = false;
else
ack = true;
SCL = 0;
_nop_();_nop_();_nop_();_nop_();
SDA = 0;
return ack;
}
//发送一个0表示应答
void ReAck()
{
SDA = 0;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
SCL = 1;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
SCL = 0;
}
//发送一个1表示不应答
void NoAck()
{
SDA = 1;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
SCL = 1;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
SCL = 0;
_nop_();_nop_();_nop_();_nop_();
SDA = 0;
}
unsigned char SendOneByte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(dat & 0x01)
SDA = 1;
else
SDA = 0;
SCL = 1;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
SCL = 0;
}
SDA = 0;
if(Ack() == true)
return true;
else
return false;
}
unsigned char ReceiveOneByte(unsigned char Respond)
{
unsigned char i;
unsigned char result;
for(i=0;i<8;i++)
{
SCL = 1;
_nop_();_nop_();_nop_();_nop_();
if(SDA)
result = (result << 1) | 1;
else
result = (result << 1) | 0;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
SCL = 0;
}
SDA = 0;
if(Respond)
ReAck();
else
NoAck();
return result;
}