You are on page 1of 27

IIC.

H
#ifndef IIC_H_
#define IIC_H_

#define IIC_READ 1
#define IIC_WRITE 0

#define TRANSMIT 1
#define RECEIVE 0

#define TRUE 1
#define FALSE 0

#define ACK_ENABLE 0
#define ACK_DISABLE 1

#define NO_ERROR 0
#define BUS_BUSY_TIME_OUT 1
#define NO_ACK 2
#define NO_RESPONSE 3

#define IIC_DATA I2C1_D
#define IIC_BUS_BUSY ((I2C1_S & I2C_S_BUSY_MASK)>>I2C_S_BUSY_SHIFT)

extern unsigned char iic_address;

unsigned char Tx_IIC_Master(unsigned char *data_ptr,unsigned char data_count,unsigned char slave_address);
unsigned char Rx_IIC_Master(unsigned char *data_ptr,unsigned char data_count,unsigned char slave_address);
unsigned char check_iic_device(unsigned char slave_address,unsigned char mode);
unsigned char Tx_IIC_Byte(unsigned char data);
unsigned char Rx_IIC_Byte(unsigned char *data);
unsigned char check_iic_line_busy(void);
unsigned char tx_iic_address(unsigned char slave_address,unsigned char mode);
unsigned char wait_for_iic_bus_response(void);
unsigned char check_iic_ack(void);
void Switch_Tx_Mode(void);
void Switch_Rx_Mode(void);
void Start_IIC_Comm(void);
void Stop_IIC_Comm(void);
void Enable_IIC_ack(void);
void Disable_IIC_ack(void);
void init_iic(unsigned char bit_rate);
unsigned char check_response(void);
void disable_iic(void);
void display_iic_error(unsigned char error);

#endif /* IIC_H_ */

#I NCLUDE " DERI VATI VE. H
#I NCLUDE " I I C. H"
#I NCLUDE " I I C. H"
#I NCLUDE " LCD. H"

header file to be included for i2c driver

unsigned char iic_address=0xD0;
Set the slave address
Slave:rtc
Address =0xDO


