#include<reg52.h>
#include<intrins.h>
sbit SCL = P3^6;
sbit SDA = P3^7;
unsigned char dTube[10]={0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
void delay_nop_(unsigned int t)
{
while(t-->0)
_nop_();
}
void EEPROM_init()
{
SDA=1;
delay_nop_(3);
SCL=1;
delay_nop_(3);
}
void EEPROM_start()
{
SDA=1;
delay_nop_(3);
SCL=1;
delay_nop_(3);
SDA=0;
delay_nop_(3);
}
void EEPROM_stop()
{
SDA=0;
delay_nop_(3);
SCL=1;
delay_nop_(3);
SDA=1;
delay_nop_(3);
}
void ack_in()
{
unsigned char i=0;
SCL=1;
delay_nop_(3);
while((i++<250)&&(SDA==1));
SCL=0;
delay_nop_(3);
}
void ack_out(bit signal)
{
SDA=signal;
delay_nop_(3);
SCL=1;
delay_nop_(3);
SCL=0;
delay_nop_(3);
}
void EEPROM_write_byte(unsigned char c)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCL=0;
delay_nop_(3);
c=c<<1;SDA=CY;
delay_nop_(3);
SCL=1;
delay_nop_(3);
}
SCL=0;
delay_nop_(3);
SDA=1;
delay_nop_(3);
}
void EEPROM_write(unsigned char address,unsigned char content[],unsigned char num)
{
char i=0;
while(i<num)
{
EEPROM_start();
EEPROM_write_byte(0xa0);
ack_in();
EEPROM_write_byte(address+i);
ack_in();
EEPROM_write_byte(content[i++]);
ack_in();
EEPROM_stop();
delay_nop_(10);
}
}
unsigned char read_byte()
{
unsigned char i,temp;
for(i=0;i<8;i++)
{
SCL=1;
delay_nop_(3);
temp=(temp<<1)|SDA;
SCL=0;
delay_nop_(3);
}
return temp;
}
void EEPROM_read(unsigned char address, unsigned char content[], unsigned char num)
{
unsigned char i=0;
EEPROM_start();
EEPROM_write_byte(0xa0);
ack_in();
EEPROM_write_byte(address);
ack_in();
while(i<num)
{
EEPROM_start();
EEPROM_write_byte(0xa1);
ack_in();
content[i++]=read_byte();
ack_out(1);
EEPROM_stop();
delay_nop_(10);
}
}
main()
{
unsigned char x=dTube[10];
EEPROM_init();
EEPROM_write(80,dTube,8);
delay_nop_(200);
EEPROM_read(80,&x,8);
P2=x;
while(1);
}