PIC32MX: I2C EEPROM

From Mech
Revision as of 12:20, 14 February 2010 by MeganWelker (talk | contribs) (→‎Code)
Jump to navigationJump to search

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****************************************************/

  1. include <string.h> // used for string compare to verify data transmission
  2. include <plib.h>
  3. include <HardwareProfile.h>
  4. include <LCD.h> // used to display for debugging
  5. include <I2C_EEPROM.h>

/*Definitions*************************************************/

  1. define BAUD_RATE 100000
  2. 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