Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I2C Protocol Implement for AT89LP6440..???

Status
Not open for further replies.

Vindy789

Newbie level 3
Joined
Nov 30, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,299
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.
 

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.

i2c.jpg


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:

    V

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top