做FCT程序时,需要IO口模拟IIC给自身IIC收发,IO虽然也是双向模式,但没有配置说明,也是弄了好久,分享一下,看宏定义配置就可以了!
//---------------------------------------------------------------
#define FCT_SCLK_H do{FCT_SCL_Write(1);} while(0)
#define FCT_SCLK_L do{FCT_SCL_Write(0);} while(0)
#define FCT_DATA_H do{FCT_SDA_Write(1);} while(0)
#define FCT_DATA_L do{FCT_SDA_Write(0);} while(0)
#define FCT_DATA_READ FCT_SDA_Read()
#define FCT_DATA_IN FCT_SDA_SetDriveMode(FCT_SDA_DM_RES_UPDWN)
#define FCT_DATA_OUT FCT_SDA_SetDriveMode(FCT_SDA_DM_STRONG)
void fct_iic_Send_Byte(uint8 txd)
{
uint8 t;
FCT_DATA_OUT;
FCT_SCLK_L;// 拉低时钟开始数据传输
delay_us(2);
for(t=0; t<8; t++)
{
if((txd&0x80)>>7)
{
FCT_DATA_H;
}
else
{
FCT_DATA_L;
}
txd<<=1;
delay_us(2);
FCT_SCLK_H;
delay_us(4);
FCT_SCLK_L;
delay_us(2);
}
delay_us(10);
}
uint8 fct_iic_Read_Byte_ex(void)
{
uint8 n_read_data = 0;
uint8 n_Kt = 0;
FCT_SCLK_L;
FCT_DATA_IN;
while(n_Kt < 8)
{
n_read_data <<= 1;
FCT_SCLK_H;
delay_us(5);
if( FCT_DATA_READ )
{
n_read_data |= 0x01;
}
else
{
n_read_data &= 0xFE;
}
FCT_SCLK_L;
n_Kt++;
delay_us(5);
}
FCT_DATA_OUT;
FCT_DATA_L;
delay_us(5);
FCT_SCLK_H;
delay_us(5);
FCT_SCLK_L;
delay_us(5);
return n_read_data;
}
|