sbit SDA =P1^1;
sbit SCL =P1^4;
sbit WP =P1^7;
sbit light =P1^0;
sbit power =P1^6;
#define uchar unsigned char
#define uint unsigned int
void iic_delay10ms();
void iic_delay(void);
void iic_start(void);
void iic_stop(void);
void iic_ack(void);
void read_byte(void);
void send_byte(void);
void initial(void);
void ok(void);
void error(void);
void read_eeprom(void);
void write_eeprom(void);
void init(void);
uchar ack_flag,send_data,receive_data[32];
uchar rw,type,device_addr,addr[3],num;
void main(void)
{
initial();
power =0;
light =0;
while(1)
{
ES =1;
rw =0x55;
WP =0;
while(rw ==0x55);
ES =0;
switch(type)
{
case 0x01:
case 0x02:
case 0x04:
case 0x08:
case 0x16:
type =0x02;
addr[1] |=0xa0;
device_addr <<=1;
addr[1] |=device_addr;
break;
default:
type =0x03;
addr[2] |=0xa0;
device_addr <<=1;
addr[2] |=device_addr;
}
if(rw ==0x01) //读EEPROM
{
read_eeprom();
}
else if(rw ==0x00) //写EEPROM
{
write_eeprom();
}
else //error command
{
error();
ACC =0x01;
SBUF =ACC;
while(!TI);
TI =0;
}
}
}
void read_eeprom(void)
{
uchar i;
WP =1;
iic_start();
for(i=1;i<=type;i++)
{
send_data =addr[type-i];
send_byte();
iic_ack();
if(ack_flag ==0x01)
break;
}
if(ack_flag ==0x00)
{
iic_start();
addr[type-1] |=0x01;
send_data =addr[type-1];
send_byte();
iic_ack();
if(ack_flag ==0x00)
{
for(i=0;i<num;i++)
{
read_byte();
SBUF =send_data;
while(!TI);
TI =0;
SDA =0;
SCL =1;
iic_delay();
SCL =0;
}
iic_stop();
rw =0x55;
}
else
{
error();
ACC =0x03;
SBUF =ACC;
while(!TI);
TI =0;
}
}
else
{
error();
ACC =0x02;
SBUF =ACC;
while(!TI);
TI =0;
}
}
void write_eeprom(void)
{
uchar i;
WP =0;
iic_start();
for(i=1;i<=type;i++)
{
send_data =addr[type-i];
send_byte();
iic_ack();
if(ack_flag ==0x01)
break;
}
if(ack_flag ==0x00)
{
for(i=0;i<num;i++)
{
send_data =receive_data;
send_byte();
iic_ack();
iic_delay10ms();
}
iic_stop();
rw =0x55;
}
else
{
error();
ACC =0x04;
SBUF =ACC;
while(!TI);
TI =0;
}
}
void read_byte(void)
{
uchar i,temp=0x80;
send_data =0x00;
SCL =0;
for(i=0;i<8;i++)
{
SDA =1;
SCL =1;
if(SDA)
send_data |=temp;
temp/=2;
SCL =0;
iic_delay();
}
}
void send_byte(void)
{
uchar i,temp=0x80;
for(i=0;i<8;i++)
{
if((send_data&temp)==temp)
{
SDA =1; //sda=1
}
else
{
SDA =0; //sda=0
}
temp /=2;
iic_delay();
SCL =1;
iic_delay();
SCL =0;
iic_delay();
}
}
void iic_start(void)
{
SDA =1;
SCL =1;
WP =0;
iic_delay();
SDA =0;
iic_delay();
SCL =0;
}
void iic_stop(void)
{
SDA =0;
SCL =1;
iic_delay();
SDA =1;
//WP =1;
}
void iic_ack(void)
{
ack_flag =0x00; //ack_flag等于0x00时有应答
SDA =1;
iic_delay();
SCL =1;
iic_delay();
if(SDA)
{
ack_flag =0x01;
}
SCL =0;
}
void iic_delay(void)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
void iic_delay10ms(void)
{
#pragma ASM
mov r6,#200
mov r7,#10
delay:
nop
nop
nop
djnz r6,delay
mov r6,#200
djnz r7,delay
#pragma ENDASM
}
void initial(void)
{
TMOD =0x21;
TH1 =0xfd;
TL1 =0xfd;
TH0 =0xfd;
TL0 =0xfd;
SCON =0x50;
TR1 =1;
EA =1;
ES =1;
ET0 =1;
PT0 =1;
}
void ok(void)
{
ACC =0x55;
SBUF =ACC;
while(!TI);
TI =0;
ACC =0xaa;
SBUF =ACC;
while(!TI);
TI =0;
}
void error(void)
{
ACC =0xaa;
SBUF =ACC;
while(!TI);
TI =0;
ACC =0x55;
SBUF =ACC;
while(!TI);
TI =0;
rw =0x55;
}
void es_int(void)interrupt 4
{
uchar i;
RI =0;
type =SBUF; //型号
while(!RI);
RI =0;
device_addr =SBUF; //器件地址
for(i=0;i<3;i++)
{
while(!RI);
RI =0;
addr[2-i] =SBUF; //地址高、中、低位字节
}
while(!RI);
RI =0;
num =SBUF; //要操作的字节数
while(!RI);
RI =0;
rw =SBUF;
if(rw ==1) //接收到要读的命令
{
ok();
}
else //接收到要写的命令
{
for(i=0;i<num;i++)
{
while(!RI);
RI =0;
receive_data=SBUF;
}
ok();
}
//else
//{
// error();
//}
}
用户3768741 2016-5-16 04:20