Spread the love

I2C  protocol

___________________________________________________________________________________________________


  • What is I2C ? – I²C (Inter-Integrated Circuit)(alternately spelled I2C or IIC)(most commonly pronounced I-squared-C) is a multimaster serial single-ended computer bus invented by the Philips semiconductor division, today NXP Semiconductors, and used for attaching low-speed peripherals to main system.

 2000px-I2C.svg

  • How I2C protocol work – The I2C protocol requires only 2 signals: clock and data.  Clock is known as SCL or SCK (for Serial Clock), while data is known as SDA (for Serial Data).  What makes I2C unique is the use of special combinations of signal conditions and changes.  Fundamentally, there are just two: Start and Stop.  A ‘START” condition is generated by the Master, followed by 7 bits of address, then a ReadWrite bit.  If a slave device detects an address match, it will send an ACK by driving SDA low during the next clock cycle; if no slave recognizes the address then the SDA line will be left alone to be pulled up high.  Following a successful ACK, data will be either sent to the slave device or read from the slave device (depending on what was indicated by the Read/Write bit).  Therefore, each byte is 9 bits: either 7 address plus one R/W plus one ACK/NAK, or 8 data plus one ACK/NAK.  The last data byte of a transaction should generally be followed by a NAK, to indicate that it is intended to be the final byte.  After this, either a STOP or a ReSTART should be issued by the Master. Bus errors are rarely introduced when using a dedicated I2C peripheral on the Master.

i2c protocol

  •   Feature of  I2C protocol
  •  I2C protocol supports multiple data speeds: standard (100 kbps), fast (400 kbps) and high speed (3.4 Mbps) communications.
  • Built in collision detection
  • 10-bit Addressing
  • Supports both Multi-master and Multi-master with Slave functions.
  • Data broadcast (general call).
  • Since only two wires are required, I2C is well suited for boards with many devices connected on the bus. This helps reduce the cost and complexity of the circuit as additional devices are added to the system.

i2c protcol

 

  • Applications-       The I2C bus is a great option for applications that require low cost and simple implementation rather than high speed. For example, reading certain memory ICs, accessing DACs and ADCs,reading sensors,access LCDs, transmitting and controlling user directed actions, reading hardware sensors, and communicating with multiple microcontrollers are common uses of the I2C communication protocol.I2C also used for attaching low-speed peripherals to a motherboard, embedded system, cellphone, or other digital electronic devices. Several competitors, such as Siemens AG (later Infineon Technologies AG, now Intel mobile communications), NEC, Texas Instruments, STMicroelectronics (formerly SGS-Thomson), Motorola (later Freescale), and Intersil, have introduced compatible I²C products to the market since the mid-1990s.

Click Here : Registers associated with I2C

Click Here : Depth understanding of I2C Protocol

1. CIRCUIT DIAGRAM OF RTC IMPLEMENTATION USING I2C protocol OF ATMEGA 16

RTC VALUE FROM DS1307 IS DISPLAYED ON LCD USING I2C PROTOCOL 

________________________________________________________________________________________________

 1. PROGRAM OF I2C PROTCOL OF ATMEGA16 TO SHOW RTC DATA OF DS1307 ON LCD 

/******************************************************
www.firmcodes.com
DEVELOPED BY:- FIRMWARE DEVELOPER
WHAT PROGRAM DO:-I2C PROTOCOL OF ATMEGA16 USED TO DISPLAY RTC OF DS1307 ON LCD
******************************************************/

#include<avr/io.h>

#include<util/delay.h>

#define lcd PORTA

void serial_ini();
void serial_tr(unsigned char);
void serial_tr_bcd(unsigned char);

void i2c_ini();
void i2c_start();
void i2c_wr(unsigned char);
unsigned char i2c_re(unsigned char);
void i2c_stop();

void rtc_ini();
void rtc_set_t(unsigned char,unsigned char,unsigned char);
void rtc_set_d(unsigned char,unsigned char,unsigned char);
void rtc_get_t(unsigned char *,unsigned char *,unsigned char *);
void rtc_get_d(unsigned char *,unsigned char *,unsigned char *);

void cmd(unsigned char x);
void lcd_display(unsigned char x);
void lcd_ini();
void lcd_str(unsigned char *str);
void lcd_pos(int line,int pos);


int main()
   {
  
       unsigned char i,j,k,p,q,r;
       DDRA=0XFF;
       DDRB=0XFF;
       rtc_ini();
       rtc_set_t(0x19,0x45,0x30);
       rtc_set_d(0x09,0x01,0x10);
       
       serial_ini();
       lcd_ini();
       lcd_str("TIME ");
       lcd_pos(2,0);
       lcd_str("DATE ");
       while(1) 
          {
                 /* time
**************************************************************************/
               lcd_pos(1,6);
               rtc_get_t(&i,&j,&k);
               lcd_display('0'+(i>>4));
               lcd_display('0'+(i & 0x0f));
               lcd_display(':');

               lcd_display('0'+(j>>4));
               lcd_display('0'+(j & 0x0f));
               lcd_display(':');

               lcd_display('0'+(k>>4));
               lcd_display('0'+(k & 0x0f));
               _delay_ms(500);

  /* date
****************************************************************************/

              lcd_pos(2,6);
              rtc_get_d(&p,&q,&r);
              lcd_display('0'+(r>>4));
              lcd_display('0'+(r & 0x0f));
              lcd_display(':');

              lcd_display('0'+(q>>4));
              lcd_display('0'+(q & 0x0f));
              lcd_display(':');

              lcd_display('0'+(p>>4));
              lcd_display('0'+(p & 0x0f));
              _delay_ms(500);

         }
       return 0;
   }

