Brayan Andrés BermúdezEdgard Daniel Ucha
Published © GPL3+

EEPROM: How To Read/Write Data With PSoC5 LP

Easy and optimal way to write and read data in the EEPROM memory of PSoC5LP.

BeginnerProtip4 hours8,168
EEPROM: How To Read/Write Data With PSoC5 LP

Things used in this project

Story

Read more

Schematics

5dfdEsKV30GuJR3IsjVT.png

Schematic

Code

Main Code

C/C++
/* ========================================
 *
 * Copyright YOUR COMPANY, THE YEAR
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF your company.
 *
 * ========================================
*/
#include <project.h>
#include <stdio.h>//library to use the function sprintf
#include <stdlib.h>//library to use the function atoi and itoa
#include <string.h>//library for handling characters

int numero[4]={0,0,0,0};//vector of 4 positions where each digit of the counter will be
char numaimp[4];//vector containing the 4 digits but converted into character for storage in EEPROM
char lectura[4];// vector that stores the characters read from the memory EEPROM

cystatus writeStatus;//Variable to check if writing was successful or not

int i=0;//variable used in the for cycle reading

CY_ISR(InterruptReset)
{
    for(i=0;i<=3;i++)// for cycle writing zeros in the EEPROM
    {
        writeStatus=EEPROM_WriteByte('0',i); // writing in the memory addresses that are using the zeros
    }

    if(writeStatus == CYRET_SUCCESS)
    {

    }else{
    }

    CySoftwareReset();// reset por software

}

CY_ISR(InterruptTemp)// Interrupcion linked to the PWM that is generated every 1 sec.
{
    /*********************************************************************
    You have to understand the logic of a 4 digit counter,
    to use one to one we must ask for each digit, once you arrive to 10,
    increase the next digit and return to zero the previous,
    once that is increased the digit more significant to 10,
    mean that we have overflowed the counter then you must return all the variables to zeros
    *********************************************************************/

    numero[3]=numero[3]+1;
    if(numero[3]==10)
    {
        numero[2]=numero[2]+1;
        numero[3]=0;
    }
    if(numero[2]==10)
    {
        numero[1]=numero[1]+1;
        numero[2]=0;
        numero[3]=0;

    }
    if(numero[1]==10)
    {
        numero[0]=numero[0]+1;
        numero[1]=0;
        numero[2]=0;
        numero[3]=0;

    }
    if(numero[0]==10)
    {
        numero[0]=0;
        numero[1]=0;
        numero[2]=0;
        numero[3]=0;
    }

    /*
    This function sprintf converts all vector integers to character in the numaimp variable for storage
    */
    sprintf(numaimp,"%d%d%d%d",numero[3],numero[2],numero[1],numero[0]);

    writeStatus=EEPROM_WriteByte(numaimp[0],3);//Data storage for the variable numaimp in the memory address 3
    writeStatus=EEPROM_WriteByte(numaimp[1],2);//Data storage for the variable numaimp in the memory address 2
    writeStatus=EEPROM_WriteByte(numaimp[2],1);//Data storage for the variable numaimp in the memory address 1
    writeStatus=EEPROM_WriteByte(numaimp[3],0);//Data storage for the variable numaimp in the memory address 0


}


int main()
{
    CyGlobalIntEnable;

    PWM_Start();//PWM initialization
    EEPROM_Start();//EEPROM initialization
    LCD_Start();//LCD initialization

    LCD_Position(0,0);//position 0,0
    LCD_PrintString("Demo EEPROM");//print text

    isrTemp_StartEx(InterruptTemp);//it links the interruption of the pwm in the schematic one, with the method executes
    isrReset_StartEx(InterruptReset);// the interruption of the reset linked in the schematic with the method that executes

    for(i=0;i<=3;i++)//for cycle dedicated to read EEPROM memory and save the data in the vector read
    {
        lectura[i]=EEPROM_ReadByte(i);//Lectura de eeprom, recibe la direccin y devuelve el dato de dicha direccion
    }

    numero[0]=(int)(read[0]-48);// conversion of the variable read from the EEPROM that is character to entire
    numero[1]=(int)(read[1]-48);// conversion of the variable read from the EEPROM that is character to entire
    numero[2]=(int)(read[2]-48);// conversion of the variable read from the EEPROM that is character to entire
    numero[3]=(int)(read[3]-48);// conversion of the variable read from the EEPROM that is character to entire

    for(;;)
    {
        LCD_Position(2,0);//position 2,0 LCD
        LCD_PrintString("Val Cont: ");// print text
        LCD_PutChar(numaimp[3]);//Print variable converted to character of the integer that is increasing
        LCD_PutChar(numaimp[2]);
        LCD_PutChar(numaimp[1]);
        LCD_PutChar(numaimp[0]);

        LCD_Position(3,0);// position 3,0 LCD
        LCD_PrintString("Ult val: "); // print text
        LCD_PrintString(read);// Prints the variable read that makes reference to the number that was saved in the EEPROM

    }
}

Files for this projects.

Credits

Brayan Andrés Bermúdez
9 projects • 19 followers
Contact
Edgard Daniel Ucha
3 projects • 4 followers
An electronic engineer, hardware designs,
Contact

Comments

Please log in or sign up to comment.