Sie sind auf Seite 1von 17

#include<lpc214x.

h>
#include <stdio.h>

char addr;
void delay1(void );
void delay(void );
int atoh(int ch);
void serial_tx(int ch);
void string_tx(char *a);

void UARTInit();

//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/************************************ Macros
*******************************************/
#define UP

1<<20

#define INCR

1<<21

#define DECR

1<<22

#define MAX

10

#define

AA

#define

SI

#define

STO

#define

STA

#define

I2EN 6

//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Declarations
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void I2C_ISR(void)__irq;

void Wait (unsigned int);


void I2C_Init (void);

int I2C_Start (unsigned int Slave_Addr);

char ReLoad[MAX] =
{0x00/*Address Low Bits*/,0x00/*Address Low
Bits*/,'L','P','C','-','2','1','4','8'};
char Buff[MAX]
=
{0x00/*Address Low Bits*/,0x00/*Address Low
Bits*/,'L','P','C','-','2','1','4','8'};

unsigned char Rec[MAX] =

{"NO-DATA!"};

unsigned char index

0;

unsigned char flag = 0, ii, Ready=0;


unsigned char Erase = 0;

//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

void Delay(void)
{
unsigned int i,j;

for(i=0;i<150;i++)
for(j=0;j<900;j++);
}

void Wait (unsigned int Delay)


{
while(Delay--);
}

void I2C_Init (void)


