Difference between revisions of "PIC32MX: I2C EEPROM"

From Mech
Jump to navigationJump to search
Line 43: Line 43:
int main(void)
int main(void)
{
{
char LCDbuffer[33]; // this will store the string for the LCD
char LCDbuffer[33]; // this will store the string for the LCD
//unsigned char test[28]="Hello world\nI2C is working!";
//unsigned char test[28]="Hello world\nI2C is working!";
unsigned char test[13]="Hello world!";
unsigned char test[13]="Hello world!";
int size = sizeof(test);
int size = sizeof(test);
Line 50: Line 50:
//Initialize LCD
//Initialize LCD
lcd_init();
lcd_init();
//Enable channel
//Enable channel
OpenI2C1( I2C_EN, BRG_VAL );
OpenI2C1( I2C_EN, BRG_VAL );
SlaveAddress = 0x50; //0b1010(B0)(A0)(A1) 24AA1025 EEPROM address
SlaveAddress = 0x50; //0b1010(B0)(A0)(A1) 24AA1025 EEPROM address
sendString(SlaveAddress, 0x0540, test, size);
sendString(SlaveAddress, 0x0540, test, size);
receiveString(SlaveAddress, 0x0540, readtest, size);
receiveString(SlaveAddress, 0x0540, readtest, size);
//Print to LCD screen
//Print to LCD screen
if(strcmp(readtest, test)==0) sprintf(LCDbuffer, "\fTrue"); // make the string
if(strcmp(readtest, test)==0) sprintf(LCDbuffer, "\fTrue"); // make the string
else sprintf(LCDbuffer, "\fFalse");
else sprintf(LCDbuffer, "\fFalse");
putsLCD(LCDbuffer); // write the contents of the variable
putsLCD(LCDbuffer); // write the contents of the variable
StopI2C1(); //Send the Stop condition
StopI2C1(); //Send the Stop condition
IdleI2C1(); //Wait to complete
IdleI2C1(); //Wait to complete
CloseI2C1();
CloseI2C1();
while(1)
while(1)
{}
{}
}
}

Revision as of 11:51, 16 February 2010

Original Assignment

Do not erase this section!

Your assignment is to interface to the I2C 24AA1025 EEPROM chip.

Overview

You can use the PIC32 to communicate with an EEPROM chip using I2C. The EEPROM chip can store data sent from the PIC and be retrieved at a later time (like an external hardrive).

To test that the code data was writing and reading correctly, the attached code sends data to the EEPROM through I2C, then from the EEPROM back to the PIC, and out to a LED display screen.

Circuit

Connection schematic for 24AA1025 Chip.

I2C.jpg

For visual feedback the PIC32 was connected to and LCD screen(1602A1 USB-A/Mini-B5-06):

LCD.jpg

Code

         /**************************************************************
         * I2C EEPROM using LCD screen as output display
         *
         **************************************************************/
         /*Includes****************************************************/
          #include <string.h> // used for string compare to verify data transmission
          #include <plib.h>
          #include <HardwareProfile.h>
          #include <LCD.h> // used to display for debugging
          #include <I2C_EEPROM.h>
         /*Definitions*************************************************/
          #define BAUD_RATE		100000
          #define BRG_VAL 		((SYS_FREQ/2/BAUD_RATE)-2)
         /*Global Variables *******************************************/
          unsigned char SlaveAddress = 0x50;
          short int addressCounter = 0x0000;
         /*Main Function***********************************************/
          int main(void)
            {
	       char LCDbuffer[33]; // this will store the string for the LCD
	       //unsigned char test[28]="Hello world\nI2C is working!";
	       unsigned char test[13]="Hello world!";
	       int size = sizeof(test);
	       unsigned char readtest[size];
	
	       //Initialize LCD
	       lcd_init();

	       //Enable channel
	       OpenI2C1( I2C_EN, BRG_VAL );

	       SlaveAddress = 0x50;	//0b1010(B0)(A0)(A1) 24AA1025 EEPROM address

	       sendString(SlaveAddress, 0x0540, test, size);

	       receiveString(SlaveAddress, 0x0540, readtest, size);

	       //Print to LCD screen
	       if(strcmp(readtest, test)==0) sprintf(LCDbuffer, "\fTrue"); // make the string 
	       else sprintf(LCDbuffer, "\fFalse");
	       putsLCD(LCDbuffer);  // write the contents of the variable

	       StopI2C1();	//Send the Stop condition
	       IdleI2C1();	//Wait to complete

	       CloseI2C1();

	       while(1)
	       {}

             }

Further Reading

PIC32MX:_I2C_DAC