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.
main.cpp
00001 #include "mbed.h" 00002 00003 #define MCP24AA02_ADDR (0xA0) // 24AA02 2K EEPROM using i2c Address 00004 // for the 22AA025 variant this can be 0xA0,0xA2,0xA4,0xA6,0xA8,0xAA,0xAC,0xAE 00005 // since LSB is used to to control a WRITE(0) or a READ(1) Operation 00006 #define WRITE (0x00) // 24AA02 2K EEPROM using i2c Write bit 00007 #define READ (0x01) // 24AA02 2K EEPROM using i2c Read bit 00008 #define MCP24AA02_MID (0xFA) // Manufacturer ID Address (Read Only 0x29==Microchip) 00009 #define MCP24AA02_DID (0xFB) // Device ID Adress (Read Only 0x41==4 is i2c family and 1 is 2K device) 00010 // Information from Microchip Datasheet DS20005202A Part #24AA02UID/24AA025UID 00011 /* This is a very simple program to demonstrate the ease of use of the i2c bus utilising an EEPROM. 00012 Do not forget that Pull Up resistors are required on the SDA and SCL lines 00013 (Typically 2K ohms for 400KHz bus speeds) 00014 These are required since devices connected with the i2c will have 'open drain' (or open collector) 00015 circuitry to allow wire 'ORing' of their respective connections 00016 Used with a stm32-Nucleo-F401RE 00017 */ 00018 00019 I2C i2c(I2C_SDA, I2C_SCL); //I2C Class Pin Assignments see I2C.h 00020 00021 Serial pc(SERIAL_TX, SERIAL_RX); //Serial Class Pin Assignments see Serial.h 00022 00023 void readID(int addy) 00024 { 00025 char ucdata_write[1]; 00026 char ucdata_read[1]; 00027 ucdata_write[0] = addy; //MCP24AA02_DID, Address here for Device ID 00028 00029 while (i2c.write((MCP24AA02_ADDR|WRITE), ucdata_write, 1, 0)){} //Wait for ACK if EEPROM is in 'write' cycle 00030 00031 { 00032 i2c.read((MCP24AA02_ADDR|READ),ucdata_read,1,0); 00033 if (ucdata_read[0]==0x29) 00034 { 00035 pc.printf("Code=%#x (Microchip Technology ltd.(c))\n\r",ucdata_read[0]);return; 00036 } 00037 if (ucdata_read[0]==0x41) 00038 { 00039 pc.printf("Code=%#x (2K EEPROM Using i2c)\n\r",ucdata_read[0]); 00040 } 00041 else 00042 { 00043 pc.printf("0x%x 0x%X %c\n\r",addy,ucdata_read[0],(ucdata_read[0])); 00044 } 00045 } 00046 } 00047 00048 void readEE(int addy) 00049 { 00050 char ucdata_write[1]; 00051 char ucdata_read[1]; 00052 ucdata_write[0] = addy; //Address here to read 00053 while (i2c.write((MCP24AA02_ADDR|WRITE), ucdata_write, 1, 0)){} //Wait for ACK if EEPROM is in 'write' cycle 00054 i2c.read((MCP24AA02_ADDR|READ),ucdata_read,1,0); //Note 'OR' Address with Read bit 00055 pc.printf("%#X %c",ucdata_read[0],ucdata_read[0]); 00056 } 00057 00058 void writeEE(int addy,int data) 00059 { 00060 char ucdata_write[2]; 00061 ucdata_write[0] = addy; 00062 ucdata_write[1] = data; 00063 while (i2c.write((MCP24AA02_ADDR|WRITE), ucdata_write, 1, 0)){} //Wait for ACK if EEPROM is in 'write' cycle 00064 i2c.write((MCP24AA02_ADDR|WRITE),ucdata_write,2,0); //Note 'OR' Adress with Write bit 00065 pc.printf("%04d %c\t",addy,data); 00066 } 00067 00068 void eraseEE(void) 00069 { 00070 for (int i=0;i<0xFA;i++) //0xFA to 0xFF are read only with Manufacture/Hardware ID and a Unique Serial Number 00071 { 00072 writeEE(i,0xFF); 00073 } 00074 } 00075 00076 int main() 00077 { 00078 unsigned int uibaudrate=115200; 00079 pc.baud(uibaudrate); 00080 00081 unsigned int uifrequency=400000; //400KHz for i2c Max 00082 i2c.frequency (uifrequency); 00083 00084 char ucdata_write[1]; 00085 if (!i2c.write((MCP24AA02_ADDR|WRITE), ucdata_write, 1, 0))// Check for ACK from EPPROM 00086 { 00087 pc.printf("\n\rHello World "); 00088 pc.printf("at %u BAUD and %uKHz i2c Frequency\n\r",uibaudrate,uifrequency/1000); 00089 pc.printf("Using mbed.org Martin\n\r"); 00090 readID(MCP24AA02_MID); 00091 readID(MCP24AA02_DID); 00092 // Uncomment the following 6 lines of code to write Data into the EEPROM 00093 /* 00094 writeEE(0x00,0x4D); // ASCII M 00095 writeEE(0x01,0x61); // ASCII a 00096 writeEE(0x02,0x72); // ASCII r 00097 writeEE(0x03,0x74); // ASCII t 00098 writeEE(0x04,0x69); // ASCII i 00099 writeEE(0x05,0x6E); // ASCII n 00100 pc.printf("\n\r"); 00101 */ 00102 00103 //Uncomment the following line to Erase the EEPROM 00104 // eraseEE(); 00105 00106 for (int i=0;i<=0x5;i++) //Just going to read the first 6 (zero to five) locations in the EEPROM 00107 { 00108 readEE(i);pc.printf("\t"); //A Bit of formating going to tab into 8 columns 00109 if (!((i+1)%8)){pc.printf("\n\r");} //of data 00110 } 00111 } 00112 else 00113 { 00114 pc.printf("\n\rCannot get an ACK from the Device check connections!\n\r"); 00115 } 00116 } 00117
Generated on Sat Jul 16 2022 18:10:22 by 1.7.2