Sunday 26 January 2014

I2C header for AVR microcontrollers

        This header contains function description for I2C module in AVR microcontrollers.
        The Two-wire Serial Interface (TWI) is ideally suited for typical microcontroller applications. The TWI protocol allows the systems designer to interconnect up to 128 different devices using only two bi-directional bus lines, one for clock (SCL) and one for data (SDA). The only external hardware needed to implement the bus is a single pull-up resistor for each of the TWI bus lines. All devices connected to the bus have individual addresses, and mechanisms for resolving bus contention are inherent in the TWI protocol.

Features:
• Simple Yet Powerful and Flexible Communication Interface, Only Two Bus Lines Needed
• Both Master and Slave Operation Supported
• Device Can Operate as Transmitter or Receiver
• 7-bit Address Space allows up to 128 Different Slave Addresses
• Multi-master Arbitration Support
• Up to 400 kHz Data Transfer Speed
• Slew-rate Limited Output Drivers
• Noise Suppression Circuitry Rejects Spikes on Bus Lines
• Fully Programmable Slave Address with General Call Support
• Address Recognition causes Wake-up when AVR is in Sleep Mode
Registers:
Clock frequency is set by TWBR register

TWI Bit Rate Register – TWBR
TWI Data Register      – TWDR
TWI (Slave) Address Register – TWAR
TWI Control Register – TWCR
TWI Status Register  – TWSR


THE FUNCTIONS USED IN I2C.H ARE...
    void I2CInit();
            To initialse I2C module with defined clock freq, mode of operation.

    void I2CStart();
            Sends a start signal on I2C bus

    void I2CStop();
            Sends a stop signal on I2C bus

    unsigned char I2CWriteSLA(unsigned char sla);
            Sends Slave address with write/read command.

    unsigned char I2CWriteByte(unsigned char dat);
             sends a data byte on I2C bus

    unsigned char I2CReadByte(unsigned char *data);
            Reads a data byte from I2C bus.

void I2CInit()
 {
  TWBR=0x04;
  TWSR|=((1<<TWPS1) | (1<<TWPS0));
  SETBIT(TWCR,TWEN)
// SDA & SCL PINS ARE OVERRIDE BY TWI & CAN'T BE USED AS I/O WHEN TWI ENABLED
// so no DDR & PULL-UP settings required.
 }

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

#define I2CStop()      TWCR=(1<<TWINT)| (1<<TWEN)|(1<<TWSTO)

unsigned char I2CWriteByte(uint8_t dat)
 {
unsigned char STATUS;
  TWDR=dat;
  TWCR=(1<<TWEN) | (1<<TWINT);
  while(!(TWCR & (1<<TWINT)));

  STATUS=TW_STATUS;

 if(STATUS==TW_MT_DATA_ACK || STATUS==TW_MT_DATA_NACK || STATUS==TW_MT_SLA_ACK || STATUS==TW_MR_SLA_ACK)
      return TRUE;
  else
    return FALSE;
}

unsigned char I2CReadByte(unsigned char *data)
 {
  unsigned char STATUS; 
  TWCR&=(~(1<<TWEA));
  CLEARBIT(TWCR,TWINT)
  while(!(TWCR & (1<<TWINT)));
  STATUS=TW_STATUS;
  if(STATUS==TW_MR_DATA_ACK || STATUS==TW_MR_DATA_NACK)   
   {
      *data=TWDR;       
    return TRUE;    
    }
  else
    return FALSE;
}



0 comments:

Post a Comment

Search Here...