I2C實時時鐘/日歷芯片在8051系統(tǒng)中的應(yīng)用
void I_stop(void) //I2C總線停止條件子程序
{sda=0;
;
scl=1;
delay();
sda=1;
delay();
}
bit I_send(byte I_data) //字節(jié)數(shù)據(jù)傳送子程序
{data byte i;
for(i=0,i8;i++)
{sda-(bit)(I_data0x80);
I_data=I_data1;
;
scl=1;
delay();
scl=0;
}
;;
sda=1; ;; //ready for receiving ACK bit
scl=1; ;; //start receiving ack bit
flag=0;
if(sda= =0)flag=0;
else flag=1; //return(~I_clock());
scl=0;
return(flag);
}
byte I_receive(void) //字節(jié)數(shù)據(jù)接收子程序
{ data byte i;
byte I_data=0;
sda=1;
for(i=0;I8;i++)
{ I_data*=2;
;
scl=0;
delay();
scl=1; ;;
if(sda= =1)I_data++;
;;
}
scl=0; ;;;
sda=0;
if(flag1= =0){;;scl=1;delay();scl=0;} //not last receic_byte ACK
else{sda=1; ;;scl=1;delay();scl=0;flag1=0;} //the last receive_byte ~ACK
return(I_data);
}
main() //主程序
{data byte i;
rom_sed[0]=0x00; rom_sed[1]=0x00;
rom_sed[2]=0x00; rom_sed[3]=0x55;
rom_sed[4]=0x23; rom_sed[5]=0x31;
rom_sed[6]=0x06; rom_sed[7]=0x92;
rom_sed[8]=0x99;
for(i=0;i255;i++)delay();
I_start();
if(~I_send(rom_sed[i]));
else;
}
I_stop();
}
else;
}
else;
start: I_start();
if(~I_send(0xa2)) //pcf_write address
{if(~I_send(0x02)) //pcf_status register address
{I_start();
if(~I_send(0xa3)) //write status register
{for(i=0;i7;i++)
{if(i= =6)flag1=1;
else flag1=0;
rom_rec[i]=I_receive();
switch(i)
{case 1:rom_rec[i]=rom_rec[i]0x7f;break;
ease2:
case3:rom_rec[i]=rom_rec[i]0x3f;break;
case4:rom_rec[i]=rom_rec[i]0x07;break;
case5:rom_rec[i]=rom_rec[i]0x9f;brealk;default:break;
}
}
I_stop()
}
}
}
goto start;
}
評論