9 years, 8 months ago.

writing and reading to and from EEPROM

is there any sample progs around for this ,simple tests ?internally like the arduino progs

2 Answers

9 years, 8 months ago.

You could try some parts of this code I used to test some eeproms I was going to use to log data. This scans the i2c bus first to find any devices connected then writes RTC clock time to all the of memory reading after each write to confirm data.

I used this eeprom library:

https://mbed.org/users/bborredon/code/eeprom/

I used 24wc32 chips

There are a few considerations though, eeproms are slow to write to and generally arranged in 8 bit wide data so you will use 4 bytes for a 32 bit value. A 32k eeprom will give you 4096 32bit wide bytes. This was not enough and too slow so I used a SD card instead.

i2c scan and eeprom test

#include "mbed.h"
#include "eeprom.h"
#include "OLED32028P1T.h"


//OLED32028P1T oled(p13, p14, p29);   // Oled Display tx, rx, rs
OLED32028P1T oled(p28, p27, p29);   // Oled Display tx, rx, rs

EEPROM rom(p9, p10, 0, EEPROM::T24C32);
I2C i2c(p9, p10); // I2C device sda, scl

Timer timer;

int n,x;
int RomAddress;
int rdata,wdata;
int hour,minute,second,dayofweek,dayofmonth,month,year;
int RTCsecond,RTCminute,RTChour,RTCdayofweek,RTCdayofmonth,RTCmonth,RTCyear; 

char buffer[32];

void RTCread();

struct tm t;

int main() {
    
    
    oled.init();
    oled.eraseScreen();
    oled.setTextBackgroundType(TEXT_OPAQUE);
    oled.setFontColor(WHITE);
        
        oled.printf("I2C Searching!");
        oled.locate(0,2);
        
        int count = 0;
        for (int address=0; address<256; address+=2) {
            if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
                oled.setFontColor(GREEN);
                oled.printf("I2C address 0x%02X\n", address);
                count++;
            }
        }
        oled.setFontColor(CYAN);
        oled.locate(0,7);
        oled.printf("%d devices found\n", count);
        oled.locate(0,12);
        oled.setFontColor(YELLOW);
        
        
        
        while (RomAddress < 4096){
            
         
                while (n<10){
                
                    time_t seconds = time(NULL);
                    
                    wdata = seconds;
                    rom.write(RomAddress,wdata); wait_ms(1); // wait for eeprom to complete write cycle.
                                
                    rom.read(RomAddress,rdata);
                    float memused = RomAddress;
                    oled.locate(0,n+10);               
                    oled.printf("Address %04d   data  %d  %d \n", RomAddress, rdata, rom.getError());
                    if(rom.getError()==2) {rdata=0;}                   
                    
                    RTCread();
                    
                    oled.locate(10,22);
                    oled.printf("%2d:%02d:%02d  ",RTChour,RTCminute,RTCsecond);
                    oled.printf("%d-%02d-20%02d",RTCdayofmonth,RTCmonth,RTCyear); 
                    
                    oled.locate(10,24);oled.printf("%3.2f %% Memory used", memused/4096*100);
                    n++;RomAddress=RomAddress+4; 
                
                    wait(1);
                }
                
             n=0;      
            }
    
        oled.printf("Finished");
     
}

void RTCread()
{
                time_t seconds = rdata;               

                strftime(buffer, 2,"%S", localtime(&seconds));
                RTCsecond = atoi(buffer);
                strftime(buffer, 2,"%M", localtime(&seconds));
                RTCminute = atoi(buffer);                       
                strftime(buffer, 2,"%H", localtime(&seconds));
                RTChour = atoi(buffer);
                strftime(buffer, 2,"%d", localtime(&seconds));                                
                RTCdayofmonth = atoi(buffer);
                strftime(buffer, 2,"%w", localtime(&seconds));                
                RTCdayofweek = atoi(buffer);
                strftime(buffer, 2,"%m", localtime(&seconds));
                RTCmonth = atoi(buffer);
                strftime(buffer, 2,"%y", localtime(&seconds));
                RTCyear = atoi(buffer);
} 

Accepted Answer
9 years, 8 months ago.

You mean internal EEPROM of the microcontroller? Your KL25 doesn't have EEPROM, so no ;)

how about progs to an external eeprom ,it seems to have room for it to be soldered on to the board

posted by brian craig 30 Jul 2014

It doesn't, the empty socket is for a flash IC, which would be connected to the programmer/debugger IC, not the KL25 itself. Of course you can always connect another EEPROM yourself. Some EEPROM code for external EEPROMs: http://mbed.org/search/?selected_facets=obj_type_exact%3ACode+Repository&order_by=-import_count&q=eeprom

posted by Erik - 30 Jul 2014