#include<lpc214x.h>
void ser_init(void);
void tx(unsigned char c);
unsigned char rx(void);
void tx_string(unsigned char *s);
void i2c_init(void);
void write(unsigned char data,unsigned char addr);
unsigned char read(unsigned char addr);
int main()
{
unsigned char temp;
ser_init();
i2c_init();
tx_string("Writing\n\r");
write('A',0x00);
tx_string("Write successfully...\n\r");
tx_string("Reading...\n\r");
temp=read(0x00);
tx_string("Received data is : ");
tx(temp);
while(1);
}
void i2c_init()
{
PINSEL0|=0x50;
I2C0SCLH=75;
I2C0SCLL=75;
}
void write(unsigned char data,unsigned char addr)
{
unsigned char addL,addH;
addL=addr&0xff;
addH=(addr&0xff) >>8;
I2C0CONCLR=0x000000FF; //Clear all I2C settings
I2C0CONSET=1<<6;//I2EN=1
I2C0CONSET=1<<5;//Start
while(I2C0STAT != 0x08);
I2C0CONCLR=(1<<5)|(1<<3);//Clear Start and Interrupt
I2C0DAT = 0xA0;
I2C0CONSET=1<<2;
I2C0CONCLR=(1<<3);//Clear Interrupt
while(I2C0STAT != 0x18);
I2C0DAT = addH;
I2C0CONCLR=(1<<3);//Clear Interrupt
while(I2C0STAT != 0x28);
I2C0DAT = addL;
I2C0CONCLR=(1<<3);//Clear Interrupt
while(I2C0STAT != 0x28);
I2C0DAT = data;
I2C0CONCLR=(1<<3);//Clear Interrupt
while(I2C0STAT != 0x28);
I2C0CONCLR=(1<<3);//Clear Interrupt
I2C0CONSET=1<<4;//Stop
}
unsigned char read(unsigned char addr)
{
unsigned char read=0,addL,addH;
addL=addr&0xff;
addH=(addr&0xff00)>>8;
I2C0CONCLR=0x000000FF; //Clear all I2C settings
I2C0CONSET=1<<6;//I2EN=1
I2C0CONSET=1<<5;//Start
while(I2C0STAT != 0x08);
I2C0CONCLR=(1<<5)|(1<<3);//Clear Start and Interrupt
I2C0ADR = 0xA0;
I2C0DAT = 0xA0;
I2C0CONCLR=(1<<3);//Clear Interrupt
while(I2C0STAT != 0x18);
I2C0DAT = addH;
I2C0CONCLR=(1<<3);//Clear Interrupt
while(I2C0STAT != 0x28);
I2C0DAT = addL;
I2C0CONCLR=(1<<3);//Clear Interrupt
while(I2C0STAT != 0x28);
I2C0CONSET=(1<<2)|(1<<5); //Start
I2C0CONCLR=(1<<3);//Clear Interrupt
while(I2C0STAT != 0x10);
I2C0ADR = 0xA1;
I2C0DAT = 0xA1;
I2C0CONCLR=(1<<3) | (1<<5);//Clear Interrupt , Start
while(I2C0STAT != 0x40);
I2C0CONCLR=(1<<3);//Clear Interrupt
while(I2C0STAT != 0x50);
read=I2C0DAT;
I2C0CONCLR=(1<<3);//Clear Interrupt
I2C0CONCLR=(1<<3);//Clear Interrupt
I2C0CONSET=1<<4;
return read;
}
void ser_init()
{
VPBDIV=0x02;
PINSEL0|=0x05;
U0LCR=0x83;
U0DLL=195;
U0DLM=0;
U0LCR=0x03;
U0TER=(1<<7);
}
void tx(unsigned char c)
{
U0THR=c;
while((U0LSR&(1<<5))==0);
}
void tx_string(unsigned char *s)
{
while(*s)
{
tx(*s++);
}
}
unsigned char rx()
{
while((U0LSR&(1<<0))==0);
return U0RBR;
} |
#include<lpc214x.h>
#define delay for(i=0;i<100000;i++)
#define FALSE 0
#define TRUE 1
unsigned long int i;
void i2c_init(void);
unsigned char check_status(unsigned char status);
unsigned char i2c_write(unsigned char data,unsigned char status);
unsigned char i2c_read(void);
unsigned char send_byte(unsigned char data,unsigned char address);
unsigned char read_byte(unsigned char address);
void i2c_delay(void);
void ser_init(void);
void tx(unsigned char c);
unsigned char rx(void);
void tx_string(unsigned char *s);
int main(void)
{
ser_init();
tx_string("I2C INTERFACING\n\r");
send_byte('w',0x00);
delay;
tx_string("I2C Write\n\r");
delay;
tx(read_byte(0x01));
while(1);
}
void ser_init()
{
VPBDIV=0x02;
PINSEL0|=0x05;
U0LCR=0x83;
U0DLL=195;
U0DLM=0;
U0LCR=0x03;
U0TER=(1<<7);
}
void tx(unsigned char c)
{
U0THR=c;
while((U0LSR&(1<<5))==0);
}
void tx_string(unsigned char *s)
{
while(*s)
{
tx(*s++);
}
}
unsigned char rx()
{
while((U0LSR&(1<<0))==0);
return U0RBR;
}
void i2c_init(void)
{
PCONP|= 0x00000080;// Power on I2C0 peripheral
PINSEL0 |= 0x50;/*Initialize Pin Connect Block P0.2 as SCL0 P0.3 as SDA0*/
I2C0CONCLR = 0x6C;/*Clear AA, I2C Interrupt Flag, Start Bit*/
I2C0CONSET = 0x40;/*Enable I2C0*/
/*I2C0SCLH = 75;
I2C0SCLL = 75;*/
I2C0SCLH = 14745600/(2*50000);
I2C0SCLL = 14745600/(2*50000);
}
unsigned char check_status(unsigned char status)
{
unsigned long int time=0;
while(time<300000)
{
if(I2C0CONSET & 1<<3)
{
if (I2C0STAT == status)
{
return TRUE;
}
}
time++;
}
return FALSE;
}
unsigned char i2c_write(unsigned char data,unsigned char status)
{
I2C0DAT = data & 0xFF;;
I2C0CONCLR = 0X2C;
if(!check_status(0x08))/*Wait for the Status Set*/
return FALSE;
else
return TRUE;
}
unsigned char i2c_read(void)
{
I2C0CONCLR= 0x2C;// clear all except I2EN
if(!check_status(0x58))/* Wait for Status Set - 0x58 */
return FALSE;
i2c_delay();
return (char)I2C0DAT;
}
unsigned char send_byte(unsigned char data,unsigned char address)
{
//-------------------START--------------------------------------------
I2C0CONSET = 0x20;/*Set the Start Bit*/
if(!check_status(0x08))/*Wait for the Status Set*/
return FALSE;
//------------------SEND SLAVE ADDRESS---------------------------------
i2c_delay();
if(!i2c_write(0xA0,0x18))
return FALSE;
//------------------SEND ADDRESS--------------------------------------
i2c_delay();
if(!i2c_write(address,0x28))
return FALSE;
//------------------SEND DATA----------------------------------------
i2c_delay();
if(!i2c_write(data,0x28))
return FALSE;
//-------------------STOP---------------------------------------------
i2c_delay();
I2C0CONSET= 0x10;// generate stop condition
I2C0CONCLR= 0x2C;
return TRUE;
}
unsigned char read_byte(unsigned char address)
{
unsigned char read;
i2c_init();
//-------------------START--------------------------------------------
I2C0CONSET = 0x20;/*Set the Start Bit*/
if(!check_status(0x08))/*Wait for the Status Set*/
return FALSE;
//------------------SEND SLAVE ADDRESS---------------------------------
i2c_delay();
if(!i2c_write(0xA0,0x18))
return FALSE;
//------------------SEND ADDRESS--------------------------------------
i2c_delay();
if(!i2c_write(address,0x28))
return FALSE;
I2C0CONCLR= 0x08;// clear SI flag
I2C0CONSET= 0x10;// generate stop condition
//-------------------START--------------------------------------------
I2C0CONSET = 0x20;/*Set the Start Bit*/
if(!check_status(0x08))/*Wait for the Status Set*/
return FALSE;
//------------------SEND READ ADDRESS--------------------------------------
I2C0DAT = 0xA1;
I2C0CONCLR= 0x28;// clear all except I2EN and AA
if(!check_status(0x40))/*Wait for the Status Set*/
return FALSE;
//----------------------READ-------------------------------------------------
read = i2c_read();
//-------------------STOP---------------------------------------------
i2c_delay();
I2C0CONSET= 0x10;// generate stop condition
I2C0CONCLR= 0x2C;
return read;
}
void i2c_delay()
{
unsigned long int i2c_del;
for(i2c_del=0;i2c_del<=100000;i2c_del++);
}
|
Original Attachment has been moved to: code%201.rar