分类: LINUX
2016-09-19 20:55:58
原文地址:用s5pc100的GPIO模拟IIC 作者:草根老师
uint8 GPDDAT_0:1;uint8 GPDDAT_1:1;uint8 GPDDAT_2:1;uint8 GPDDAT_3:1;uint8 GPDDAT_4:1;uint8 GPDDAT_5:1;uint8 GPDDAT_6:1;uint8 GPDDAT_7:1;
SDA = HIGH;SCL = HIGH;delay(50);//高到低的跳变产生start信号SDA = LOW;delay(50);
//在SCL高时,SDA必须保持稳定,SCL低时,SDA可以任意改变//此处将SCL拉低的目的是,接下来就要发送数据了SCL = LOW;delay(50);
SDA = LOW;SCL = LOW;delay(50);
SCL = HIGH;delay(50);//SCL为高电平时,SDA从低电平跳变到高电平//,产生停止信号SDA = HIGH;delay(50);
return;
uint8 loop;
for(loop = 8;loop > 0;loop --){
//先发送最高位,在SCL高电平时,SDA必须保持稳定SDA = data >> 7;SCL = HIGH;delay(50);
//SCL为低电平时,SDA可以任意改变SCL = LOW;//低位向高位移动data <<= 1;delay(50);
}
return;
uint8 loop;uint8 value = 0;
for(loop = 8; loop > 0;loop --){
SCL = HIGH;
delay(50);
value <<= 1;
//读取1位数据value |= SDA;
SCL = LOW;
delay(50);
SCL = HIGH;SDA = LOW;delay(50);
SCL = LOW;delay(50);
return;
uint8 ret;
SCL = HIGH;delay(50);
ret = SDA;
SCL = LOW;delay(50);
return ret;
uint8 ack;uint8 high,low;
//设置IIC连接的pin为输出模式SET_GPIO_MODE(GPD.GPDCON,3,1);SET_GPIO_MODE(GPD.GPDCON,4,1);
delay(100);
//产生起始信号iic_start();
//发送从机地址iic_write_byte(0x91);
//设置IIC连接的pin(SDA)为输入模式SET_GPIO_MODE(GPD.GPDCON,3,0);
//等待从机的ACKdo{ack = iic_get_ack();}while(ack);
//读取从机发送过来的数据high = iic_read_byte();
//设置IIC连接的pin(SDA)为输出模式SET_GPIO_MODE(GPD.GPDCON,3,1);
//发送ACK信号iic_send_ack();
//设置IIC连接的pin(SDA)为输入模式SET_GPIO_MODE(GPD.GPDCON,3,0);
//读取从机发送过来的数据low = iic_read_byte();
//设置IIC连接的pin(SDA)为输出模式SET_GPIO_MODE(GPD.GPDCON,3,1);
//发送停止信号iic_stop();
return (high << 8) | low;