#include <reg52.h>
#include <intrins.h>
/***************for 24c02************************************************************/
#define WriteDeviceAddress 0xa0 //第6与7位控制数据的段地址,第8位控制读写,1至4位固定为1010,5位为器件代码
#define ReadDviceAddress 0xa1
#define NOP5() _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
/***************************************************************************/
sbit SCL=P2^4;
sbit SDA=P2^5;
sbit led = P2^0;
unsigned char darray[3] ;
unsigned char paddress;
void int_0(); //外部中断0
void Start() {
SDA=1;
NOP5();
SCL=1;
NOP5();
SDA=0;
NOP5();
SCL=0;
NOP5();
}
/***************************************************************************/
void Stop() {
SDA=0;
NOP5();
SCL=1;
NOP5();
SDA=1;
NOP5();
SCL=0;
NOP5();
}
/***************************************************************************/
void Ack() {
SDA=0;
NOP5();
SCL=1;
NOP5();
SCL=0;
NOP5();
SDA=1;
}
/***************************************************************************/
void NoAck()
{
SDA=1;
NOP5();
SCL=1;
NOP5();
SCL=0;
NOP5();
}
/***************************************************************************/
bit TestAck(void) {
bit ErrorBit;
SDA=1;
NOP5();
SCL=1;
NOP5();
ErrorBit=SDA;
NOP5();
SCL=0;
return(ErrorBit);
}
/***************************************************************************/
void Write8Bit(unsigned char input) {
unsigned char temp;
for(temp=8;temp!=0;temp--) {
SDA=(bit)(input&0x80);
NOP5();
SCL=1;
NOP5();
SCL=0;
NOP5();
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();
//delay_ms(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) {
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
Start();
Write8Bit(ReadDviceAddress);
TestAck();
while(bytes!=1) {
*RamAddress=Read8Bit();
Ack();
RamAddress++;
bytes--;