|
原贴地址: http://www.pudn.com/search_db.asp?keyword=AT24C1024
/*实验名称:读写AT24C02实验
功能:可对24c02单独写或读一个字节,也可对24c02连续写或读多个字节。
硬件设置:SW1:1-2 SW2: 1-2
*/
#include<reg51.h>
#include <intrins.h>
sbit SCL=P2^4;
sbit SDA=P2^5;
//****************************************************************
void Delay_ms_24C04(unsigned int t) //Delay t ms for 22.1184MHz crystal //MCU=STC12C5412
{
unsigned int i;
while(t--)
{
for(i=0;i<1100;i++);
}
}
//**起动24c02时序*******************
void Start()
{
SCL=1;
SDA=1;
SDA=0;
SCL=0;
}
//**停止24c02时序*******************
void Stop()
{
SDA=0;
SCL=1;
SDA=1;
}
//**检测24c02的响应信号****************
bit CxxACK()
{
bit c;
SDA=1;
SCL=1;
c=SDA;
SCL=0;
return c;
}
//**************************************
void MCUACK()
{
SDA=0;
SCL=1;
SCL=0;
SDA=1;
}
//**********************************************
void NoACK()
{
SDA=1;
SCL=1;
SCL=0;
}
//**往24c02发一8位数据*********************************
void SendChar(unsigned char dat)
{
unsigned char i=8;
do
{
SDA=(dat&0x80);
SCL=1;
SCL=0;
dat<<=1;
}while(--i!=0);
}
//**从24c02接收一8位数据******************************
unsigned char RecChar()
{
unsigned char i=8,dat;
do
{SCL=1;
dat=(dat<<1)|SDA;
SCL=0;
}while(--i!=0);
return dat;
}
//**往24c02写一个字节*************************************
void WriteByte_24C1024(bit A1,bit P0,unsigned int addr,unsigned char dat)
{
unsigned char c=0xa0;
if(A1) c=0xa4;
if(P0) c=c|0x02;
Start();
SendChar(c);
CxxACK();
SendChar(addr>>8);
CxxACK();
SendChar(addr);
CxxACK();
SendChar(dat);
CxxACK();
Stop();
Delay_ms_24C04(10);
}
//**往24c02写连续写多个字节,但最多不能超过16个字节************************
void WriteBytes_24C1024(bit A1,bit P0,unsigned int addr,unsigned char *buf,unsigned int bytes)
{
unsigned char c=0xa0;
if(A1) c=0xa4;
if(P0) c=c|0x02;
Start();
SendChar(c);
CxxACK();
SendChar(addr>>8);
CxxACK();
SendChar(addr);
CxxACK();
for(;bytes>0;bytes--)
{
SendChar(*buf++);
CxxACK();
}
Stop();
Delay_ms_24C04(10);
}
//**从24c02读一个字节*************************************
unsigned char ReadByte_24C1024(bit A1,bit P0,unsigned int addr)
{
unsigned char dat;
unsigned char c=0xa0;
if(A1) c=0xa4;
if(P0) c=c|0x02;
Start();
SendChar(c);
CxxACK();
SendChar(addr>>8);
CxxACK();
SendChar(addr);
CxxACK();
Start();
SendChar(c|1);
CxxACK();
dat=RecChar();
NoACK();
Stop();
return dat;
}
//**从24c02连续读多个字节,长度不限*******************************
void ReadBytes_24C1024(bit A1,bit P0,unsigned int addr,unsigned char *buf,unsigned int bytes)
{
unsigned char c=0xa0;
if(A1) c=0xa4;
if(P0) c=c|0x02;
Start();
SendChar(c);
CxxACK();
SendChar(addr>>8);
CxxACK();
SendChar(addr);
CxxACK();
Start();
SendChar(c|1);
CxxACK();
while(--bytes>0)
{*buf++=RecChar();
MCUACK();
}
*buf++=RecChar();
NoACK();
Stop();
}
//*******************************************************
/*
//**往24c02写连续写多个字节,但最多不能超过8个字节************************
void WriteBytes_24C02(unsigned char addr,unsigned char *buf,unsigned char bytes)
{
Start();
SendChar(0xa2);
CxxACK();
SendChar(addr);
CxxACK();
for(;bytes>0;bytes--)
{SendChar(*buf++);
CxxACK();
}
Stop();
Delay_ms_24C04(10);
}
//**从24c02连续读多个字节,长度<=8*******************************
void ReadBytes_24C02(unsigned char addr,unsigned char *buf,unsigned char bytes)
{ Start();
SendChar(0xa2);
CxxACK();
SendChar(addr);
CxxACK();
Start();
SendChar(0xa2);
CxxACK();
while(--bytes>0)
{*buf++=RecChar();
MCUACK();
}
*buf++=RecChar();
NoACK();
Stop();
}*/ |
|