📄 i2c.c
字号:
#pragma SFR
#pragma NOP
#include "i2c.h"
void I2C_Start(void);
void I2C_Wait(void);
void I2C_Stop(void);
uchar I2C_SendByte(uchar byte);
uchar I2C_ReceiveByte(void);
uchar I2C_Read(uchar slvaddr,uchar address,uchar length,uchar *databuf);
uchar I2C_Write(uchar slvaddr,uchar address,uchar length,uchar *databuf);
void SendAcknowledge(uchar ack);
void LCDI2C_Start(void);
void LCDI2C_Stop(void);
uchar LCDI2C_SendByte(uchar byte);
extern void Delay(uint n);
void I2C_Start(void)
{
SDAMODE=0;
I2C_Wait();
SDA=1;
I2C_Wait();
SCL=1;
I2C_Wait();
SDA=0;
I2C_Wait();
SCL=0;
}
void I2C_Stop(void)
{
SDAMODE=0;
I2C_Wait();
SDA=0;
I2C_Wait();
SCL=1;
I2C_Wait();
SDA=1;
}
void I2C_Wait(void)
{
NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
}
uchar I2C_SendByte(uchar byte)
{
uchar i,ack;
SDAMODE=0;
I2C_Wait();
for (i=0;i<=7;i++)
{
if(byte&0x80)
{
SDA=1;
}
else
{
SDA=0;
}
byte<<=1;
SCL=1;
I2C_Wait();
SCL=0;
I2C_Wait();
}
SDA=1;
SDAMODE=1;
I2C_Wait();
SCL=1;
I2C_Wait();
if(SDA==1)
{
ack=0;
}
else
{
ack=1;
}
SCL=0;
SDAMODE=0;
return(ack);
}
uchar I2C_ReceiveByte(void)
{
uchar i,byte=0;
SDAMODE=1;
I2C_Wait();
for(i=0;i<=7;i++)
{
SCL=1;
I2C_Wait();
byte<<=1;
if(SDA==1)
{
byte|=0x01;
}
SCL=0;
}
return(byte);
}
void SendAcknowledge(uchar ack)
{
SDAMODE=0;
I2C_Wait();
if(ack)
{
SDA=1;
}
else
{
SDA=0;
}
I2C_Wait();
SCL=1;
I2C_Wait();
SCL=0;
}
uchar I2C_Read(uchar slvaddr,uchar address,uchar length,uchar *databuf)
{
uchar i,j,ack;
for(j=0;j<3;j++)
{
I2C_Start();
ack=I2C_SendByte(slvaddr);
if(ack==0)
{
I2C_Stop();
continue;
}
I2C_Wait();
ack=I2C_SendByte(address);
if(ack==0)
{
I2C_Stop();
continue;
}
I2C_Wait();
ack=I2C_SendByte(slvaddr|0x01);
if(ack==0)
{
I2C_Stop();
continue;
}
NOP();
for(i=0;i<length;i++)
{
*(databuf+i)=I2C_ReceiveByte();
if(i<length-1)
{
SendAcknowledge(0);
}
else
{
SendAcknowledge(1);
}
}
I2C_Stop();
if(ack==1)
{
break;
}
}
return(ack);
}
uchar I2C_Write(uchar slvaddr,uchar address,uchar length,uchar *databuf)
{
uchar i,j,ack;
for(j=0;j<3;j++)
{
I2C_Start();
ack=I2C_SendByte(slvaddr);
if(ack==0)
{
I2C_Stop();continue;
}
I2C_Wait();
ack=I2C_SendByte(address);
if(ack==0)
{
I2C_Stop();continue;
}
for(i=0;i<length;i++)
{
ack=I2C_SendByte(*(databuf+i));
if(ack==0)
{
break;
}
}
if(ack==0)
{
I2C_Stop();continue;
}
I2C_Stop();
if(ack==1)
{
break;
}
}
return(ack);
}
uchar Read_24LC64(uint address,uchar length,uchar *databuf)
{
uchar i,j,ack;
for(j=0;j<3;j++)
{
I2C_Start();
ack=I2C_SendByte(EEPADR);
if(ack==0)
{
I2C_Stop();
continue;
}
I2C_Wait();
ack=I2C_SendByte((uchar)(address>>8));
if(ack==0)
{
I2C_Stop();
continue;
}
I2C_Wait();
ack=I2C_SendByte((uchar)(address&0x00FF));
if(ack==0)
{
I2C_Stop();
continue;
}
I2C_Wait();
ack=I2C_SendByte(EEPADR|0x01);
if(ack==0)
{
I2C_Stop();
continue;
}
NOP();
for(i=0;i<length;i++)
{
*(databuf+i)=I2C_ReceiveByte();
if(i<length-1)
{
SendAcknowledge(0);
}
else
{
SendAcknowledge(1);
}
}
I2C_Stop();
if(ack==1)
{
break;
}
}
return(ack);
}
uchar Write_24LC64(uint address,uchar length,uchar *databuf)
{
uchar i,j,ack;
WP=0;
for(j=0;j<3;j++)
{
I2C_Start();
ack=I2C_SendByte(EEPADR);
if(ack==0)
{
I2C_Stop();continue;
}
I2C_Wait();
ack=I2C_SendByte((uchar)(address>>8));
if(ack==0)
{
I2C_Stop();continue;
}
I2C_Wait();
ack=I2C_SendByte((uchar)(address&0x00FF));
if(ack==0)
{
I2C_Stop();continue;
}
I2C_Wait();
for(i=0;i<length;i++)
{
ack=I2C_SendByte(*(databuf+i));
if(ack==0)
{
break;
}
}
if(ack==0)
{
I2C_Stop();continue;
}
I2C_Stop();
if(ack==1)
{
break;
}
}
Delay(5);
WP=1;
return(ack);
}
uchar Read_24LC64_Check(uint address,uchar length,uchar *databuf)
{
uchar i,j,ack;
uchar checksum;
uchar tempchar;
for(j=0;j<3;j++)
{
checksum=0;
I2C_Start();
ack=I2C_SendByte(EEPADR);
if(ack==0)
{
I2C_Stop();
continue;
}
I2C_Wait();
ack=I2C_SendByte((uchar)(address>>8));
if(ack==0)
{
I2C_Stop();
continue;
}
I2C_Wait();
ack=I2C_SendByte((uchar)(address&0x00FF));
if(ack==0)
{
I2C_Stop();
continue;
}
I2C_Wait();
ack=I2C_SendByte(EEPADR|0x01);
if(ack==0)
{
I2C_Stop();
continue;
}
NOP();
for(i=0;i<length;i++)
{
*(databuf+i)=I2C_ReceiveByte();
checksum+=(*(databuf+i));
SendAcknowledge(0);
}
tempchar=I2C_ReceiveByte();
SendAcknowledge(1);
I2C_Stop();
if(checksum!=tempchar)
{
ack=0;
}
if(ack==1)
{
break;
}
}
return(ack);
}
uchar Write_24LC64_Check(uint address,uchar length,uchar *databuf)
{
uchar i,j,ack;
uchar checksum;
WP=0;
for(j=0;j<3;j++)
{
checksum=0;
I2C_Start();
ack=I2C_SendByte(EEPADR);
if(ack==0)
{
I2C_Stop();continue;
}
I2C_Wait();
ack=I2C_SendByte((uchar)(address>>8));
if(ack==0)
{
I2C_Stop();continue;
}
I2C_Wait();
ack=I2C_SendByte((uchar)(address&0x00FF));
if(ack==0)
{
I2C_Stop();continue;
}
I2C_Wait();
for(i=0;i<length;i++)
{
ack=I2C_SendByte(*(databuf+i));
if(ack==0)
{
break;
}
checksum+=(*(databuf+i));
}
ack=I2C_SendByte(checksum);
if(ack==0)
{
I2C_Stop();continue;
}
I2C_Stop();
if(ack==1)
{
break;
}
}
Delay(5);
WP=1;
return(ack);
}
/***********************************************************************/
void RX8025_Init(void)
{
uchar rx8025_buf[2];
rx8025_buf[0]=RX8025_CTRLBYTE1;
rx8025_buf[1]=RX8025_CTRLBYTE2;
RX8025_Write(0xe0,2,&rx8025_buf[0]);
}
uchar RX8025_Read(uchar address,uchar length,uchar *databuf)
{
uchar addr;
addr=address<<4;
if (I2C_Read(RX8025ADR,address,length,databuf))
{
return(1);
}
return(0);
}
uchar RX8025_Write(uchar address,uchar length,uchar *databuf)
{
uchar addr;
addr=address<<4;
if( I2C_Write(RX8025ADR,address,length,databuf) )
{
return(1);
}
return(0);
}
/***********************************************************************/
void LCDI2C_Start(void)
{
SDA1MODE=0;
I2C_Wait();
SDA1=1;
I2C_Wait();
SCL1=1;
I2C_Wait();
SDA1=0;
I2C_Wait();
SCL1=0;
}
void LCDI2C_Stop(void)
{
SDA1MODE=0;
I2C_Wait();
SDA1=0;
I2C_Wait();
SCL1=1;
I2C_Wait();
SDA1=1;
}
uchar LCDI2C_SendByte(uchar byte)
{
uchar i,ack;
SDA1MODE=0;
I2C_Wait();
for (i=0;i<=7;i++)
{
if(byte&0x80)
{
SDA1=1;
}
else
{
SDA1=0;
}
byte<<=1;
SCL1=1;
I2C_Wait();
SCL1=0;
I2C_Wait();
}
SDA1=1;
SDA1MODE=1;
I2C_Wait();
SCL1=1;
I2C_Wait();
if(SDA1==1)
{
ack=0;
}
else
{
ack=1;
}
SCL1=0;
SDA1MODE=0;
return(ack);
}
void BU9792_Init(void)
{
LCDI2C_Start();
LCDI2C_SendByte(BU9792ADR);
I2C_Wait();
LCDI2C_SendByte(BU9792_ICSET|0x80);
I2C_Wait();
LCDI2C_SendByte(BU9792_DISCTL|0x80);
I2C_Wait();
LCDI2C_SendByte(BU9792_BLKCTL|0x80);
I2C_Wait();
LCDI2C_SendByte(BU9792_APCTL|0x80);
I2C_Wait();
LCDI2C_SendByte(BU9792_MODESET);
I2C_Wait();
LCDI2C_Stop();
}
void BU9792_DisplayAll(uchar byte)
{
uchar test_num;
LCDI2C_Start();
LCDI2C_SendByte(BU9792ADR);
I2C_Wait();
LCDI2C_SendByte(0x00);
I2C_Wait();
test_num=16;
while(test_num--)
{
LCDI2C_SendByte(byte);
}
LCDI2C_Stop();
}
void BU9792_Display(uchar address,uchar length,uchar *databuf)
{
uchar i,ack;
LCDI2C_Start();
LCDI2C_SendByte(BU9792ADR);
I2C_Wait();
LCDI2C_SendByte(address);
I2C_Wait();
for(i=0;i<length;i++)
{
LCDI2C_SendByte(*(databuf+i));
}
I2C_Wait();
LCDI2C_Stop();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -