I2C using LPC2148 (ARM):
Program for I2C:
#include<lpc214x.h>
#define I2EN 6
#define STA 5
#define STO 4
#define SI 3
void delay(int time)
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<100;j++);
}
void txstring(char *str)
{
while(*str)
{
U0THR=*str;
while(!(U0LSR & (1<<6)));
str++;
delay(1000);
}
}
void i2c_address()
{
I2C0DAT = 0x40;
I2C0CONCLR = 1<<SI;
while(!(I2C0CONSET & (1<<SI)));
switch(I2C0STAT)
{
case 0x18:
txstring("\rSLA+W Has Been Sent With ACK\r");
break;
case 0x20:
txstring("\rSLA+W Has Been Sent With NACK\r");
break;
default:
txstring("\rInvalid Case\r");
}
}
void i2c_send(char val)
{
I2C0DAT = val;
I2C0CONCLR = 1<<SI;
while(!(I2C0CONSET & (1<<SI)));
switch(I2C0STAT)
{
case 0x28:
txstring("\rDATA Has Been Sent With ACK\r");
break;
case 0x30:
txstring("\rADD Has Been Sent With NACK\r");
break;
default:
txstring("\rInvalid Case\r");
}
}
void i2c_data()
{
i2c_send(0xAA);
delay(1000);
i2c_send(0x55);
delay(1000);
i2c_send(0x00);
delay(1000);
}
void pll_init()
{
PLL0CON = 0x01;
PLL0CFG = 0x24;
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
while(!(PLL0STAT & (1<<10)));
PLL0CON = 0x03;
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
VPBDIV = 0x00;
}
void uart0_init()
{
U0LCR = 0x83;
U0DLL = 97;//0x61
U0DLM = 0;
U0LCR = 0x03;
}
void i2c_init()
{
I2C0CONSET = 1<<I2EN;
I2C0SCLL = 19;
I2C0SCLH = 18;
}
void i2c_start()
{
I2C0CONSET = 1<<STA;
while(!(I2C0CONSET & (1<<SI)));
switch(I2C0STAT)
{
case 0x08:
txstring("\rStart Condition Generated\r");
break;
default:
txstring("\rError In Start\r");
}
}
void i2c_stop()
{
I2C0CONSET = 1<<STO;
while(!(I2C0CONSET & (1<<SI)));
if(I2C0CONSET & (1<<SI))
{
txstring("\rSTOP Condition Has Been Transmitted\r");
}
else
{
txstring("\rError in Stop\r");
}
}
int main()
{
PINSEL0 = 1<<4 | 1<<6 | 1<<0;
pll_init();
uart0_init();
i2c_init();
i2c_start();
i2c_address();
while(1)
{
i2c_data();
}
i2c_stop();
}
Program for I2C:
#include<lpc214x.h>
#define I2EN 6
#define STA 5
#define STO 4
#define SI 3
void delay(int time)
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<100;j++);
}
void txstring(char *str)
{
while(*str)
{
U0THR=*str;
while(!(U0LSR & (1<<6)));
str++;
delay(1000);
}
}
void i2c_address()
{
I2C0DAT = 0x40;
I2C0CONCLR = 1<<SI;
while(!(I2C0CONSET & (1<<SI)));
switch(I2C0STAT)
{
case 0x18:
txstring("\rSLA+W Has Been Sent With ACK\r");
break;
case 0x20:
txstring("\rSLA+W Has Been Sent With NACK\r");
break;
default:
txstring("\rInvalid Case\r");
}
}
void i2c_send(char val)
{
I2C0DAT = val;
I2C0CONCLR = 1<<SI;
while(!(I2C0CONSET & (1<<SI)));
switch(I2C0STAT)
{
case 0x28:
txstring("\rDATA Has Been Sent With ACK\r");
break;
case 0x30:
txstring("\rADD Has Been Sent With NACK\r");
break;
default:
txstring("\rInvalid Case\r");
}
}
void i2c_data()
{
i2c_send(0xAA);
delay(1000);
i2c_send(0x55);
delay(1000);
i2c_send(0x00);
delay(1000);
}
void pll_init()
{
PLL0CON = 0x01;
PLL0CFG = 0x24;
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
while(!(PLL0STAT & (1<<10)));
PLL0CON = 0x03;
PLL0FEED = 0xAA;
PLL0FEED = 0x55;
VPBDIV = 0x00;
}
void uart0_init()
{
U0LCR = 0x83;
U0DLL = 97;//0x61
U0DLM = 0;
U0LCR = 0x03;
}
void i2c_init()
{
I2C0CONSET = 1<<I2EN;
I2C0SCLL = 19;
I2C0SCLH = 18;
}
void i2c_start()
{
I2C0CONSET = 1<<STA;
while(!(I2C0CONSET & (1<<SI)));
switch(I2C0STAT)
{
case 0x08:
txstring("\rStart Condition Generated\r");
break;
default:
txstring("\rError In Start\r");
}
}
void i2c_stop()
{
I2C0CONSET = 1<<STO;
while(!(I2C0CONSET & (1<<SI)));
if(I2C0CONSET & (1<<SI))
{
txstring("\rSTOP Condition Has Been Transmitted\r");
}
else
{
txstring("\rError in Stop\r");
}
}
int main()
{
PINSEL0 = 1<<4 | 1<<6 | 1<<0;
pll_init();
uart0_init();
i2c_init();
i2c_start();
i2c_address();
while(1)
{
i2c_data();
}
i2c_stop();
}
0 comments:
Post a Comment
if you have any doubt please let me know