Friday 30 January 2015

I2C Header File for AVR By Elecdude


/************************************************************************************/

 Author: ElecDude
         admin@elecdude.com        

 Please report bugs, errors, modifications, etc. Thank you

 Copyright - 2015 - ElecDude

 USAGE AND REDISTRIBUTION OF THIS SOURCE CODE IS PERMITTED PROVIDED THAT
 THE FOLLOWING CONDITIONS ARE MET:

    1. REDISTRIBUTIONS OF SOURCE CODE MUST RETAIN THE ABOVE ORIGINAL COPYRIGHT
  NOTICE AND THE ASSOCIATED DISCLAIMER, THIS LIST OF CONDITIONS AND
  THE FOLLOWING DISCLAIMER.
    2. REDISTRIBUTIONS IN BINARY FORM MUST REPRODUCE THE ABOVE COPYRIGHT
  NOTICE, THIS LIST OF CONDITIONS AND THE FOLLOWING DISCLAIMER IN
  THE DOCUMENTATION AND/OR OTHER MATERIALS PROVIDED WITH THE
  DISTRIBUTION.

 THIS IS PROVIDED WITHOUT ANY  EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 A PARTICULAR PURPOSE ARE DISCLAIMED. TO BE USED FOR LEARNING PURPOSE ONLY.  
 IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT OWNER, BE LIABLE FOR ANY DIRECT,
 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ARISING IN ANY WAY OUT OF THE
 USE OF THIS SOURCE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/*************************************************************************************/
/**************************************************************
 I2C  TWI  ROUTINES
void I2CInit();
void I2CStart();
void I2CStop();
unsigned char I2CWriteSLA(unsigned char sla);
unsigned char I2CWriteByte(unsigned char dat);
unsigned char I2CReadByte(unsigned char *data); */

#ifndef _I2C_H
#define _I2C_H

/*******************MACRO's DEFINITION******************************/
#ifndef BIT
#define BIT(x) _BV(x)
#endif

#ifndef SETBIT
#define SETBIT(x,b) x|=_BV(b);
#endif

#ifndef CLEARBIT
#define CLEARBIT(x,b) x&=~_BV(b);
#endif

#ifndef TOGGLEBIT
#define TOGGLEBIT(x,b) x^=_BV(b);
#endif

#ifndef CHECKBIT
#define CHECKBIT(x,b) (x & _BV(b))
#endif
/*******************************************************************/


#define TRUE 1
#define FALSE 0

void I2CInit()
 {
  TWBR=0x04;//F=100KHz
  TWSR|=((1<<TWPS1) | (1<<TWPS0));

  SETBIT(TWCR,TWEN)
 }

void I2CStart()
{
 TWCR=(1<<TWINT)| (1<<TWSTA)|(1<<TWEN);
 while(!(TWCR & (1<<TWINT)));

}

void I2CStop()
{
 TWCR=(1<<TWINT)| (1<<TWEN)|(1<<TWSTO);
 //while(!(TWCR & (1<<TWSTO)));

}

unsigned char I2CWriteSLA(unsigned char sla)
 {

  TWDR=sla;
  TWCR=(1<<TWEN) | (1<<TWINT);
  while(!(TWCR & (1<<TWINT)));
  if((TWSR & 0xF8)==0x18 || (TWSR & 0xF8)==0x40)
  //18 = SLA+W sent & ACK returned
  return TRUE;//40 = SLA+R sent & ACK returned
  else
    return FALSE;
}

unsigned char I2CWriteByte(uint8_t dat)
 {
  unsigned char
  TWDR=dat;

  TWCR=(1<<TWEN) | (1<<TWINT);
  while(!(TWCR & (1<<TWINT)));


  if((TWSR & 0xF8)==0x28 || (TWSR & 0xF8)==0x30)
  return TRUE; //28= Data sent, ACK returned.  30 data sent, NACK retuned
  else
    return FALSE;
}

unsigned char I2CReadByte(unsigned char *data)
 {
 
  TWCR&=(~(1<<TWEA));
 
  CLEARBIT(TWCR,TWINT)
  while(!(TWCR & (1<<TWINT)));

  if((TWSR & 0xF8)==0x50 || (TWSR & 0xF8)==0x58)
   {
  *data=TWDR; //58 = data READ & NACK retuned
return TRUE; //50 = Data READ & ACK returned
    }
  else
    return FALSE;
}

/***************************************************************************/
#endif
I2C.H

If you enjoyed this post plz let us know your views via comments.
This helps us to do much more better.
Thankyou.


0 comments:

Post a Comment

Search Here...