+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Newbie level 3
    Points: 203, Level: 2

    Join Date
    Nov 2011
    Posts
    3
    Helped
    0 / 0
    Points
    203
    Level
    2

    I2C Protocol Implement for AT89LP6440..???

    Dear all,

    Im started working with the Controller AT89LP6440, here need to implement I2c protocol.. Im findin difficult so plz help me out guys.. Plz give some C codes relatin this controller...

    Tx in advance.

    •   AltAdvertisment

        
       

  2. #2
    Advanced Member level 4
    Points: 6,490, Level: 19

    Join Date
    Feb 2011
    Posts
    1,170
    Helped
    284 / 284
    Points
    6,490
    Level
    19

    Re: I2C Protocol Implement for AT89LP6440..???

    I wrote and tested this on a different processor, but it is written in C hence the code is portable. You need to adapt to
    use the ports/pins on your processor, and adjust the timings where needed. The timing is implemented in two functions:
    i2c_usec_delay() and delay_10ms(). The first delays for 1usec, and the second delays in multiples of 10msec. Neither of them
    need to be accurate.
    You can see the timing information in any NXP datasheet for any I2C based device, e.g. an RTC.
    This code was confirmed working for I2C write operations - I've not tested for I2C read operations (but should hopefully be fine).
    In general, if you get stuck with serial protocols like I2C, then a storage scope connected to the data and clock lines is a good
    way to quickly troubleshoot.

    EDIT - just remembered this code is for the case if a microcontroller does not have any open-drain pins. It therefore needs some
    hardware, as shown here. If you have open-drain pin capability on your microcontroller then it is easier and you don't need this
    circuit. You'll have to adapt the code accordingly.

    Click image for larger version. 

Name:	i2c.jpg 
Views:	2 
Size:	35.0 KB 
ID:	76983


    Code:
    /**********************************************
     * I2C library version 1
     * Intended for ATmega32U2 processor
     *
     * pins:
     *  PORT D  0 - SCL_PULL
     *  PORT D  1 - SDA_PULL
     *  PORT D  2 - SCL_READ
     *  PORT D  3 - SDA_READ
    
     *  PORT D  6 - LED output
     *  PORT D  7 - Switch output
     * 
     **********************************************/
    
    
    #include <iom32u2.h>
    #include <intrinsics.h>
    #include <avr_macros.h>
    #include <stdio.h>
    
    // bits
    #define BIT0 0x01
    #define BIT1 0x02
    #define BIT2 0x04
    #define BIT3 0x08
    #define BIT4 0x10
    #define BIT5 0x20
    #define BIT6 0x40
    #define BIT7 0x80
    
    // port D stuff
    #define SCL_PULL   BIT0
    #define SDA_PULL   BIT1
    #define SCL_READ   BIT2
    #define SDA_READ   BIT3
    #define LED        BIT6
    #define BUTTON     BIT7
    
    // inputs
    #define SWITCH_ON (PIND & BUTTON) == 0
    #define SWITCH_OFF (PIND & BUTTON) != 0
    #define SCL_IS_HIGH (PIND & SCL_READ) !=0
    #define SDA_IS_HIGH (PIND & SDA_READ) !=0
    
    // outputs
    #define SCL_SET_LOW PORTD |= SCL_PULL
    #define SCL_SET_HIGH PORTD &= ~SCL_PULL
    #define SDA_SET_LOW PORTD |= SDA_PULL
    #define SDA_SET_HIGH PORTD &= ~SDA_PULL
    
    #define LED_ON PORTD |= LED
    #define LED_OFF PORTD &= ~LED
    
    void
    i2c_usec_delay(void)
    {
      __delay_cycles(8);
    
    }
    
    
    void
    delay_10ms(unsigned int val)
    {
      unsigned int i;
      for (i=0; i<val; i++)
      {
        __delay_cycles(25000);
      }
    }
    
    void
    init(void)
    {
      DDRD=0x43; // Set pin 6 (LED) and the first two pins, i.e. the SCL_PULL and SDA_PULL pins as outputs
      SDA_SET_HIGH;
      SCL_SET_HIGH;
    }
    
    void
    i2c_start(void)
    {
      i2c_usec_delay();
      SDA_SET_LOW;
      i2c_usec_delay();
      SCL_SET_LOW;
      i2c_usec_delay();
    }
    
    void i2c_stop(void)
    {
      // SDA should already be low for us to begin the stop condition
      // SCL should already be high.
      SDA_SET_LOW;
      SCL_SET_HIGH;
      i2c_usec_delay();
      SDA_SET_HIGH;
      // lets wait longer after a stop condition
      delay_10ms(1);
    }
    
    void i2c_write(unsigned char val)
    {
      unsigned char i;
      for (i=0; i<8; i++)
      {
        if (val & 0x80)
        {
          SDA_SET_HIGH;
        }
        else
        {
          SDA_SET_LOW;
        }
        i2c_usec_delay();
        SCL_SET_HIGH;
        i2c_usec_delay();
        SCL_SET_LOW;
        i2c_usec_delay();
        val <<= 1;
      }
      // ok now lets allow an ack to be received
      SDA_SET_HIGH;
      i2c_usec_delay();
      SCL_SET_HIGH;
      i2c_usec_delay();
      SCL_SET_LOW;
      i2c_usec_delay();
    }
      
    unsigned char
    i2c_read(void)
    {
      unsigned char i;
      unsigned char val=0;
      
      for (i=0; i<8; i++)
      {
        val<<=1;
        SCL_SET_HIGH;
        i2c_usec_delay();
        if (SDA_IS_HIGH)
        {
          val |= 0x01;
        }
        SCL_SET_LOW;
        i2c_usec_delay();
      }
      return val;
    }
    Last edited by sky_123; 9th July 2012 at 15:28.


    1 members found this post helpful.

--[[ ]]--