c51寫的24c02的讀寫程序
#include
//對24C02的讀、寫
//externvoidDelayMs(unsignedint);
//externvoidRead24c02(unsignedchar*RamAddress,unsignedcharRomAddress,unsignedcharbytes);
//externvoidWrite24c02(unsignedchar*RamAddress,unsignedcharRomAddress,unsignedcharbytes);
/***************************************************************************/
#defineWriteDeviceAddress0xa0
#defineReadDviceAddress0xa1
/***************************************************************************/
sbitSCL=P1^4;
sbitSDA=P1^3;
sbitDOG=P3^7;
/***************************************************************************/
voidDelayMs(unsignedintnumber){
unsignedchartemp;
for(;number!=0;number--,DOG=!DOG){
for(temp=112;temp!=0;temp--){
}
}
}
/***************************************************************************/
voidStart(){
SDA=1;
SCL=1;
SDA=0;
SCL=0;
}
/***************************************************************************/
voidStop(){
SCL=0;
SDA=0;
SCL=1;
SDA=1;
}
/***************************************************************************/
voidAck(){
SDA=0;
SCL=1;
SCL=0;
SDA=1;
}
/***************************************************************************/
voidNoAck(){
SDA=1;
SCL=1;
SCL=0;
}
/***************************************************************************/
bitTestAck(){
bitErrorBit;
SDA=1;
SCL=1;
ErrorBit=SDA;
SCL=0;
return(ErrorBit);
}
/***************************************************************************/
bitWrite8Bit(unsignedcharinput){
unsignedchartemp;
for(temp=8;temp!=0;temp--){
SDA=(bit)(input&0x80);
SCL=1;
SCL=0;
input=input<<1;
}
}
/***************************************************************************/
voidWrite24c02(unsignedchar*Wdata,unsignedcharRomAddress,unsignedcharnumber){
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
for(;number!=0;number--){
Write8Bit(*Wdata);
TestAck();
Wdata++;
}
Stop();
DelayMs(10);
}
/***************************************************************************/
unsignedcharRead8Bit(){
unsignedchartemp,rbyte=0;
for(temp=8;temp!=0;temp--){
SCL=1;
rbyte=rbyte<<1;
rbyte=rbyte|((unsignedchar)(SDA));
SCL=0;
}
return(rbyte);
}
/***************************************************************************/
voidRead24c02(unsignedchar*RamAddress,unsignedcharRomAddress,unsignedcharbytes){
unsignedchartemp,rbyte;
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
Start();
Write8Bit(ReadDviceAddress);
TestAck();
while(bytes!=1){
*RamAddress=Read8Bit();
Ack();
RamAddress++;
bytes--;
}
*RamAddress=Read8Bit();
NoAck();
Stop();
}