Device Description-Features
4
RTC device used is DS1307.
Resolution of the device=1 seconds.
The contents of the registers are in the BCD format.
Device slave address= 0xD0 h.
Interface : Inter Integrated circuit Communication (I2C).
Maximum SCL clock frequency=100 kHz.
Power supply ratings : Vcc=5V (atleast1.2xVBAT).
VBAT=3V.
Crystal frequency = 32.768kHz.
Temperature Range : Commercial 0C to +70C .
Industrial -40C to +85C.
CHECK FOR I2C INITIALISATION
void display_iic_error(unsigned char error)
{
switch(error)
{
case NO_ERROR :LCD_DisplayString(ROW2,COL1,CONST"IIC MSG:NO
ERROR");
break;
case BUS_BUSY_TIME_OUT :LCD_DisplayString(ROW2,COL1,CONST"IIC
MSG:BUS BUSY");
break;
case NO_ACK :LCD_DisplayString(ROW2,COL1,CONST"IIC MSG:NO
ACK");
break;
case NO_RESPONSE :LCD_DisplayString(ROW2,COL1,CONST"IIC MSG:NO
RESP");
break;
default :LCD_DisplayString(ROW2,COL1,CONST"UNKOWN IIC
ERROR");
}
}


void init_iic(unsigned char bit_rate)
SIM_SCGC4 |= SIM_SCGC4_I2C1_MASK

Set the clock to i2c module
PORTC_PCR10 = PORT_PCR_MUX(2);


PORTC_PCR11 = PORT_PCR_MUX(2);

Portc_pcr10 and portc_pcr11

GPIO pin is configured to I2C function

I2C1_F = 0x60;
I2C1_C1 = I2C_C1_IICEN_MASK;
>>> #define I2C_C1_IICEN_MASK 0x80u <<<<
ENABLE IICEN bit
// IIC IS enabled
while((I2C1_S &I2C_S_BUSY_MASK) == 0x20)
{
I2C1_C1&=(~I2C_C1_IICEN_MASK);
delay(10);
;
I2C1_C1|=(I2C_C1_IICEN_MASK);
}
>>#define I2C_S_BUSY_MASK 0x20u
Whie (bus busy)
{
disable iic till bus is idle
}
I2C1_S|=(I2C_S_ARBL_MASK):

>>>#define I2C_S_ARBL_MASK 0x10u
Clear arbitration lost bit (by writing one to it )
master is transmitter

unsigned char Tx_IIC_Master(unsigned char *data_ptr,unsigned char
data_count,unsigned char slave_address)
{
unsigned char error=NO_ERROR;
unsigned int byte_count=0;
error=check_iic_device(slave_address,IIC_WRITE);
if(error!=NO_ERROR)
{
return(error);
}







Unsigned char Rx_IIC_Master(unsigned char *data_ptr,unsigned char data_count,unsigned char slave_address)

{
unsigned char error=NO_ERROR;
unsigned int byte_count=0;
error=check_iic_device(slave_address,IIC_READ);
if(error!=NO_ERROR)
{
return(error);
}
{
Enable_IIC_ack();
}
Switch_Rx_Mode();
for(byte_count=0;byte_count<data_count;byte_count++)
{
if(byte_count==data_count-1)
{
}
error=Rx_IIC_Byte(data_ptr++);
if(error!=NO_ERROR)
{
return(error);
}
}
Stop_IIC_Comm();
return(error);
}

CHECK
unsigned char check_iic_device(unsigned char
slave_address,unsigned char mode)
{
unsigned char error=NO_ERROR;
Stop_IIC_Comm();
error=check_iic_line_busy();
while(((I2C1_S & I2C_S_IICIF_MASK )>>1) == TRUE)
{
I2C1_S |= I2C_S_IICIF_MASK;
}
if(error!=NO_ERROR)
{
return(error);
}
Switch_Tx_Mode();
Start_IIC_Comm();
error=Tx_IIC_Byte(slave_address|mode);
return(error);
}













Checks for interrupt flag ,
if no interrupt is pending ,clear IICIF by writing one to it
Byte transmit function
unsigned char Tx_IIC_Byte(unsigned char data)
{
unsigned char error=NO_ERROR;
IIC_DATA=data;
while((I2C1_S & I2C_S_TCF_MASK) !=I2C_S_TCF_MASK );
//wait till tx to complete
error=wait_for_iic_bus_response();
if(error!=NO_ERROR)
{
return(error);
}
if(check_iic_ack() != TRUE)
{
error= NO_ACK;
}
return(error);
}




Receive byte function
unsigned char Rx_IIC_Byte(unsigned char *data)
{
unsigned char error=NO_ERROR;
while((I2C1_S & I2C_S_TCF_MASK)
!=I2C_S_TCF_MASK ); //wait till tx to complete
error=wait_for_iic_bus_response();
if(error!=NO_ERROR)
{
return(error);
}
*data=IIC_DATA;
return(error);
}
Check i2c busy function
unsigned char check__iic_line_busy(void)
{
unsigned char error=NO_ERROR;
unsigned int i=0xffff;
//start_timer(1000);
while(IIC_BUS_BUSY == TRUE && i >=0 )
{
I2C1_C1&=(~I2C_C1_IICEN_MASK);
if(i==1)
{
error=BUS_BUSY_TIME_OUT;
break;
}
i--;
I2C1_C1|=(I2C_C1_IICEN_MASK);
}
return(error);
}
Wait for i2c response for bus free
unsigned char wait_for_iic_bus_response(void)
{
unsigned char error=NO_ERROR;
unsigned int i=0xffff;
while(((I2C1_S & I2C_S_IICIF_MASK )>>1) !=TRUE)
{
if(i== 1)
{
error=NO_RESPONSE;
break;
}
i--;
}
I2C1_S |= I2C_S_IICIF_MASK;
return(error);
}
Get i2c acknowledgment
unsigned char check_iic_ack(void)
{
return(!(I2C1_S & I2C_S_RXAK_MASK) );
}
>> #define I2C_S_RXAK_MASK 0x1u
Switchto tx mode
void Switch_Tx_Mode(void)
{
I2C1_C1|=(I2C_C1_TX_MASK);
}
<<<#define I2C_C1_TX_MASK 0x10u>>>
Switch to rx mode
void Switch_Rx_Mode(void)
{
unsigned char dummy=0;
I2C1_C1&=(~I2C_C1_TX_MASK);
dummy=IIC_DATA;
}
Start I2C communication
void Start_IIC_Comm(void)
{
while((I2C1_S &I2C_S_BUSY_MASK) == 0x20)
{
I2C1_C1&=(~I2C_C1_IICEN_MASK);
delay(10);
I2C1_C1|=(I2C_C1_IICEN_MASK);
}
I2C1_C1 |= 0x10;
I2C1_C1 |= I2C_C1_MST_MASK;
}

Stop i2c communication
void Stop_IIC_Comm(void)
{
I2C1_C1 &= ~I2C_C1_MST_MASK;
I2C1_C1 &= ~I2C_C1_TX_MASK;
}
Enable i2c acknowledgment
void Enable_IIC_ack(void)
{
I2C1_C1|=I2C_C1_TXAK_MASK ;
}

You might also like