void serial_ini()
   {
       UCSRB=(1<<TXEN);
       UCSRC=(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);
       UBRRL=0X33;
    }
    
void serial_tr(unsigned char x)
   {
       while(!(UCSRA & (1<<UDRE)));
       UDR= x; 
       while(TXC ==0);
   }

void serial_tr_bcd(unsigned char x)
   {
       serial_tr('0'+(x>>4));
       serial_tr('0'+(x & 0x0f));
   }

void i2c_ini()
   {
       TWSR=0X00;
       TWBR=0X47;
       TWCR=0X04;
   }

void i2c_start()
  {
      TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
      while((TWCR &(1<<TWINT))==0);
  }

void i2c_wr(unsigned char x)
  {
     TWDR=x;
     TWCR=(1<<TWINT)|(1<<TWEN);
     while((TWCR & (1<<TWINT))==0);

  }

unsigned char i2c_re(unsigned char x)
   {
       TWCR=(1<<TWINT)|(1<<TWEN)|(x<<TWEA);
       while((TWCR &(1<<TWINT))==0);
       return TWDR;
   }

void i2c_stop()
   {
       TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
       for(int i=0;i<200;i++);
   }

void rtc_ini()
   {
      i2c_ini();
      i2c_start();
      i2c_wr(0xd0);   // address DS1307 for write
      i2c_wr(0x07);  //set register pointer to 7
      i2c_wr(0x00);  //set value of location 7 to 0
      i2c_stop();  // transmit stop condition
   }

void rtc_set_t(unsigned char h,unsigned char m,unsigned char s)
   {
       i2c_start();
       i2c_wr(0xd0);     // address DS1307 for write
       i2c_wr(0);        //set register pointer to 0
       i2c_wr(s);
       i2c_wr(m);
       i2c_wr(h);
       i2c_stop();
   }

void rtc_set_d(unsigned char y,unsigned char m,unsigned char d)
   {
       i2c_start();
       i2c_wr(0xd0);     // address DS1307 for write
       i2c_wr(4);        //set register pointer to 4
       i2c_wr(d);
       i2c_wr(m);
       i2c_wr(y);
       i2c_stop(); 
   }

void rtc_get_t(unsigned char *h,unsigned char *m,unsigned char *s)
   {
       i2c_start();
       i2c_wr(0xd0);     // address DS1307 for write
       i2c_wr(0);        //set register pointer to 0
       i2c_stop();

       i2c_start();
       i2c_wr(0xd1);     // address DS1307 for read
       *s=i2c_re(1);     //read sec ,read ack
       *m=i2c_re(1);     //read min ,read ack
       *h=i2c_re(0);     //read hour ,read nack
       i2c_stop();
   }


void rtc_get_d(unsigned char *y,unsigned char *m,unsigned char *d)
    {
        i2c_start();
        i2c_wr(0xd1);     // address DS1307 for write
        i2c_wr(0);        //set register pointer to 4
        i2c_stop();


        i2c_start();
        i2c_wr(0xd1);     // address DS1307 for read
        *d=i2c_re(1);     //read day ,read ack
        *m=i2c_re(1);     //read month ,read ack
        *y=i2c_re(0);     //read year ,read nack
        i2c_stop();
    }

void cmd(unsigned char x)
   {
       lcd=x;
       PORTB=(0<<0);
       PORTB=(0<<1);
       PORTB=(1<<2);
       _delay_ms(10);
       PORTB=(0<<2);

   }

void lcd_display(unsigned char x)
   {
      lcd=x;
      PORTB=(1<<0);
      PORTB=(0<<1)|(1<<0);
      PORTB=(1<<2)|(0<<1)|(1<<0);
      _delay_ms(20);
      PORTB=(0<<2)|(0<<1)|(1<<0);
   }

void lcd_ini()
   {
       cmd(0x38);
       cmd(0x0e);
       cmd(0x01);
       cmd(0x06);
       cmd(0x80);
   }

void lcd_str(unsigned char *str)
   {
        while(*str!='\0')
          {
               lcd_display(*str);
               str++;
           }
    }

void lcd_pos(int line,int pos)
    {
        if(line==1)
            cmd(0x80+pos);
        else if(line==2)
            cmd(0xc0+pos);
    }

PROTEUS File for SIMULATION(Password Of RAR file is :-firmcodes.com)

download (1)

__________________________________________________________________________________

Content for the tab VIDEO