{
PINSEL0
- SCL0

0x00050050;

I2C0SCLH

150;

// 50%duty cycle .....

I2C0SCLL
30MHz

150;

// I2C Frequency ->100 KHz for PCLK =

1 << I2EN;

// Enable I2C 0

I2C0CONSET =

int I2C_Start (unsigned int Slave_Addr)

// P0.3 - SDA0 and P0.2

{
I2C0CONCLR =

1 << STO;

I2C0CONSET =

1 << AA;

I2C0CONSET =

1 << STA;

return 0;
}

void delay1(void )
{
volatile int i,j;
for (i=0;i<5;i++)
for (j=0;j<50;j++);
}

void delay(void )
{
volatile int i,j;
for (i=0;i<100;i++)
for (j=0;j<500;j++);
}
int atoh(int ch)
{
if(ch<=0x09)
ch = ch + 0x30;

else
ch = ch + 0x37;
return(ch);
}
/****************** serial transmission routine ***************/
void serial_tx(int ch)
{

int i;

U1THR = ch;for(i=0;i<15000;i++);
}
void string_tx(char *a)
{

int i;
while(*a!='\0')
{
// while((U1LSR&0X20)!=0X20);
U1THR=*a;for(i=0;i<15000;i++);
a++;
}

int main(void)
{

int i;
PINSEL0 |= 0x00050050; // Enable rx,tx
U1LCR = 0x00000083; // 8 bit data,1 stop bit,no parity bit
U1DLM = 0x00;
U1DLL = 0x5D;//U1DLL = u1dl & 0xFF;
U1LCR = 0x00000003; // DLAB =0

VICIntSelect = 0<<9;
VICVectCntl0

= 0x020 | 9 ;

VICVectAddr0

= (unsigned long)I2C_ISR;

VICIntEnable

= 1<<9;

//Select Vector Slot for

I2C0

/*

Before the master transmitter mode


can be entered, the I2CONSET register must be initialized

*/

I2C_Init();
//UART0_PutS ("[2J\0");
// Clear Screen

string_tx ("********* ARM LPc2148 I2C EEPROM Demo **********\n\n\r");


string_tx("[~] Press UP Key to Write default data to EEPROM! \n\r");
string_tx ("[~] Press DOWN Key to Read and Display data from EEPROM!
\n\r");

string_tx ("[~] Press INCR ON Key Erase data from EEPROM \n\r");

while (1)
{
if ((IOPIN1 & UP) == 0)
Default Data to the EEPROM ...*/

/*...To Load the

{
ii = 0;
while (ii < MAX)
Default Data to Buff

//Re-load

{
Buff [ii]

ReLoad [ii];

ii++;
}
Wait (5000);
flag

'W';

I2C_Start (0x70);
Erase = 0;
for (i=0;i<30;i++) Wait(10000);

I2C0CONCLR =

1 << SI;

while (!(IOPIN1 & UP));


Wait (5000);Wait (5000);
}
else if ((IOPIN1 & INCR) == 0)
Data Stored in the EEPROM...*/
{

/*..To Read the

flag

'R';

I2C_Start (0x70);

for (i=0;i<30;i++) Wait(10000);

I2C0CONCLR =

1 << SI;

while (Ready == 'F');


//Ready = FALSE

if (Ready == 'T')

//Check if

Ready = TRUE
{
ii=0;
string_tx("\n\r The Read Data are: \t");
while (ii<MAX)
{
//U0THR

'\n';

Rec[ii];

Wait (1000);
U1THR
Wait (1000);
ii++;
}
serial_tx (0x0d);
serial_tx (0x0e);
Wait (1000);
Ready

'F';

}
while (!(IOPIN1 & INCR));
Wait (5000);Wait (5000);Wait (5000);Wait (5000);
}

if ((IOPIN1 & DECR) == 0)


Erase the Content in EEPROM*/

/*To

{
ii = 2;
while (ii < MAX)
{
Buff[ii]

0xFF;

0xFF to EEPROM
ii++;
}
flag

'W';

Erase = 1;
I2C_Start (0x70);

for (i=0;i<30;i++) Wait(10000);

I2C0CONCLR =

1 << SI;

while (!(IOPIN1 & DECR));


Wait (5000);Wait (5000);Wait (5000);Wait (5000);
}
}

//Load

void I2C_ISR(void) __irq


{
if (I2C0CONSET & 0x08)
{

switch (I2C0STAT)
{
case (0x08) :/*... A START condition has been Transmitted...*/

if (flag == 'W')
{
I2C0CONCLR =

1 << STO;

I2C0CONCLR =

1 << STA;

I2C0DAT

I2C0CONCLR =

1 << SI;

//Clear START Bit


0xA0;

//Slave Addr + W 1010 p2 p1 p0 w

}
else if (flag == 'R')
{
//I2C0CONCLR
STO;

1 <<

I2C0DAT

0xA0;

//Slave Addr + R 1010 p2 p1 p0 r


I2C_Start (0xA1);
I2C0CONCLR =

1 << SI;

}
index =

0;

break;

case (0x10) :

/*...A repeated START condition has been

Transmitted...*/

I2C0CONCLR =

1 << STA;

if (flag == 'W')
I2C0DAT

0xA0;

//Slave Addr + W 1010 p2 p1 p0 w


else if (flag == 'R')
{
I2C0CONCLR =

1 << STA;

I2C0CONCLR =

1 << STO;

I2C0DAT

I2C0CONCLR =

1 << SI;

0xA1;

//Slave Addr + R 1010 p2 p1 p0 r

index = 0;
}

break;

case (0x18) :

/*....SLA+W has been transmitted; ACK has

been received....*/

I2C0CONCLR =

0x20;

I2C0CONCLR =

0x38;

I2C0DAT

//Clear START Bit


//Clear all flags (STA, STO, SI)
Buff[index];

index++;

break;

case (0x20) :
has been received....*/

/*...SLA+W has been transmitted; NOT ACK

I2C0CONCLR =

0x20;

I2C0CONCLR =

0x38;

I2C0DAT

//Clear START Bit


//Clear all flags (STA, STO, SI)
Buff[index];

index++;
break;

case (0x28) :
ACK has been received....*/

/*...Data byte in I2DAT has been transmitted;

if (index < MAX)


// Place all the contents of Buff[] in I2C0DAT
{

if (flag == 'W')
{
I2C0DAT

I2C0CONCLR =

0x20;

I2C0CONCLR =

0x38;

Buff[index];
//Clear START Bit
//Clear all flags (STA, STO, SI)

}
'R')

else if ((index > 1) && flag ==


//Generate a Repeated Start to jump to MST/REC Mode
{
I2C0CONCLR =

0x18;

I2C0CONSET =

1 <<

STA;

}
else
// Send Upper Nibble of Adress to Read the Data...
{
I2C0DAT

I2C0CONCLR =

0x20;

I2C0CONCLR =

0x38;

Buff[index];
//Clear START Bit
//Clear all flags (STA, STO, SI)

index++;
}
else
//Stop Transmission to EEPROM.... Data Count Reset..
{
index = 0;
flag = 'R';
I2C0CONSET =

1 << STO;

//Send STOP Bit


if (Erase == 1)
{

string_tx ("\n\r Memory


Erase Successfull.....!\n");
}
else
{

string_tx ("\n\r Data


Successfully Written on Memory!\n");
}

}
break;

I2C0CONCLR =

1 << STA;

I2C0CONCLR =

1 << SI;

case (0x30) :
/*... Data byte in I2DAT has been
transmitted; NOT ACK has been received ...*/

I2C0CONCLR =

0x20;

//Clear START Bit


if (index < MAX)
{
if (flag == 'W')
{
I2C0DAT

Buff[index];

index++;
}
else
{
index = 0;
flag = 0;
I2C0CONSET =

0x10;

I2C0CONCLR =

1 << SI;

//Send STOP Bit

}
break;

case (0x38) :

/*... Arbitration lost in SLA+R/W or Data

bytes ....*/

I2C0CONSET =

0x20;

break;

case (0x40) :

/*... SLA+R has been transmitted; ACK has

been received.*/

I2C0CONSET =

1 << AA;

I2C0CONCLR =

1 << STA;

I2C0CONCLR =

1 << STO;

I2C0CONCLR =

1 << SI;

break;

case (0x48) :
has been received ...*/

/*...SLA+R has been transmitted; NOT ACK

I2C0CONSET =

1 << STA;

//Repeated Start
break;

case (0x50) :

/*... Data byte has been received; ACK has

been returned ....*/

I2C0CONSET =
if (index < MAX)
{

1 << AA;

Rec [index] =

I2C0DAT;

index++;

}
else
{
I2C0CONSET =

1 << STO;

I2C0CONCLR =

1 << SI;

index = 0;
Ready =

'T';

//Make Ready TRUE after Reading is over!


}
break;

case (0x58) :
has been returned ....*/

/*... Data byte has been received; NOT ACK

Rec [index] =

I2C0DAT;

I2C0CONCLR =

1 << STA;

I2C0CONCLR =

1 << STO;

break;

}
}

I2C0CONCLR =

1 << SI;

VICVectAddr =

0x00;

Das könnte Ihnen auch gefallen