#include<reg52.h>
#include <intrins.h>
#define ucr8 unsigned char
#define AT24c16 0xa0
sbit SDA = P2^0;
sbit SCL = P2^1;
/***************************************************************
函數(shù)名稱:delay5us1;
函數(shù)類型:無;
函數(shù)參數(shù):無;
子函數(shù)順序:1;
函數(shù)功能:產(chǎn)生大約5US的延遲;
****************************************************************/
void delay5us1(void) //誤差 -0.659722222222us
{
_nop_(); //if Keil,require use intrins.h
_nop_(); //if Keil,require use intrins.h
}
/*_____________________________________________________子函數(shù)結(jié)束*/
/*______________________________________________________________*/
/***************************************************************
函數(shù)名稱:IICStart2;
函數(shù)類型:無;
函數(shù)參數(shù):無;
子函數(shù)順序:2;
函數(shù)功能:啟動IIC總線;
****************************************************************/
void IICStart2(void)
{
SDA = 1;
SCL = 1;
delay5us1();
SDA = 0;
delay5us1();
}
/*_____________________________________________________子函數(shù)結(jié)束*/
/*______________________________________________________________*/
/***************************************************************
函數(shù)名稱:IICStop3;
函數(shù)類型:無;
函數(shù)參數(shù):無;
子函數(shù)順序:3;
函數(shù)功能:結(jié)束IIC總線;
****************************************************************/
void IICStop3(void)
{
SDA = 0;
SCL = 0;
delay5us1();
SCL = 1;
SDA = 1;
}
/*_____________________________________________________子函數(shù)結(jié)束*/
/*______________________________________________________________*/
/***************************************************************
函數(shù)名稱:IICSend4;
函數(shù)類型:無;
函數(shù)參數(shù):unsigned char型;
子函數(shù)順序:4;
函數(shù)功能:向IIC發(fā)送數(shù)據(jù)傳輸信號,并傳輸數(shù)據(jù)或者地址;
子函數(shù)內(nèi)變量:i、dat、Judge;
****************************************************************/
void IICSend4(ucr8 dat)
{
ucr8 i,Judge;
Judge = 0x80;
for(i=0;i<8;i++)
{
SCL = 0;
delay5us1();
if( dat & Judge == 0) {SDA = 0;}
else {SDA = 1;}
Judge >>= 1;
delay5us1();
SCL = 1;
}
}
/*_____________________________________________________子函數(shù)結(jié)束*/
/*______________________________________________________________*/
/***************************************************************
函數(shù)名稱:IICReceive5;
函數(shù)類型:unsigned char型;
函數(shù)參數(shù):無;
子函數(shù)順序:5;
函數(shù)功能:向IIC接收數(shù)據(jù),并返回應(yīng)答信號;
子函數(shù)內(nèi)變量:i、dat、Judge;
****************************************************************/
ucr8 IICReceive5()
{
ucr8 i,dat;
dat = 0;
for(i=0;i<8;i++)
{
SCL = 0;
SDA = 1;
delay5us1();
SCL = 1;
delay5us1();
dat <<= 1;
if(SDA == 1) {dat|= 0x01;}
delay5us1();
}
SCL = 0;
SDA = 1;
delay5us1();
SCL = 1;
return dat;
}
/*_____________________________________________________子函數(shù)結(jié)束*/
/*______________________________________________________________*/
void main(void)
{
IICStart2();
IICSend4(AT24c16);
IICSend4(0);
IICSend4();
IICStop3();
IICStart2();
IICSend4(AT24c16);
IICSend4(0);
IICStart2();
IICSend4(AT24c16+1);
P0 = IICReceive5();
IICStop3();
while(1);
}
幫忙看下程序 EEPROM數(shù)值總是讀不出來
|