I2C總線驅(qū)動(dòng)(C51語言源程序)
/*****************停止I2C總線****************/
void I2CStop(void)
{
SCL=0;
SDA=0;
SCL=1;
_nop_();
_nop_();
_nop_();
SDA=1;
_nop_();
_nop_();
_nop_();
SCL=0;
}
/**************檢查應(yīng)答位*******************/
bit I2CRecAck(void)
{
SCL=0;
SDA=1;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
CY=SDA; //因?yàn)榉祷刂悼偸欠旁贑Y中的
SCL=0;
return(CY);
}
/***************對(duì)I2C總線產(chǎn)生應(yīng)答*******************/
void I2CACK(void)
{
SDA=0;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
_nop_();
SDA=1;
}
/*****************不對(duì)I2C總線產(chǎn)生應(yīng)答***************/
void I2CNoAck(void)
{
SDA=1;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
}
/*******************向I2C總線寫數(shù)據(jù)*********************/
void I2CSendByte(unsigned char sendbyte)
{
unsigned char data j=8;
for(;j>0;j--)
{
SCL=0;
sendbyte=1; //無論C51怎樣實(shí)現(xiàn)這個(gè)操作,始終會(huì)使CY=sendbyte^7;
SDA=CY;
SCL=1;
}
SCL=0;
}
/**********************從I2C總線上讀數(shù)據(jù)子程序**********/
unsigned char I2CReceiveByte(void)
{
register receivebyte,i=8;
SCL=0;
while(i--)
{
SCL=1;
receivebyte=(receivebyte1)|SDA;
SCL=0;
}
return(receivebyte);
}
/***************一個(gè)簡(jiǎn)單延時(shí)程序************************/
void Delay(unsigned char DelayCount)
{
while(DelayCount--);
}
void main()
{
// unsigned int i;
//unsigned char j[32];
unsigned char *pa,*pb,temp=0;
unsigned char a[8]={5,6,7,8,9,10,11,12};
unsigned char b[8]={0,0,0,0,0,0,0,0};
pa=a[0];
pb=b[0];
P2_7=0;
RW24XX(pa,8,0x0,writeI2C,M2401);
RW24XX(pa,8,0x20,writeI2C,M2401);
RW24XX(pb,8,0x20,readI2C,M2401);
RW24XX(pb,8,0x30,writeI2C,M2401);
// RW24XX(unsigned char *DataBuff,unsigned char ByteQuantity,unsigned int Address,
// unsigned char ControlByte,enum eepromtype EepromType)
while(1)
{
P2_7=1;
}
}
評(píng)論