0% found this document useful (0 votes)
217 views9 pages

Programming I2C/TWI Using AVR Microcontroller

This document discusses programming the Inter-Integrated Circuit (I2C) interface on a microcontroller. It describes the registers used for I2C communication - the TWBR for setting the bit rate, TWSR for status, and TWCR for control. The steps for a programming I2C transaction are outlined as initializing the bit rate and enabling I2C, starting the transaction, writing the address and data, and stopping. Examples of writing a byte in master mode and reading data from an address are provided in C code.

Uploaded by

Azam Rafique
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
217 views9 pages

Programming I2C/TWI Using AVR Microcontroller

This document discusses programming the Inter-Integrated Circuit (I2C) interface on a microcontroller. It describes the registers used for I2C communication - the TWBR for setting the bit rate, TWSR for status, and TWCR for control. The steps for a programming I2C transaction are outlined as initializing the bit rate and enabling I2C, starting the transaction, writing the address and data, and stopping. Examples of writing a byte in master mode and reading data from an address are provided in C code.

Uploaded by

Azam Rafique
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Mehran University of Engineering & Technology,

Jamshoro
Department of Telecommunication Engineering

Microcontroller & Applications


Programming I2C or TWI

By:

Engr. Azam Rafique


Instrumentation Engineer
TQCIC, IIT Building, MUET,
Jamshoro, Pakistan
email: [email protected]

Inter-Integrated Circuit/Two Wire


Interface

Registers
TWBR (TWI Bitrate Register)
To TWI Clock Frequency

TWSR (TWI Status Register)


TWCR (TWI Control Register)

TWBR (TWI Bit-rate Register)


=

24

16)

TWPS is in TWSR to set Pre-scaler

TWSR (TWI Status Register)

TWCR (TWI Control Register)

Programming Steps

1. Set Frequency and Enable TWI


2. Start TWI (TWCR)
3. Write Address (TWDR & TWCR)
4. Write Data (TWDR & TWCR)
5. Stop TWI (TWCR)

Address:
b0=0
b0=1

// Write Operation
// Read Operation

1. Write Byte in master Mode @ 50KHz


#include avr/io.h
int main(void)
{
TWSR=0x00;
TWBR=0x48;
TWCR=0x04;
TWCR |= (1<<TWSTA);
while(TWCR & (1<< TWINT) == 0);

// start TWI
// wait till op. finish

TWDR=0xD0;
while(TWCR & (1<<TWINT) == 0);

// Address + Write(0)
// wait till tx completes

TWDR=0xF0;
while(TWCR & (1<<TWINT) == 0);

// Data
// wait till tx completes

TWCR |= (1<<TWSTO);
while(1);

// Stop TWI

return 0;
}

2. Read Data from Address 0x98 and


display data at Port A @ 50KHz
#include avr/io.h
int main()
{
DDRA=0xFF;
TWSR=0x00;
TWBR=0x48;
TWCR=0x04;
TWCR |= (1>>TWSTA);
while(TWCR & (1<<TWINT) == 0);
TWDR=0x99;
//Address + Read (1)
while(TWCR & (1<<TWINT) == 0);
while(TWCR & (1<<TWINT) == 0);
PORTA=TWDR;
TWCR |= (1<<TWSTO);
while(1);
return 0;
}

You might also like