Saturday, July 25, 2015

INTERFACING OF PCF 8574 TO ATmega16 USING I2C COMMUNICATION


Hi Guys Today we will see about I2C Communication in ATmega16.

I2C - Inter Integrated Circuit also can be called as I2C

In I2C Multi Master, Master-Slave, Multi Slave communication takes places. By using I2C protocol we can connect N number devices with a µC using two pin SCL and SDA.

SCL and SDA are pin used for I2C communication. Serial Clock Pin and Serial Data pin

I2C main works with clock frequency because it’s synchronous communication. Today we will see simple interface of I/O Expander

To work with I2C we must know following Condition:
     
     1.) Start.
     2.) Stop.
     3.) Write.
     4.) Read.

Following are Step to initiate I2C communication
1.) Initial Condition:
         1.)  Set Baud Rate for Data Transfer in TWBR
         2.)  Set the TWEN bit in TWCR reg, for enabling TWI communication.
         3.)  Set Prescaler in TWSR.

    1.)  TWBR: Two Wire Baud Rate
Bit
7
6
5
4
3
2
1
0
Name
TWBR7
TWBR6
TWBR5
TWBR4
TWBR3
TWBR2
TWBR1
TWBR0
Value









TWBR Value is calculated by using this equation:
SCL CLOCK = (CPU CLOCK FREQUENCY) / (16+2*(TWBR)*4^TWPS);
In equation TWSR value is set by the two bit in that reg.
TWPS1
TWPS2
Prescaler
0
0
1
0
1
4
1
0
16
1
1
64

In this case I am assume SCL as 10 kHz, I am using F_CPU as 8MHz and setting Prescaler as 1.
By Calculating we get TWBR Value as 98 in decimal we will enter in Hex as 62
    
 2.)   TWCR: Two Wire Control Register

Bit
7
6
5
4
3
2
1
0
Name
TWINT
TWEA
TWSTA
TWSTO
TWWC
TWEN
TWIE
Value





1


         
Set Value of 1 to TWEN Bit to Start I2C communication.



    3.)  TWSR: Two Wire Status Register

Bit
7
6
5
4
3
2
1
0
Name
TWS7
TWS6
TWS5
TWS4
TWS3
-
TWPS1
TWPS0
Value









 In my case it’s TWPS1 = 0 and TWPS0 = 0.
We should set TWPS1 & TWPS0 for prescaler.
Rest of bit as the status bit which indicates the status of i2c communication.

2.) Start Condition:
To Initiate start condition.
    1.)  Clear TWINT by setting it 1.
    2.)  Set TWSTA Bit in TWCR reg as 1.
    3.)  Set TWEN Bit.
    4.)  Check for TWINT bit set in TWCR reg.

         TWCR:

Bit
7
6
5
4
3
2
1
0
Name
TWINT
TWEA
TWSTA
TWSTO
TWWC
TWEN
TWIE
Value
1

1


1



Syntax: while (! (TWCR & (1<<TWINT)));

3.) STOP CONDITION:

To Stop the I2c
     1.)  Clear TWINT bit by setting it 1.
     2.)  Set TWEN bit as 1.
     3.)  Set TWSTO bit as 1.

TWCR:

Bit
7
6
5
4
3
2
1
0
Name
TWINT
TWEA
TWSTA
TWSTO
TWWC
TWEN
TWIE
Value
1


1

1




4.) Write Condition:

To write to slave.
     1.)  Move the data to be transmitted to slave in TWDR (I2C Data Register).
     2.)  Clear TWINT by setting bit as 1 in TWCR and TWEN is set.
     3.)  Check for TWINT bit set in TWCR reg.

TWCR:

Bit
7
6
5
4
3
2
1
0
Name
TWINT
TWEA
TWSTA
TWSTO
TWWC
TWEN
TWIE
Value
1




1



Syntax: while (! (TWCR & (1<<TWINT)));

5.) Read Condition:

To read data from slave

    1.)  Clear TWINT bit as 1 in TWCR and TWEN is set.
    2.)  Check for TWINT bit set in TWCR reg. 
    3.)  Return TWDR using return function.

TWCR:

Bit
7
6
5
4
3
2
1
0
Name
TWINT
TWEA
TWSTA
TWSTO
TWWC
TWEN
TWIE
Value
1




1



Syntax: while (! (TWCR & (1<<TWINT)));
Return Value in TWDR.

 We are using PCF 8574




IO Expander :

It has control register which should be writing with correct control word or we can’t access the IC. So for IO Expander to work

Control word is form by:

D7
D6
D5
D4
D3
D2
D1
D0
0
1
0
0
A2
A1
A0
0

A2-A0 is used to different device we can connect around 7 devices like this.

D0 = 0 means write to IO Pin Register.
D0 = 1 means read from IO Pin Register.

In our case its
We are using as output. So writing to IO Expander

D7
D6
D5
D4
D3
D2
D1
D0
0
1
0
0
0
0
0
0

Control Word to write is 0x40

We will now program it’s as input pin for Atmega16 and move data in IO pin into PORTA

We must give start condition to get each 8 bit data so we should get start in while loop

D7
D6
D5
D4
D3
D2
D1
D0(R/W)
0
1
0
0
0
0
0
0


Control words to read is 0x41

Thank You.

No comments:

Post a Comment