📄 com_i2c.c
字号:
#include "reg51.h"
#include "common.h"
#include "COM_I2C.h"
//----------------------------------------
// S_Condition for TWD protocol
//----------------------------------------
void StartCondition(void)
{
uCHAR ix;
uCHAR cTWtrytime=0;
//EA=0;
while(++cTWtrytime)
{
Set_SDA_High;
Set_SCL_High; /* make sure two line is release */
for(ix = 0; ix < TWD_LONG_TIME; ix++)
{ }; /* Delay 12us */
Set_SDA_Low;
for(ix = 0; ix < TWD_LONG_TIME; ix++)
{ }; /* Delay 12us */
if((SCL_High)&&(SDA_Low))
break;
}
for(ix = 0; ix < TWD_SHORT_TIME; ix++)
{ }; /* Delay 12us */
}
//----------------------------------------
// P_Condition for TWD protocol
//----------------------------------------
void StopCondition(void)
{
uCHAR ix;
uCHAR cTWtrytime=0;
Set_SDA_Low;
Set_SCL_High;
for(ix = 0; ix < TWD_SHORT_TIME; ix++)
{ }; /* delay 12us */
//while(SCL_Low && ++cTWtrytime)
//{ };
for(ix = 0; ix < TWD_SHORT_TIME; ix++)
{ }; /* delay 12us */
Set_SDA_High;
for(ix = 0; ix < TWD_SHORT_TIME; ix++)
{ }; /* delay 12us */
//EA=1;
}
//--------------------------------------------------
// Send_Byte
// Send a byte to master with a acknowledge bit
//--------------------------------------------------
uCHAR Send_Byte(uCHAR cData)
{
uCHAR ix, j, cAcknowledge;
uCHAR cTWtrytime=0;
cAcknowledge = 0;
for(ix = 0; ix < 8; ix++)
{
Set_SCL_Low;
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
if(cData&0x80)Set_SDA_High;
else Set_SDA_Low;
cData<<=1;
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
Set_SCL_High;
//while(SCL_Low && ++cTWtrytime)
//{ };
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
}
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
Set_SCL_Low;
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
Set_SDA_High; /* release data line for acknowledge */
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
Set_SCL_High; /* Send a clock for Acknowledge */
//while(SCL_Low)
//{ };
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
if(SDA_High)
cAcknowledge = 1; /* No Acknowledge */
Set_SCL_Low; /* Finish Acknoledge */
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
return(cAcknowledge);
}
//--------------------------------------------------
// Read_Byte
// Read a byte from master with a acknowledge bit
//--------------------------------------------------
uCHAR Read_Byte(uCHAR cNum)
{
uCHAR ix, j;
uCHAR cRetval=0;
for(ix=0;ix<8;ix++){
Set_SCL_High;
//while(SCL_Low){};
for(j=0;j<TWD_SHORT_TIME;j++){};
cRetval = (SDA_High)? (cRetval|(1<<(7-ix))):cRetval ; /* MSB First */
Set_SCL_Low;
}
if(cNum==1)
Set_SDA_High;
else
Set_SDA_Low;
//SDA = (cNum==1) 1:0;
Set_SCL_High;
//while(SCL_Low){};
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
Set_SCL_Low;
Set_SDA_High;
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
return cRetval;
}
//--------------------------------------------------
// Read Byte to Device
//--------------------------------------------------
uCHAR I2CReadByte(uCHAR cDevAddr, uCHAR cReg)
{
uCHAR cResult = 0;
/* write reg offset */
StartCondition();
if(Send_Byte(cDevAddr))
return 0; // Write address
if(Send_Byte(cReg))
return 0;
/* read data */
StartCondition();
if (Send_Byte(cDevAddr | 0x01)) // Read address
return 0;
cResult = Read_Byte(1);
StopCondition();
return cResult;
}
uCHAR I2CWriteByte(uCHAR cDevAddr, uCHAR cReg, uCHAR cData)
{
/* start condition */
StartCondition();
cDevAddr &= 0xFE; // Force WRITE address
/* send device ID and write data */
if(!Send_Byte(cDevAddr))
{
if(!Send_Byte(cReg))
{
if(!Send_Byte(cData))
{
StopCondition();
return(0); /* success */
}
}
}
StopCondition();
return(1);
}
//--------------------------------------------------
// 041901, Added for TWD Burst Write
// 1. PT8655X_Wr_Burst_A (Start + Slave_ID + Address)
// 2. PT8655X_Wr_Burst_D (Data)
// 3. PT8655X_Wr_Burst_P (stop)
//--------------------------------------------------
uCHAR twdWr_Burst_A(uCHAR cReg)
{
StartCondition();
if(!Send_Byte(TW803_P0))
{
if(!Send_Byte(cReg))
{
return(0); //
}
}
return(1); //
}
void twdWr_Burst_D(uCHAR cData)
{
uCHAR ix;
for(ix = 0; ix < 8; ix++)
{
Set_SCL2Low;
if(cData&0x80)Set_SDA2High;
else Set_SDA2Low;
cData<<=1;
Set_SCL2High;
}
Set_SCL2Low;
Set_SDA2High; /* release data line for acknowledge */
Set_SCL2High; /* Send a clock for Acknowledge */
Set_SCL2Low; /* Finish Acknoledge */
}
void twdWr_Burst_P(void)
{
StopCondition();
}
//twdWriteTable() is used for burst write for
//any I2C standar device
void OSDSetRamDataBuest(uWORD wdata) //add for new version
{
twdWr_Burst_D((uCHAR)(wdata & 0xff));
twdWr_Burst_D((uCHAR)(wdata>>8));
}
//--------------------------------------------------
// Write Multiple Bytes to Device
//--------------------------------------------------
void twdDelay(uWORD wLoops)
{
uWORD wTemp;
while (wLoops--) {
wTemp = 1000/6; // one loop below takes about 11 us
while (wTemp--);
}
}
// OSD config register Write
void OSD1CfgWr(uCHAR index,uCHAR dat)
{
I2CWriteByte(TW803_P0,OSD_CFG_INDEX,index);
I2CWriteByte(TW803_P0,OSD_CFG_DATA,dat);
}
void OSD1SetRamAddr(uWORD address)
{
I2CWriteByte(TW803_P0,OSD_RAM_AL,(uCHAR)(address & 0xff));
I2CWriteByte(TW803_P0,OSD_RAM_AH,(uCHAR)(address>>8));
}
void OSD1SetRamData(uWORD wdata)
{
I2CWriteByte(TW803_P0,OSD_RAM_DL,(uCHAR)(wdata & 0xff));
I2CWriteByte(TW803_P0,OSD_RAM_DH,(uCHAR)(wdata>>8));
}
// OSD2 config register Write
void OSD2CfgWr(uCHAR index,uCHAR dat)
{
I2CWriteByte(TW803_P0,OSD2_CFG_INDEX,index);
I2CWriteByte(TW803_P0,OSD2_CFG_DATA,dat);
}
void OSD2SetRamAddr(uWORD address)
{
I2CWriteByte(TW803_P0,OSD2_RAM_AL,(uCHAR)(address & 0xff));
I2CWriteByte(TW803_P0,OSD2_RAM_AH,(uCHAR)(address>>8));
}
void OSD2SetRamData(uWORD wdata)
{
I2CWriteByte(TW803_P0,OSD2_RAM_DL,(uCHAR)(wdata & 0xff));
I2CWriteByte(TW803_P0,OSD2_RAM_DH,(uCHAR)(wdata>>8));
}
void serial_Init(void)
{
}
/*
unsigned char c2n(unsigned char keyv)
{
keyv = keyv;
return 1;
}
unsigned char n2c(unsigned char hexv)
{
hexv = hexv;
return 1;
}
void putByte(unsigned char cByte)
{
cByte = cByte;
}
*/
/*
void putValue(unsigned char *sStr,unsigned char cByte,bit crlf)
{
uCHAR *str;
str = sStr;
cByte = cByte;
crlf = crlf;
}
unsigned char getByte(void)
{
return 1;
}
void putString(unsigned char *puts)
{
uCHAR *str;
str = puts;
}
*/
/*
void I2CRd(unsigned char host,unsigned char addr)
{
host = host;
addr = addr;
}
void I2CWt(unsigned char host,unsigned char addr,unsigned char valu)
{
host = host;
addr = addr;
valu = valu;
}
void serial_Debug(void)
{
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -