📄 i2c.c
字号:
#include <Reg52.h>
#include "I2C.h"
void I2CDelay(unsigned char _I2CDelayValue)
{
unsigned char cnt;
for(cnt=0;cnt<_I2CDelayValue;cnt++)
;
}
////////////////////////////////////////////////////////////////////////////////////////
void I2CSCLH(void)
{
SCL=1;
}
////////////////////////////////////////////////////////////////////////////////////////
void I2CSCLL(void)
{
SCL=0;
}
////////////////////////////////////////////////////////////////////////////////////////
void I2CSDAH(void)
{
SDA=1;
}
////////////////////////////////////////////////////////////////////////////////////////
void I2CSDAL(void)
{
SDA=0;
}
///////////////////////////////////////////////////////////////////////////////////////
void I2CSendEnd(void)
{
I2CSCLL();
I2CSDAL();
I2CSCLH();
I2CSDAH();
}
////////////////////////////////////////////////////////////////////////////////////////
void I2CSendStart(void)
{
I2CSDAH();
I2CSCLH();
I2CSDAL();
I2CSCLL();
}
///////////////////////////////////////////////////////////////////////////////////////
unsigned char I2CGetByte(void)
{
unsigned char cnt,value;
//I2CSDAH();
for(cnt=0;cnt<8;cnt++)
{
value=(value<<1);
I2CSCLH();
if(SDA==1)
value=(value|0x01);
I2CSCLL();
}
I2CSDAH();
I2CSCLH();
I2CSCLL();
I2CSDAL();
return(value);
}
///////////////////////////////////////////////////////////////////////////////////////
unsigned char I2CSendByte(unsigned char value)
{
unsigned char Itercnt=0;
unsigned char TimeoutCnt=0;
I2CSCLL();
for(Itercnt=0;Itercnt<8;Itercnt++)
{
if((value&0x80)==0x80)
{
I2CSDAH();
}
else
{
I2CSDAL();
}
I2CSCLH();
I2CSCLL();
value=(value<<1);
}
I2CSDAH();
I2CSCLH();
while((SDA!=0)&&(TimeoutCnt!=0xfe))
{
I2CDelay(3);
TimeoutCnt++;
}
I2CSCLL();
if (TimeoutCnt==0xfe)
return(0);
else
return(1); // This is ack
}
///////////////////////////////////////////////////////////////////////////////////////
unsigned char I2CRead(unsigned char maddr,unsigned char saddr)
{
unsigned char value;//,Pre_IE;
I2CSendStart();
if(I2CSendByte(maddr))
{
if(I2CSendByte(saddr))
{
I2CSendEnd();
I2CSendStart();
if(I2CSendByte((maddr|0x01)))
{
value=I2CGetByte();
}
}
}
I2CSendEnd();
return(value);
}
///////////////////////////////////////////////////////////////////////////////////////
unsigned char I2CWrite(unsigned char maddr,unsigned char saddr,unsigned char value)
{
unsigned char success=0;
I2CSendStart();
if (I2CSendByte(maddr))
{
if (I2CSendByte(saddr))
{
if (I2CSendByte(value))
success=1;
}
}
I2CSendEnd();
if (success)
return(1);
else
return(0);
}
///////////////////////////////////////////////////////////////////////////////////////
void I2CMultiRead(unsigned char maddr,unsigned char saddr,unsigned char number,unsigned char *ptr)
{
unsigned char i;
I2CSendStart();
if(I2CSendByte(maddr))
{
if(I2CSendByte(saddr))
{
for(i=0;i<number;i++,ptr++)
{
//I2CSendEnd();
I2CSendStart();
if(I2CSendByte((maddr|0x01)))
{
*ptr=I2CGetByte();
// I2CSendEnd();
}
}
}
}
I2CSendEnd();
}
/*
//////////////////////////////////////////////////////////////////////////////////////////
void I2CKeepWrite(unsigned char maddr,unsigned char saddr,unsigned char number,unsigned char *ptr)
{
unsigned char i;
I2CSendStart();
if (I2CSendByte(maddr))
{
if(I2CSendByte(saddr))
{
for(i=0;i<number;i++,ptr++)
{
if (I2CSendByte(*ptr))
;
//I2CSendEnd();
}
}
}
I2CSendEnd();
}
*/
/*
unsigned char OSDMultiWrite(unsigned char maddr,unsigned char saddr,unsigned char number,unsigned char *value)
{
unsigned char Pre_IE,i;
unsigned char success=1;
Pre_IE=IE;
IE=0xc1;
// I2CSendStart();
SDA=0;
SCL=0;
if (I2CSendByte(maddr))
{
if (I2CSendByte(saddr))
{
for (i=0;i<(unsigned int)number;i++)
{
if (I2CSendByte(*(value)))
;
else
success=0;
}
}
}
else
success=0;
// I2CSendEnd();
SCL=1;
SDA=1;
IE=Pre_IE;
if (success)
return(1);
else
return(0);
}
*/
unsigned char I2CMultiWrite(unsigned char maddr,unsigned char saddr,unsigned int number,unsigned char *value)
{
unsigned int i;// Pre_IE;
unsigned char success=1;
I2CSendStart();
if (I2CSendByte(maddr))
{
if (I2CSendByte(saddr))
{
for (i=0;i<number;i++)
{
if (I2CSendByte(*(value+i)))
;
else
success=0;
}
}
}
else
success=0;
I2CSendEnd();
// IE=Pre_IE;
if (success)
return(1);
else
return(0);
}
/*
///////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned char I2CMultiAttribute(unsigned char maddr,unsigned char saddr,unsigned char number,unsigned char value)
{
unsigned char i;// Pre_IE;
unsigned char success=1;
// Pre_IE=IE;
// IE=0xc1;
I2CSendStart();
if (I2CSendByte(maddr))
{
if (I2CSendByte(saddr))
{
for (i=0;i<number;i++)
{
if (I2CSendByte(value))
;
else
success=0;
}
}
}
else
success=0;
I2CSendEnd();
// IE=Pre_IE;
if (success)
return(1);
else
return(0);
}
*/
/*
bit I2CTVMultiWrite(unsigned char maddr,unsigned char number,unsigned char *value)
{
unsigned char Pre_IE,i;
bit success=1;
Pre_IE=IE;
IE=0xc1;
// I2CSendStart();
SDA=0;
SCL=0;
if (I2CSendByte(maddr))
{
for (i=0;i<number;i++)
{
if (I2CSendByte(*(value+i)))
;
else
success=0;
}
}
else
success=0;
// I2CSendEnd();
SCL=1;
SDA=1;
IE=Pre_IE;
if (success)
return(1);
else
return(0);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -