Вы находитесь на странице: 1из 6

/******************************* ESA I2C INTERFACE

*************************************************

OBJECT : To Demonstrate the working of I2C EEPROM Interface.

CONNECTIONS : Connect the interface module to 26 pin FRC connector of MCB


2140 Board.

OUTPUT : Writes the data in to EEPROM which is placed in


Send_Data[] array.

Reads the data from EEPROM and displays it on DU3&4


seven segments
of I2C interface.

// SIRI 11.09 //
***********************************************************************************
**************/

#include <LPC214X.H>

void I2CWrite2402(unsigned char, unsigned char, unsigned char);


unsigned char I2CRead2402(unsigned char, unsigned char);
void LED_display(unsigned char, unsigned char);
void delay(void);
void Set_Address(void);
unsigned char Find_Address(unsigned char);

unsigned char U1,U2,U3,U4;


unsigned char Recv_Data[8],
Send_Data[8] = {0xAB,0x11,0x45,0x67,0x89,0xAB,0xCD,0xEF};

/* Lookup table for display code of 0 to F on LED */


unsigned char lookup[0x10] = { 0xC0,0xF9,0xA4,0xB0,0x99,
0x92,0x82,0xF8,0x80,0x90,
0x88,0x83,0xC6,0xA1,0x86,0x8E};

main()
{

unsigned char i,k;

PINSEL0 = 0x00000050; /* Select I2C0 by selecting


SCL0 & SDA0 */
I2C0SCLH = 0xC0; /* Set bit rate PCLK/(I2CSCLH
+ I2CSCLL) */
I2C0SCLL = 0xC0;

Set_Address(); /* Set the Addresses of


I2C 7 Segments */

while(1)
{

LED_display(U1,0xFF); /* Clear all 7 segments */


LED_display(U2,0xFF);
LED_display(U3,0xFF);
LED_display(U4,0xFF);

I2CWrite2402(0xA0,0x00,0x08); /* Write data into EEPROM from 0x00


address */

for(i=0x00; i<0x08; i++)


Recv_Data[i] = I2CRead2402(0xA1,i); /* Read data from EEPROM
from 0x00 address */

for(i=0x00; i<0x08; i++)


{
k = Recv_Data[i] & 0x000F ;
LED_display(U4,lookup[k]); /* Display the data */
k = (Recv_Data[i] & 0x00F0) >> 4 ;
LED_display(U3,lookup[k]);
k = (Recv_Data[i+2] & 0x0F00) >> 8 ;
LED_display(U2,lookup[k]);
k = (Recv_Data[i+2] & 0xF000)>> 0xC ;
LED_display(U1,lookup[k]);
delay();
}
}

void Set_Address()
{

U1 = Find_Address(0x40); /* Find Address of U1 */


U2 = Find_Address(0x42); /* Find Address of U2 */
U3 = Find_Address(0x44); /* Find Address of U3 */
U4 = Find_Address(0x46); /* Find Address of U4 */

unsigned char Find_Address(unsigned char Address)


{

unsigned long i;

I2C0CONSET = 0x40; /* Enable I2C interface


in Master mode */
I2C0CONSET = 0x20; /* Set STA bit */

while((I2C0STAT != 0x08)); /* Check status */


I2C0CONCLR = 0x20; /* Clear STA bit */

I2C0DAT = Address; /* Slave address + Write


signal */
I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */

for(i=0; i<0x10000; i++)


{
if(I2C0STAT == 0x18) /* Check status */
goto find;
}

I2C0CONSET = 0x10; /* Set STO bit */


I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */
I2C0CONCLR = 0x40; /* Disable I2C interface
*/

I2C0CONSET = 0x40; /* Enable I2C interface


in Master mode */
I2C0CONSET = 0x20; /* Set STA bit */

while((I2C0STAT != 0x08)); /* Check status */


I2C0CONCLR = 0x20; /* Clear STA bit */

Address = Address + 0x30;

I2C0DAT = Address; /* Slave address + Write


signal */
I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */

for(i=0; i<0x10000; i++)


{
if(I2C0STAT == 0x18) /* Check status */
goto find;
}

find:
I2C0CONSET = 0x10; /* Set STO bit */
I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */
I2C0CONCLR = 0x40; /* Disable I2C interface
*/

return(Address);

void I2CTransferByte(unsigned char Slv_Addr, unsigned char Data)


{
I2C0CONSET = 0x40; /* Enable I2C interface
in Master mode */
I2C0CONSET = 0x20; /* Set STA bit */

while((I2C0STAT != 0x08)); /* Check status */


I2C0CONCLR = 0x20; /* Clear STA bit */

I2C0DAT = Slv_Addr; /* Slave address + Write


signal */
I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */

while((I2C0STAT != 0x18)); /* Check status */

I2C0DAT = Data; /* data */


I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */
while((I2C0STAT != 0x28)); /* Check status */

I2C0CONSET = 0x10; /* Set STO bit */


I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */
I2C0CONCLR = 0x40; /* Disable I2C interface
*/

void I2CWrite2402(unsigned char Slv_Addr, unsigned char wrd_adrs,unsigned char


no_locations)
{
unsigned char k;

I2C0CONSET = 0x40; /* Enable I2C interface


in Master mode */
I2C0CONSET = 0x20; /* Set STA bit */
while(I2C0STAT != 0x08); /* Check status */
I2C0CONCLR = 0x20; /* Clear STA bit */

I2C0DAT = Slv_Addr; /* Slave address + Write


signal */
I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */
while(I2C0STAT != 0x18); /* Check status */

I2C0DAT = wrd_adrs; /* Word address */


I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */
while(I2C0STAT != 0x28); /* Check status */

for(k=0x00; k < no_locations; k++)


{
I2C0DAT = Send_Data[k]; /* data */
I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */
while(I2C0STAT != 0x28); /* Check status */
}

I2C0CONSET = 0x10; /* Set STO bit */


I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */
delay();
I2C0CONCLR = 0x40; /* Disable I2C interface
*/

unsigned char I2CRead2402(unsigned char Slv_Addr,unsigned char word_adrs)


{
unsigned char data;

I2C0CONSET = 0x40; /* Enable I2C interface


in Master mode */
I2C0CONSET = 0x20; /* Set STA bit */

while(I2C0STAT != 0x08); /* Check status */


I2C0CONCLR = 0x20; /* Clear STA bit */
I2C0DAT = 0xA0; /* Slave address + Write
signal */
I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */

while(I2C0STAT != 0x18);

I2C0DAT = word_adrs; /* word address */


I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */

while(I2C0STAT != 0x28);

I2C0CONCLR = 0x40; /* Disable I2C interface


*/
I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */

I2C0CONSET = 0x40; /* Enable I2C interface


in Master mode */
I2C0CONSET = 0x20; /* Set STA bit */

while(I2C0STAT != 0x08); /* Check status */

I2C0CONCLR = 0x20; /* Clear STA bit */

I2C0DAT = Slv_Addr; /* Slave address + Read


signal */
I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */

while(I2C0STAT != 0x40);
I2C0CONSET = 0x04;

I2C0CONCLR = 0x08;
while(I2C0STAT != 0x50);
data = I2C0DAT ; /* Data from EEPROM */

I2C0CONCLR = 0x04;

I2C0CONSET = 0x10; /* Set STO bit */


I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */
I2C0CONCLR = 0x40; /* Disable I2C interface
*/

return(data);
}

void LED_display(unsigned char Slv_Addr, unsigned char Data)


{
I2C0CONSET = 0x40; /* Enable I2C interface
in Master mode */
I2C0CONSET = 0x20; /* Set STA bit */

while((I2C0STAT != 0x08)); /* Check status */


I2C0CONCLR = 0x20; /* Clear STA bit */
I2C0DAT = Slv_Addr; /* Slave address + Write
signal */
I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */

while((I2C0STAT != 0x18)); /* Check status */

I2C0DAT = Data; /* data */


I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */

while((I2C0STAT != 0x28)); /* Check status */

I2C0CONSET = 0x10; /* Set STO bit */


I2C0CONCLR = 0x08; /* Clear I2C interrupt
bit */
I2C0CONCLR = 0x40; /* Disable I2C interface
*/

void delay() /* delay routine */


{
unsigned int i,j;

for(j=0; j<0x300; j++)


for(i=0x00; i<=9999; i++);
}

Вам также может понравиться