标签:
转载:http://blog.chinaunix.net/uid-26833883-id-3823530.html
先来看看LM75硬件的上的连线:
其中I2C_SDA0是数据线,I2C_SCL0是时钟线,它们分别接到s5pc100的GPD3和GPD4,如下图所示
在模拟IIC的时候,用GPD3引脚发送数据或读取数据,用GPD4引脚提供时钟信号即可。
typedef struct
{
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;
} gpddat_t;
#define GPD_DAT (* (volatile gpddat_t *)0xE0300084)
#define SDA GPD_DAT.GPDDAT_3
#define SCL GPD_DAT.GPDDAT_4
/*IIC START:SCL = 1,SDA = 1->0*/
void iic_start()
{
SDA = HIGH;
SCL = HIGH;
delay(50);
//高到低的跳变产生start信号
SDA = LOW;
delay(50);
//在SCL高时,SDA必须保持稳定,SCL低时,SDA可以任意改变
//此处将SCL拉低的目的是,接下来就要发送数据了
SCL = LOW;
delay(50);
return;
}
/*IIC STOP:SCL = 1,SDA = 0->1*/
void iic_stop()
{
SDA = LOW;
SCL = LOW;
delay(50);
SCL = HIGH;
delay(50);
//SCL为高电平时,SDA从低电平跳变到高电平
//,产生停止信号
SDA = HIGH;
delay(50);
return;
}
/*Write 1 Byte to IIC*/
void iic_write_byte(uint8 data)
{
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;
}
/*Read 1 byte from IIC*/
uint8 iic_read_byte()
{
uint8 loop;
uint8 value = 0;
for(loop = 8; loop > 0;loop --)
{
SCL = HIGH;
delay(50);
value <<= 1;
//读取1位数据
value |= SDA;
SCL = LOW;
delay(50);
}
return value;
}
void iic_send_ack()
{
SCL = HIGH;
SDA = LOW;
delay(50);
SCL = LOW;
delay(50);
return;
}
uint8 iic_get_ack()
{
uint8 ret;
SCL = HIGH;
delay(50);
ret = SDA;
SCL = LOW;
delay(50);
return ret;
}
实例代码如下:
unsigned int __read_lm75()
{
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);
//等待从机的ACK
do{
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;
}
完。
标签:
原文地址:http://www.cnblogs.com/pengdonglin137/p/4624469.html