A very Simple Program to enable you to start using the i2c with the 24AA02x 2K EEPROM using i2c Addressing. It's not all singing and dancing but features read write and erase.

Dependencies:   mbed

Committer:
martinsimpson
Date:
Tue Dec 16 09:13:45 2014 +0000
Revision:
0:a51016633386
First Published Program 24AA02 2K EEPROM using i2c Address

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martinsimpson 0:a51016633386 1 #include "mbed.h"
martinsimpson 0:a51016633386 2
martinsimpson 0:a51016633386 3 #define MCP24AA02_ADDR (0xA0) // 24AA02 2K EEPROM using i2c Address
martinsimpson 0:a51016633386 4 // for the 22AA025 variant this can be 0xA0,0xA2,0xA4,0xA6,0xA8,0xAA,0xAC,0xAE
martinsimpson 0:a51016633386 5 // since LSB is used to to control a WRITE(0) or a READ(1) Operation
martinsimpson 0:a51016633386 6 #define WRITE (0x00) // 24AA02 2K EEPROM using i2c Write bit
martinsimpson 0:a51016633386 7 #define READ (0x01) // 24AA02 2K EEPROM using i2c Read bit
martinsimpson 0:a51016633386 8 #define MCP24AA02_MID (0xFA) // Manufacturer ID Address (Read Only 0x29==Microchip)
martinsimpson 0:a51016633386 9 #define MCP24AA02_DID (0xFB) // Device ID Adress (Read Only 0x41==4 is i2c family and 1 is 2K device)
martinsimpson 0:a51016633386 10 // Information from Microchip Datasheet DS20005202A Part #24AA02UID/24AA025UID
martinsimpson 0:a51016633386 11 /* This is a very simple program to demonstrate the ease of use of the i2c bus utilising an EEPROM.
martinsimpson 0:a51016633386 12 Do not forget that Pull Up resistors are required on the SDA and SCL lines
martinsimpson 0:a51016633386 13 (Typically 2K ohms for 400KHz bus speeds)
martinsimpson 0:a51016633386 14 These are required since devices connected with the i2c will have 'open drain' (or open collector)
martinsimpson 0:a51016633386 15 circuitry to allow wire 'ORing' of their respective connections
martinsimpson 0:a51016633386 16 Used with a stm32-Nucleo-F401RE
martinsimpson 0:a51016633386 17 */
martinsimpson 0:a51016633386 18
martinsimpson 0:a51016633386 19 I2C i2c(I2C_SDA, I2C_SCL); //I2C Class Pin Assignments see I2C.h
martinsimpson 0:a51016633386 20
martinsimpson 0:a51016633386 21 Serial pc(SERIAL_TX, SERIAL_RX); //Serial Class Pin Assignments see Serial.h
martinsimpson 0:a51016633386 22
martinsimpson 0:a51016633386 23 void readID(int addy)
martinsimpson 0:a51016633386 24 {
martinsimpson 0:a51016633386 25 char ucdata_write[1];
martinsimpson 0:a51016633386 26 char ucdata_read[1];
martinsimpson 0:a51016633386 27 ucdata_write[0] = addy; //MCP24AA02_DID, Address here for Device ID
martinsimpson 0:a51016633386 28
martinsimpson 0:a51016633386 29 while (i2c.write((MCP24AA02_ADDR|WRITE), ucdata_write, 1, 0)){} //Wait for ACK if EEPROM is in 'write' cycle
martinsimpson 0:a51016633386 30
martinsimpson 0:a51016633386 31 {
martinsimpson 0:a51016633386 32 i2c.read((MCP24AA02_ADDR|READ),ucdata_read,1,0);
martinsimpson 0:a51016633386 33 if (ucdata_read[0]==0x29)
martinsimpson 0:a51016633386 34 {
martinsimpson 0:a51016633386 35 pc.printf("Code=%#x (Microchip Technology ltd.(c))\n\r",ucdata_read[0]);return;
martinsimpson 0:a51016633386 36 }
martinsimpson 0:a51016633386 37 if (ucdata_read[0]==0x41)
martinsimpson 0:a51016633386 38 {
martinsimpson 0:a51016633386 39 pc.printf("Code=%#x (2K EEPROM Using i2c)\n\r",ucdata_read[0]);
martinsimpson 0:a51016633386 40 }
martinsimpson 0:a51016633386 41 else
martinsimpson 0:a51016633386 42 {
martinsimpson 0:a51016633386 43 pc.printf("0x%x 0x%X %c\n\r",addy,ucdata_read[0],(ucdata_read[0]));
martinsimpson 0:a51016633386 44 }
martinsimpson 0:a51016633386 45 }
martinsimpson 0:a51016633386 46 }
martinsimpson 0:a51016633386 47
martinsimpson 0:a51016633386 48 void readEE(int addy)
martinsimpson 0:a51016633386 49 {
martinsimpson 0:a51016633386 50 char ucdata_write[1];
martinsimpson 0:a51016633386 51 char ucdata_read[1];
martinsimpson 0:a51016633386 52 ucdata_write[0] = addy; //Address here to read
martinsimpson 0:a51016633386 53 while (i2c.write((MCP24AA02_ADDR|WRITE), ucdata_write, 1, 0)){} //Wait for ACK if EEPROM is in 'write' cycle
martinsimpson 0:a51016633386 54 i2c.read((MCP24AA02_ADDR|READ),ucdata_read,1,0); //Note 'OR' Address with Read bit
martinsimpson 0:a51016633386 55 pc.printf("%#X %c",ucdata_read[0],ucdata_read[0]);
martinsimpson 0:a51016633386 56 }
martinsimpson 0:a51016633386 57
martinsimpson 0:a51016633386 58 void writeEE(int addy,int data)
martinsimpson 0:a51016633386 59 {
martinsimpson 0:a51016633386 60 char ucdata_write[2];
martinsimpson 0:a51016633386 61 ucdata_write[0] = addy;
martinsimpson 0:a51016633386 62 ucdata_write[1] = data;
martinsimpson 0:a51016633386 63 while (i2c.write((MCP24AA02_ADDR|WRITE), ucdata_write, 1, 0)){} //Wait for ACK if EEPROM is in 'write' cycle
martinsimpson 0:a51016633386 64 i2c.write((MCP24AA02_ADDR|WRITE),ucdata_write,2,0); //Note 'OR' Adress with Write bit
martinsimpson 0:a51016633386 65 pc.printf("%04d %c\t",addy,data);
martinsimpson 0:a51016633386 66 }
martinsimpson 0:a51016633386 67
martinsimpson 0:a51016633386 68 void eraseEE(void)
martinsimpson 0:a51016633386 69 {
martinsimpson 0:a51016633386 70 for (int i=0;i<0xFA;i++) //0xFA to 0xFF are read only with Manufacture/Hardware ID and a Unique Serial Number
martinsimpson 0:a51016633386 71 {
martinsimpson 0:a51016633386 72 writeEE(i,0xFF);
martinsimpson 0:a51016633386 73 }
martinsimpson 0:a51016633386 74 }
martinsimpson 0:a51016633386 75
martinsimpson 0:a51016633386 76 int main()
martinsimpson 0:a51016633386 77 {
martinsimpson 0:a51016633386 78 unsigned int uibaudrate=115200;
martinsimpson 0:a51016633386 79 pc.baud(uibaudrate);
martinsimpson 0:a51016633386 80
martinsimpson 0:a51016633386 81 unsigned int uifrequency=400000; //400KHz for i2c Max
martinsimpson 0:a51016633386 82 i2c.frequency (uifrequency);
martinsimpson 0:a51016633386 83
martinsimpson 0:a51016633386 84 char ucdata_write[1];
martinsimpson 0:a51016633386 85 if (!i2c.write((MCP24AA02_ADDR|WRITE), ucdata_write, 1, 0))// Check for ACK from EPPROM
martinsimpson 0:a51016633386 86 {
martinsimpson 0:a51016633386 87 pc.printf("\n\rHello World ");
martinsimpson 0:a51016633386 88 pc.printf("at %u BAUD and %uKHz i2c Frequency\n\r",uibaudrate,uifrequency/1000);
martinsimpson 0:a51016633386 89 pc.printf("Using mbed.org Martin\n\r");
martinsimpson 0:a51016633386 90 readID(MCP24AA02_MID);
martinsimpson 0:a51016633386 91 readID(MCP24AA02_DID);
martinsimpson 0:a51016633386 92 // Uncomment the following 6 lines of code to write Data into the EEPROM
martinsimpson 0:a51016633386 93 /*
martinsimpson 0:a51016633386 94 writeEE(0x00,0x4D); // ASCII M
martinsimpson 0:a51016633386 95 writeEE(0x01,0x61); // ASCII a
martinsimpson 0:a51016633386 96 writeEE(0x02,0x72); // ASCII r
martinsimpson 0:a51016633386 97 writeEE(0x03,0x74); // ASCII t
martinsimpson 0:a51016633386 98 writeEE(0x04,0x69); // ASCII i
martinsimpson 0:a51016633386 99 writeEE(0x05,0x6E); // ASCII n
martinsimpson 0:a51016633386 100 pc.printf("\n\r");
martinsimpson 0:a51016633386 101 */
martinsimpson 0:a51016633386 102
martinsimpson 0:a51016633386 103 //Uncomment the following line to Erase the EEPROM
martinsimpson 0:a51016633386 104 // eraseEE();
martinsimpson 0:a51016633386 105
martinsimpson 0:a51016633386 106 for (int i=0;i<=0x5;i++) //Just going to read the first 6 (zero to five) locations in the EEPROM
martinsimpson 0:a51016633386 107 {
martinsimpson 0:a51016633386 108 readEE(i);pc.printf("\t"); //A Bit of formating going to tab into 8 columns
martinsimpson 0:a51016633386 109 if (!((i+1)%8)){pc.printf("\n\r");} //of data
martinsimpson 0:a51016633386 110 }
martinsimpson 0:a51016633386 111 }
martinsimpson 0:a51016633386 112 else
martinsimpson 0:a51016633386 113 {
martinsimpson 0:a51016633386 114 pc.printf("\n\rCannot get an ACK from the Device check connections!\n\r");
martinsimpson 0:a51016633386 115 }
martinsimpson 0:a51016633386 116 }
martinsimpson 0:a51016633386 117