Juan Esteban Paz
Published © CC BY

Easy EEMPROM Demo

A very simple way to store data in your programs even after the device is shutdown, in a very simple way to make it work.

IntermediateProtip27 minutes998
Easy EEMPROM Demo

Things used in this project

Story

Read more

Schematics

EEPROM demo

psoc workspace

Code

emEEPROM main.c

C/C++
#include <stdlib.h> 
#include <project.h>
int VariableA, VariableB;

//EEPROM related variables and arrays
cystatus status;
uint8 ram_array[4]; // RAM array size in bytes (8bits) each
static const uint8 CYCODE eepromArray[]=  { 0,0,0,0 }; // eeprom array size in bytes (8bits) each
uint16 user_array[2]; //User array is half size because it is 16bit array to store larger numbers


char msg[7];
//EEPROM transfer function destination - source
void user_eeprom(uint8 * dest, uint8 *  src,uint8 size){// size - size of the src and dest in bytes.
     uint8 i=0;
     for(i=0;i<size;i++){ *(dest+i) = *(src + i); }
}
//function prototype
void Storeparam();

void Init(){//First run routine
    EEPROM_Start();    
    CyDelay(100);    
    //First RUN write Default values
    //check if there is any stored value on the array if not write the default values
    if(*(volatile uint8 *) &eepromArray[0] == 0){      
        user_array[0]= 1;//variableA
        user_array[1]= 1;//variableB      
        user_eeprom((uint8*)&ram_array,(uint8*)(&user_array), 4);
        status = EEPROM_Write(ram_array,eepromArray,4);
        if (CYRET_SUCCESS != status){}
        Init();//after writing the values run the init Routine again to load values
    }
    else{//If there is anything stored (2nd or later run) just load the values to the variables
        user_eeprom((uint8*)(&user_array), (uint8*)&eepromArray, 4);
        VariableA=user_array[0];
        VariableB=user_array[1];   
    }    
    I2C_Start();
    I2C_LCD_Start();
    CyDelay(1200u);
    I2C_LCD_Position(0u, 4u);
    I2C_LCD_PrintString("emEEPROM DEMO");
    I2C_LCD_Position(2u, 2u);I2C_LCD_PrintString("Variable A");
    I2C_LCD_Position(3u, 2u);I2C_LCD_PrintString("Variable B");
   
}

int main()
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    Init();
    VariableA++;
    VariableB=VariableB+2;
    Storeparam();
    for(;;)
    {        
        itoa(VariableA,msg,10);
        I2C_LCD_Position(2u, 16u);I2C_LCD_PrintString(msg);
        itoa(VariableB,msg,10);
        I2C_LCD_Position(3u, 16u);I2C_LCD_PrintString(msg);
        CyDelay(150);
    }
}

void Storeparam(){//emeeprom store param
    user_array[0]=VariableA;
    user_array[1]=VariableB;  
    user_eeprom((uint8*)&ram_array,(uint8*)(&user_array),4);
    status = EEPROM_Write(ram_array,eepromArray,4);
}

Credits

Juan Esteban Paz

Juan Esteban Paz

12 projects • 22 followers
Mechanical and Electrical Engineer enjoy working with PSOC

Comments