With the analog GPIO IIC s5pc100

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Tree_in_sea/article/details/100575276

With the analog GPIO IIC s5pc100

A, IIC bus Introduction

 

IIC (Inter-Integrated Circuit, also called IIC) bus developed by PHILIPS Company is a serial bus, for connecting the microcontroller and its peripheral equipment, which has the following characteristics.

 

(1) Only two bus lines: a serial data line (SDA), a serial clock line (SCL)

(2) Each of the devices connected to the bus can use the software in accordance with its unique address to identify

Inter-device (3) data transmission is simple master / slave relationship

(4) may be used as host or the host sends the host receiver

(5) It is a true multi-master bus, two or more host initiates data transmission simultaneously, data destruction can be prevented by the collision detection and arbitration

(6) 8-bit bidirectional serial data transfer, the bit rate in standard mode up to 100kbit / s up to 400kbit / s in the fast mode in the high speed mode up to 3.4Mbit / s

Note: When the host is trying to control a plurality of bus arbitration can be made only by a host to get control of the bus, it transmits the information and is not destroyed

Second, the signal type IIC bus

There are three types IIC bus signals during data transfer: start signal, a response signal and end signal

(1) start signal (S): SCL is high, the SDA transitions from high to low to start the data transfer

(2) end signal (P): SCL is high, the SDA LOW to HIGH transition, the end of the data transfer

(3) a response signal (ACK): a receiver after receiving the 8-bit data, at the ninth clock cycle, pull down the SDA level

Note: data transmission on the SDA must remain stable during the high period SCL, SDA only data on the SCL is low during

Three, IIC bus data transfer format

 

Each byte sent to the SDA line must be 8-bit, the number of bytes that can be transmitted per transfer is not limited. First transmission is the most significant bit (MSB).

 

When starting a transmission, host first sends a signal S, 8-bit data is then sent. This 8-bit data before transmission is a seven slave address, bit 8 directions (0 indicates a write operation, a read operation indicates 1). When the slave will send an ACK signal is received.

Note: the master receiver after receiving the last byte is not an ACK signal. Thus, the slave releases the SDA line transmitter to allow the end of the transmission signal sent by the host P.

 

Third, the IIC bus timing with analog GPIO

 

By the previous introduction, we already know the IIC bus. Below we will use two pin to simulate the IIC bus timing to read the temperature measured by the sensor LM75 is stable.

 

Let's look at the connection on the LM75 hardware:

Wherein the data line is I2C_SDA0, I2C_SCL0 a clock line, which are respectively connected to the GPD3 s5pc100 and GPD4, as shown in FIG.

When the simulation IIC, GPD3 transmission data or read data pin, providing a clock signal to a GPD4 pins.

Because this group of pins GPD s5pc100 many, not individually for each pin of operation wherein, in order to separate a pair of pins wherein a separate operation, where the use of the bit field of the C language.

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

(1) generating a start signal IIC

SCL is high, the SDA transitions from high to low to start the data transfer

/*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;

}

(2)产生IIC停止信号

 

SCL为高电平时,SDA由低电平向高电平跳变,结束传送数据

/*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;

}

(3)发送数据

/*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;

}

(4)读取数据

/*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;

}

(5)主机向从机发送ACK信号

void iic_send_ack()

{

SCL = HIGH;

SDA = LOW;

delay(50);

SCL = LOW;

delay(50);

return;

}

(6)获取从机给主机的ACK信号

 

uint8 iic_get_ack()

{

uint8 ret;

SCL = HIGH;

delay(50);

ret = SDA;

SCL = LOW;

delay(50);

return ret;

}

四、读取LM75测量的温度值

通过IIC读取LM75测量温度值的时序如下:

实例代码如下:

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;

}

实验的经验:

1.读不到从机发送的ACK信号,原因是没有将SDA的那根线设为输入模式

2.在做的过程中,一开始每次读取的时候温度的值一直没有改变,后来发现是由于每次没有发送停止信号产生的。 

 

 

 

Guess you like

Origin blog.csdn.net/Tree_in_sea/article/details/100575276
IIC