/******************************************************************************* **函 数: IIC_SendByte **功 能: IIC发送一个字节 **参 数: dat --数据 **返 回: void **说 明: 高位先发 *******************************************************************************/ void IIC_SendByte(u8 dat) { u8 i; // SCL0;//xjx.add for(i=0;i<8;i++) { if(dat&0x80) { SDA1; } else { SDA0; } dat<<=1; SCL1; IIC_delay(); SCL0; IIC_delay(); } // SDA1;//xjx.add }
/******************************************************************************* **函 数: IIC_Stop **功 能: IIC停止信号 **说 明: When SCL is high , SDA change form low to high. *******************************************************************************/ void IIC_Stop(void) { SDA0; SCL1; IIC_delay(); SDA1; IIC_delay(); }
/******************************************************************************* **函 数: IIC_NACK **功 能: IIC发送NACK信号 *******************************************************************************/ void IIC_NACK(void) { SDA1; SCL1; IIC_delay(); SCL0; SDA0; IIC_delay(); }
/******************************************************************************* **函 数: IIC_Start **功 能: IIC开始信号 **说 明: When SCL is high , SDA change form high to low. *******************************************************************************/ void IIC_Start(void) { SDA1; SCL1; IIC_delay(); SDA0; IIC_delay(); SCL0; IIC_delay(); }
//终止I2C总线,当SCL为高电平时使SDA产生一个正跳变 void Stop(void) { gpio_output_low(TUNER_SDA); gpio_output_low(TUNER_SCL); IIC_delay(); gpio_output_high(TUNER_SCL); IIC_delay(); gpio_output_high(TUNER_SDA); IIC_delay(); }
void Start(void) { gpio_set_output_mode(TUNER_SDA); gpio_set_output_mode(TUNER_SCL); gpio_output_high(TUNER_SDA); gpio_output_high(TUNER_SCL); IIC_delay(); gpio_output_low(TUNER_SDA); IIC_delay(); gpio_output_low(TUNER_SCL); IIC_delay(); }
/******************************************************************************* **函 数: IIC_SendByte **功 能: IIC发送一个字节 **参 数: dat --数据 **返 回: void **说 明: 高位先发 *******************************************************************************/ void IIC_SendByte(u8 dat) { u8 i; for(i=0;i<8;i++) { if(dat&0x80) SDA1; else SDA0; dat<<=1; SCL1; IIC_delay(); SCL0; IIC_delay(); } }
/******************************************************************************* **函 数: IIC_GetByte **功 能: IIC读取一个字节 **参 数: void **返 回: dat --数据 **说 明: 高位先收 *******************************************************************************/ u8 IIC_GetByte(void) { u8 i,dat=0; SDA_IN(); for(i=0;i<8;i++) { SCL1; IIC_delay(); dat=dat<<1; if(Get_SDA()) dat |= 0x01; SCL0; IIC_delay(); } SDA_OUT(); SDA1; return dat; }
void write_byte(unsigned char data) { unsigned char m,tmp; gpio_set_output_mode(TUNER_SDA); for(m = 0; m < 8; m++) { if(data & 0x80) gpio_output_high(TUNER_SDA); else gpio_output_low(TUNER_SDA); IIC_delay(); gpio_output_high(TUNER_SCL); IIC_delay(); data = data<<1; gpio_output_low(TUNER_SCL); } IIC_delay(); IIC_ack(); IIC_delay(); }
/******************************************************************************* **函 数: IIC_WaitAck **功 能: IIC等待ACK信号 **参 数: void **返 回: 0:正常 1:等待超时 **说 明: 该函数加入等待超时处理,防止死等 *******************************************************************************/ u8 IIC_WaitAck(void) { u16 undead = 10000; SDA1; SDA_IN(); //输入 SCL1; IIC_delay(); while(undead--) { if(0 == Get_SDA()) { SCL0; SDA_OUT(); SDA1; IIC_delay(); return 0; } } SCL0; SDA_OUT(); SDA1; IIC_delay(); return 1; }
/******************************************************************************* **函 数: IIC_WaitAck **功 能: IIC等待ACK信号 **参 数: void **返 回: 0:正常 1:等待超时 **说 明: 该函数加入等待超时处理,防止死等 *******************************************************************************/ u8 IIC_WaitAck(void) { u16 undead = 0;//10000; SDA_IN(); //输入 IIC_delay(); SCL1; IIC_delay(); while(Get_SDA()) { if(++undead>1000) { SDA_OUT(); return 0; } } SCL0; IIC_delay(); SDA_OUT(); IIC_delay(); return 1; }
unsigned char read_byte(void) { unsigned char m; unsigned char tmp1 = 0; unsigned char tmp2 = 0; gpio_set_input_mode(TUNER_SDA); for(m = 0; m < 8; m++) { gpio_output_low(TUNER_SCL); IIC_delay(); gpio_output_high(TUNER_SCL); IIC_delay(); tmp1 = gpio_get_input_value(TUNER_SDA); if(1 == tmp1) tmp2 = ((tmp2 << 1) | 1); else tmp2 = ((tmp2 << 1) & 0xfe); gpio_output_low(TUNER_SCL); } IIC_delay(); IIC_ack(); return(tmp2); }
void IIC_ack(void) { unsigned char tmp,m; gpio_set_input_mode(TUNER_SDA); gpio_output_high(TUNER_SCL); for(m=0; m<10; m++) { IIC_delay(); tmp = gpio_get_input_value(TUNER_SDA); if(tmp == 0) break; } gpio_output_low(TUNER_SCL); gpio_set_output_mode(TUNER_SDA); }