Trying I2C on LPC2148

Started by lucky_suhas October 3, 2011
Hi, i'm trying to transmit and receive using the I2C. using a NGX
blueboard LPC2148. Using the I2C in master mode. What i wanted to try
was read the data from the pot for ADC. store it in the EPROM using I2C.
after 10 values are stored. i want to read the stored values using I2C
and output it on the UART. so that i can see it on my Hyper Terminal. i
have written the code. i loaded it on to the board but i am not getting
any kind of output(not even garbage values). can anybody have a look at
it and tell me what i have done wrong?

i am going to paste the whole code as is.

the code:

#include
#include
#include "adc.h"
int read;
void init_adc0(void)
{
PINSEL0 = PINSEL0|0x10000000;
}

unsigned short adc_read(void)
{
int i=0;
AD0CR=0x00200000 & (1<<3)|(1<<24);

do
{
i = AD0GDR; // Read A/D Data Register
} while ((i & 0x80000000) == 0); // Wait for end of A/D

return ((i>>6) & 0x3FF);
}

/*I2C setup*/
/* I2C states*/

/* Start condition transmitted */

void ISR_8()
{
/* Port Indicator */
IO0CLR=0x10;
/* Slave address + write */
I2C0DAT=0xA0;
/* Clear SI and Start flag */
I2C0CONCLR=0x28;
/* Port Indicator */
IO0SET=0x10;
}

/* Acknowledgement received from slave for slave address */

void ISR_18()
{
/* Port Indicator */
IO0CLR=0x20;
/* Data to be transmitted */
I2C0DATc_read();
/* clear SI */
I2C0CONCLR=0x8;
/* Port Indicator */
IO0SET=0x20;
}
/* Acknowledgement received from slave for byte transmitted from master.
Stop
condition is transmitted in this state signaling the end of transmission
*/

void ISR_28()
{
/* Port Indicator */
IO0CLR=0x80;
/* Transmit stop condition */
I2C0CONSET=0x10;
/* clear SI */
I2C0CONCLR=0x8;
/* Port Indicator */
IO0SET=0x80;
}

__irq void ISR_I2Ct(void)
{

int temp=0;
temp=I2C0STAT;
switch(temp)
{
case 8:
ISR_8();
break;
case 18:
ISR_18();
break;
case 28:
ISR_28();
break;
default :
break;
}

VICVectAddr=0xFF;
}
void ISR_8r()
{
/* Port Indicator */
// IO0CLR=0x10;
/* Slave address + write */
I2C0DAT=0xA1;
/* Clear SI and Start flag */
I2C0CONCLR=0x28;
/* Port Indicator */
// IO0SET=0x10;
}

void ISR_40r()
{
/*data is received*/
read=I2C0DAT;
/*SI is cleared*/
I2C0CONCLR=0x8;
printf("%d/n",read);
}

void ISR_58r()
{
/* Transmit stop condition */
I2C0CONSET=0x10;
/* clear SI */
I2C0CONCLR=0x8;
}

__irq void ISR_I2Cr(void)
{

int temp=0;
temp=I2C0STAT;
switch(temp)
{
case 8:
ISR_8r();
break;
case 40:
ISR_40r();
break;
case 58:
ISR_58r();
break;
default :
break;
}

VICVectAddr=0xFF;
}

void intialize_w(void)
{
PINSEL0=0x00000050;
IO0DIR=0x80000000;

I2C0CONCLR=0x6c; /* clearing all flags */
I2C0CONSET=0x40; /* enabling I2C */
I2C0SCLH=0x10; /* 100 KHz */
I2C0SCLL=0x10;

/* interrupt setup*/
VICIntSelect=0x0;
VICIntEnable=0x200;
VICVectCntl0=0x20;
VICVectAddr0=(unsigned long) &ISR_I2Ct;
}

void intialize_r(void)
{
PINSEL0=0x00000050;
IO0DIR=0x80000000;

I2C0CONCLR=0x6c; /* clearing all flags */
I2C0CONSET=0x40; /* enabling I2C */
I2C0SCLH=0x10; /* 100 KHz */
I2C0SCLL=0x10;

/* interrupt setup*/
VICIntSelect=0x0;
VICIntEnable=0x200;
VICVectCntl0=0x20;
VICVectAddr0=(unsigned long) &ISR_I2Cr;
}

int main(void)
{
int i;
init_adc0();
intialize_w();
for(i=0;i<10;i++)
{
I2C0CONSET=0x60;
}
PINSEL0 = 0x00050000; /* Enable RxD1 and TxD1
*/
U1LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit
*/
U1DLL = 97; /* 9600 Baud Rate @ 15MHz VPB Clock
*/
U1LCR = 0x03; /* DLAB = 0
*/
intialize_r();
while(1)
{
i;
while(i)
{
I2C0CONSET=0x60;
i--;
}
}
}
Thank you for your time!



An Engineer's Guide to the LPC2100 Series