#include <reg51.h>
// 对24C02的读、写
// extern void DelayMs(unsigned int);
// extern void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
// extern void Write24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
/***************************************************************************/
#define WriteDeviceAddress 0xa0
#define ReadDviceAddress 0xa1
/***************************************************************************/
sbit SCL=P1^4;
sbit SDA=P1^3;
sbit DOG=P3^7;
/***************************************************************************/
void DelayMs(unsigned int number) {
unsigned char temp;
for(;number!=0;number--,DOG=!DOG) {
for(temp=112;temp!=0;temp--) {
}
}
}
/***************************************************************************/
void Start() {
SDA=1;
SCL=1;
SDA=0;
SCL=0;
}
/***************************************************************************/
void Stop() {
SCL=0;
SDA=0;
SCL=1;
SDA=1;
}
/***************************************************************************/
void Ack() {
SDA=0;
SCL=1;
SCL=0;
SDA=1;
}
/***************************************************************************/
void NoAck() {
SDA=1;
SCL=1;
SCL=0;
}
/***************************************************************************/
bit TestAck() {
bit ErrorBit;
SDA=1;
SCL=1;
ErrorBit=SDA;
SCL=0;
return(ErrorBit);
}
/***************************************************************************/
bit Write8Bit(unsigned char input) {
unsigned char temp;
for(temp=8;temp!=0;temp--) {
SDA=(bit)(input&0x80);
SCL=1;
SCL=0;
input=input<<1;
}
}
/***************************************************************************/
void Write24c02(unsigned char *Wdata,unsigned char RomAddress,unsigned char number) {
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
for(;number!=0;number--) {
Write8Bit(*Wdata);
TestAck();
Wdata++;
}
Stop();
DelayMs(10);
}
/***************************************************************************/
unsigned char Read8Bit() {
unsigned char temp,rbyte=0;
for(temp=8;temp!=0;temp--) {
SCL=1;
rbyte=rbyte<<1;
rbyte=rbyte|((unsigned char)(SDA));
SCL=0;
}
return(rbyte);
}
/***************************************************************************/
void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes) {
unsigned char temp,rbyte;
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
Start();
Write8Bit(ReadDviceAddress);
TestAck();
while(bytes!=1) {
*RamAddress=Read8Bit();
Ack();
RamAddress++;
bytes--;
}
*RamAddress=Read8Bit();
NoAck();
Stop();
}
/***************************************************************************/
1楼
0
0
回复
// 对24C02的读、写
// extern void DelayMs(unsigned int);
// extern void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
// extern void Write24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes);
/***************************************************************************/
#define WriteDeviceAddress 0xa0
#define ReadDviceAddress 0xa1
/***************************************************************************/
sbit SCL=P1^4;
sbit SDA=P1^3;
sbit DOG=P3^7;
/***************************************************************************/
void DelayMs(unsigned int number) {
unsigned char temp;
for(;number!=0;number--,DOG=!DOG) {
for(temp=112;temp!=0;temp--) {
}
}
}
/***************************************************************************/
void Start() {
SDA=1;
SCL=1;
SDA=0;
SCL=0;
}
/***************************************************************************/
void Stop() {
SCL=0;
SDA=0;
SCL=1;
SDA=1;
}
/***************************************************************************/
void Ack() {
SDA=0;
SCL=1;
SCL=0;
SDA=1;
}
/***************************************************************************/
void NoAck() {
SDA=1;
SCL=1;
SCL=0;
}
/***************************************************************************/
bit TestAck() {
bit ErrorBit;
SDA=1;
SCL=1;
ErrorBit=SDA;
SCL=0;
return(ErrorBit);
}
/***************************************************************************/
bit Write8Bit(unsigned char input) {
unsigned char temp;
for(temp=8;temp!=0;temp--) {
SDA=(bit)(input&0x80);
SCL=1;
SCL=0;
input=input<<1;
}
}
/***************************************************************************/
void Write24c02(unsigned char *Wdata,unsigned char RomAddress,unsigned char number) {
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
for(;number!=0;number--) {
Write8Bit(*Wdata);
TestAck();
Wdata++;
}
Stop();
DelayMs(10);
}
/***************************************************************************/
unsigned char Read8Bit() {
unsigned char temp,rbyte=0;
for(temp=8;temp!=0;temp--) {
SCL=1;
rbyte=rbyte<<1;
rbyte=rbyte|((unsigned char)(SDA));
SCL=0;
}
return(rbyte);
}
/***************************************************************************/
void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes) {
unsigned char temp,rbyte;
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
Start();
Write8Bit(ReadDviceAddress);
TestAck();
while(bytes!=1) {
*RamAddress=Read8Bit();
Ack();
RamAddress++;
bytes--;
}
*RamAddress=Read8Bit();
NoAck();
Stop();
}
/***************************************************************************/