?? iic.c
字號:
/***************************************************************************
Project : Car two ting dvd System
Compiler : ST7 HiCross C (HiWARE)
Module : autos.c
Version : V 1.0
Created : 24 11, 2005
Author : tomi / SA
Description
- I2c.
Modified
- tomi
***************************************************************************/
#include "r8c2a.h"
#include "tw803_regs.h"
#include "IIC.h"
#include "OSD2_define.h"
void StartCondition(void);
void StopCondition(void);
void I2Cm_nAck(void);
void I2Cm_Ack(void);
unsigned char Send_Byte(unsigned char cData);
unsigned char Read_Byte(void);
unsigned char I2CReadByte(unsigned char cDevAddr, unsigned char cReg);
unsigned char I2CReadByte_no_sub(unsigned char cDevAddr);
unsigned char I2CWriteByte(unsigned char cDevAddr, unsigned char cReg, unsigned char cData);
unsigned char I2CReadBytes(unsigned char cDevAddr, unsigned char cReg, unsigned char *pString,char unsigned cNum);
unsigned char I2CWriteByte_noAddrsss(unsigned char cDevAddr,unsigned char cData);
unsigned char I2CWrite_nBytes(unsigned char cDevAddr,unsigned char *buff,unsigned char num);
void twdDelay(unsigned short wLoops);
void OSDCfgWr(unsigned char index,unsigned char dat);
void OSD1SetRamData(unsigned int wdata);
void OSDSetRamAddr(unsigned int address);
void OSD2CfgWr(unsigned char index,unsigned char dat);
void OSD2SetRamAddr(unsigned int address);
void OSD2SetRamData(unsigned int wdata);
unsigned char twdWr_Burst_A(unsigned char cReg);
void twdWr_Burst_D(unsigned char cData);
void twdWr_Burst_P(void);
void OSDSetRamDataBuest(unsigned int wdata);
/**********************************************************
Function:I2c start command
**********************************************************/
void StartCondition(void)
{
unsigned char ix;
unsigned char cTWtrytime=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 */
}
/**********************************************************
Function:I2c stop command
**********************************************************/
void StopCondition(void)
{
unsigned char ix;
unsigned char 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 */
}
/**********************************************************
Function:I2c send one byte to the slower
**********************************************************/
unsigned char Send_Byte(unsigned char cData)
{
unsigned char ix, j, cAcknowledge;
unsigned char cTWtrytime=0;
cAcknowledge = 0;
for(ix = 0; ix < 8; ix++)
{
Set_SCL_Low;
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;
SET_SDA_AS_INPUT;
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
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 */
SET_SDA_AS_OUTPUT; /*This is tomi new adding,because the ST IO not same as the 8051*/
return(cAcknowledge);
}
/**************************************************************
Function:Read a byte from slower with a acknowledge bit
**************************************************************/
unsigned char Read_Byte(void)
{
unsigned char ix, j;
unsigned char cRetval=0;
SET_SDA_AS_INPUT; /*This is tomi new adding,because the ST IO not same as the 8051*/
for(j=0;j<TWD_SHORT_TIME;j++)
{};
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;
}
return cRetval;
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cm_Ack
INPUT/OUTPUT : None.
DESCRIPTION : Acknoledge generation from now.
COMMENTS : Transfer sequence = DATA, ACK.
-----------------------------------------------------------------------------*/
void I2Cm_Ack (void)
{
unsigned char j;
SET_SDA_AS_OUTPUT;
Set_SDA_Low;
Set_SCL_High;
while(SCL_Low){};
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
Set_SCL_Low;
/*This is tomi new adding,because the ST IO not same as the 8051*/
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
Set_SDA_High;
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : I2Cm_nAck
INPUT/OUTPUT : None.
DESCRIPTION : Non acknoledge generation from now.
COMMENTS : Transfer sequence = DATA, NACK.
-----------------------------------------------------------------------------*/
void I2Cm_nAck (void)
{
unsigned char j;
SET_SDA_AS_OUTPUT;
Set_SDA_High;
Set_SCL_High;
while(SCL_Low){};
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
Set_SCL_Low;
/*This is tomi new adding,because the ST IO not same as the 8051*/
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
Set_SDA_High;
for(j = 0; j < TWD_SHORT_TIME; j++)
{ };
}
/*******************************************************************
Function:Maker read one byte from the slower's appointed address
*******************************************************************/
unsigned char I2CReadByte(unsigned char cDevAddr, unsigned char cReg)
{
unsigned char 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();
I2Cm_Ack();
StopCondition();
return cResult;
}
/*******************************************************************
Function:
*******************************************************************/
unsigned char I2CReadByte_no_sub(unsigned char cDevAddr)
{
unsigned char cResult = 0;
/* write reg offset */
StartCondition();
if(Send_Byte(cDevAddr))
return 0; // Write address
/* read data */
StartCondition();
if (Send_Byte(cDevAddr | 0x01)) // Read address
return 0;
cResult = Read_Byte();
I2Cm_Ack();
StopCondition();
return cResult;
}
/*******************************************************************
Function:Maker send one byte to the slower's appointed address
*******************************************************************/
unsigned char I2CWriteByte(unsigned char cDevAddr, unsigned char cReg, unsigned char 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);
}
/*********************************************************************
Function:Read more than one byte from the slower to the appointed address
*********************************************************************/
unsigned char I2CReadBytes(unsigned char cDevAddr, unsigned char cReg, unsigned char *pString,char unsigned cNum)
{
/* 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;
while(cNum)
{
*pString++ = Read_Byte();
if(cNum==1)
I2Cm_nAck();
else
I2Cm_Ack;
cNum--;
}
StopCondition();
return 1;
}
/*********************************************************************
Function:Send one byte to the slower no the appointed address
*********************************************************************/
unsigned char I2CWriteByte_noAddrsss(unsigned char cDevAddr,unsigned char cData)
{
/* start condition */
StartCondition();
cDevAddr &= 0xFE; // Force WRITE address
/* send device ID and write data */
if(!Send_Byte(cDevAddr))
{
if(!Send_Byte(cData))
{
StopCondition();
return(0); /* success */
}
}
StopCondition();
return(1);
}
/****************************************************************
Function:I2c write n bytes
****************************************************************/
unsigned char I2CWrite_nBytes(unsigned char cDevAddr,unsigned char *buff,unsigned char num)
{
unsigned char i;
unsigned char Ack;
/* start condition */
StartCondition();
cDevAddr &= 0xFE; // Force WRITE address
/* send device ID and write data */
Ack = Send_Byte(cDevAddr);
if(!Ack)
{
for (i=0; i<num; i++)
{
if (!Ack)
Ack = Send_Byte (*buff++);
}
StopCondition();
return(0); /* success */
}
StopCondition();
return(1);
}
/***********************************************************
Function:I2C write have sub address and write n bytes
***********************************************************/
unsigned char I2C_write_sub(unsigned char cDevAddr,unsigned char sub_addr,unsigned char *buff,unsigned char num)
{
unsigned char i;
unsigned char Ack;
/* start condition */
StartCondition();
cDevAddr &= 0xFE; // Force WRITE address
/* send device ID and write data */
Ack = Send_Byte(cDevAddr);
if(!Ack)
Ack = Send_Byte(sub_addr);
if(!Ack)
{
for (i=0; i<num; i++)
{
if (!Ack)
Ack = Send_Byte (*buff++);
}
StopCondition();
return(0); /* success */
}
StopCondition();
return(1);
}
////////////////////////////////////////////////////////////////////////////
void twdDelay(unsigned short wLoops)
{
unsigned short wTemp;
while (wLoops--) {
wTemp = 1000/6; // one loop below takes about 11 us
while (wTemp--);
}
}
/////////////////////////////////////////////////////////////////////////
void OSDCfgWr(unsigned char index,unsigned char dat)
{
I2CWriteByte(TW803_P0,OSD_CFG_INDEX,index);
I2CWriteByte(TW803_P0,OSD_CFG_DATA,dat);
}
/////////////////////////////////////////////////////////////////////////
void OSDSetRamAddr(unsigned int address)
{
I2CWriteByte(TW803_P0,OSD_RAM_AL,(unsigned char)(address & 0xff));
I2CWriteByte(TW803_P0,OSD_RAM_AH,(unsigned char)(address>>8));
}
///////////////////////////////////////////////////////////////////////////
void OSD1SetRamData(unsigned int wdata)
{
I2CWriteByte(TW803_P0,OSD_RAM_DL,(unsigned char)(wdata & 0xff));
I2CWriteByte(TW803_P0,OSD_RAM_DH,(unsigned char)(wdata>>8));
}
///////////////////////////////////////////////////////////////////////////
void OSD2CfgWr(unsigned char index,unsigned char dat)
{
I2CWriteByte(TW803_P0,OSD2_CFG_INDEX,index);
I2CWriteByte(TW803_P0,OSD2_CFG_DATA,dat);
}
///////////////////////////////////////////////////////////////////////////
void OSD2SetRamAddr(unsigned int address)
{
I2CWriteByte(TW803_P0,OSD2_RAM_AL,(unsigned char)(address & 0xff));
I2CWriteByte(TW803_P0,OSD2_RAM_AH,(unsigned char)(address>>8));
}
//////////////////////////////////////////////////////////////////////////////
void OSD2SetRamData(unsigned int wdata)
{
I2CWriteByte(TW803_P0,OSD2_RAM_DL,(unsigned char)(wdata & 0xff));
I2CWriteByte(TW803_P0,OSD2_RAM_DH,(unsigned char)(wdata>>8));
}
/////////////////////////////////////////////////////////////////////////////
unsigned char twdWr_Burst_A(unsigned char cReg)
{
StartCondition();
if(!Send_Byte(TW803_P0))
{
if(!Send_Byte(cReg))
{
return(0); //
}
}
return(1); //
}
//////////////////////////////////////////////////////////////////////////////
void twdWr_Burst_D(unsigned char cData)
{
unsigned char ix;
for(ix = 0; ix < 8; ix++)
{
Set_SCL_Low;
if(cData&0x80)Set_SDA_High;
else Set_SDA_Low;
cData<<=1;
Set_SCL_High;
}
Set_SCL_Low;
Set_SDA_High; /* release data line for acknowledge */
Set_SCL_High; /* Send a clock for Acknowledge */
// if(SDA_High) cAcknowledge = 1; /* No Acknowledge */
Set_SCL_Low; /* Finish Acknoledge */
// return(cAcknowledge);
}
//////////////////////////////////////////////////////////////////////////
void twdWr_Burst_P(void)
{
StopCondition();
}
void OSDSetRamDataBuest(unsigned int wdata) //add for new version
{
twdWr_Burst_D((unsigned int)(wdata & 0xff));
twdWr_Burst_D((unsigned int)(wdata>>8));
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -