Atmega 328 EEPROM ISSUE

Thread Starter

AJIN NADH

Joined Dec 18, 2014
84
Hi All,
I stored a byte value to the atmega 328 eeprom, But when a change in the Supply occurs ( a triggering voltage), the value at the eeprom gets erased...
What may be the issue, I enabled the BrownOut detection.but the issue is still existsing.....Please help me..

Ajin nadh.v.a
 

Thread Starter

AJIN NADH

Joined Dec 18, 2014
84
Would you pleases post your code so we can have a look at it?

Please read section 8.4.2 Preventing EEPROM Corruption in the datasheet.
Hi sir,
Thanks for the Reply,
Please find my code.......

C:
#include <avr/io.h>

#include <avr/wdt.h>

long previousMillis1 = 0;

long interval1 = 1000;

long DelayTime=20000;

int Write=0,Clear=0,Counter;

unsigned long previousMillis = 0,CpreviousMillis=0;

const long interval = 500;

const int EEPROM_MIN_ADDR = 0;

const int EEPROM_MAX_ADDR = 1015;

const int EEPROM_Start_ADDR = 0;

int EEPROM_End_ADDR = 0;

#include <EEPROM.h>

#include <SPI.h>

#include <MFRC522.h>

#define RFID_SS  10

#define RFID_RST 5

MFRC522 rfid( RFID_SS, RFID_RST );

char RFIDValue[3];

const int BUFSIZE =5;

char buf[BUFSIZE];

char RFIDbuf[4];

int mem,N=0,l=0;

boolean Found=false;

int buzzer=A0;

const int ledRed =  A1;  // the number of the LED pin

const int ledGreen = A2;

const int ButtonPin =  A3;

int buzzerState = LOW;  // ledState used to set the LED

int ledState = LOW;  // ledState used to set the LED

boolean Flag=false;

const int Green=1,Red=2,Yellow=3;

int i;

/*****************************************************************************************************/

void eeprom_erase_all(byte b = 0xFF) {

int i;

for (i = EEPROM_MIN_ADDR; i <= EEPROM_MAX_ADDR; i++) {

EEPROM.write(i, b);

}

}

/*****************************************************************************************************/

void setup() {

MCUSR=0;

wdt_disable();

SPI.begin();

Serial.begin(19200);

rfid.begin();

pinMode(buzzer, OUTPUT);

pinMode(ButtonPin, INPUT_PULLUP);

pinMode(ledRed, OUTPUT);

pinMode(ledGreen, OUTPUT);

}

/*****************************************************************************************************/


void loop() {

byte uid[5];

byte data[BUFSIZE];

if (digitalRead(ButtonPin)==LOW)

{

ButtonPress();

}

if (Write==0&&Clear==0)

{

BlinkLed(ledGreen,ledRed,Green);

}

else if (Write==1&&Clear==0)

{

BlinkLed(ledGreen,ledRed,Yellow);

Delay();

}

else if (Write==0&&Clear==1)

{

BlinkLed(ledGreen,ledRed,Red);

Delay();

}

Serial.flush();

/*****************************************************************************************************/

if (Serial.available()>0)

{

String str= Serial.readString();

Serial.println(str);

if(str="AT+C")

{



Serial.println("Empty Database");

eeprom_erase_all();

LongBuzzer();

}

}

/*****************************************************************************************************/ //READING

if ( rfid.requestTag( MF1_REQIDL, data ) == MI_OK) {

if ( rfid.antiCollision( data ) == MI_OK  &&Write==0&&Clear==0 ) {

Flag=true;

wdt_enable(WDTO_1S);

memcpy( uid, data, 5 );

for(int i=0;i<4;i++)

{

RFIDValue[I]= uid[I];

}

for (int i = 0; i < BUFSIZE; i++) {

buf[I] = 0;

}

int j=0,b;

for(int i=0;i<=EEPROM_MAX_ADDR;i+=5)

{

b=i;



for(int k=i,m=0;m<4;k++,m++)

{

if(uid[m]==EEPROM.read(k))

{

j=j+1;



}



if (j==3)

{

N=1;

i=i+EEPROM_MAX_ADDR;

j=0;



}



}



}

if(N==0)

{

ErrorBeep(" Not Registered");

}



else if(N==1)

{

PrintRFID(b);

b=0;

N=0;

buzzer_trigger();

delay(1000);

Restart();

}

wdt_reset();

}

/*****************************************************************************************************/  ///WRITING

if ( rfid.antiCollision( data ) == MI_OK && Write==1 && Clear==0) {

wdt_enable(WDTO_1S);

for (int i = 0; i < BUFSIZE; i++) {

uid[I] = 0;

}

memcpy( uid, data, 5 );

for(int i=0;i<4;i++)

{

RFIDValue[I]= uid[I];



}



delay(500);

int j=0,b,z;

for(int i=0;i<=EEPROM_MAX_ADDR;i+=5)

{

b=i;

for(int k=i,m=0;m<4;k++,m++)

{



if(uid[m]==EEPROM.read(k))

{

j=j+1;



}

}

if (j==4)

{



ErrorBeep(" Card already exist");

delay(3000);

b=0;

j=0;

z=10;

}

else

{

z=0;

}

}

if (z!=10)

{

byte x = 0xFF;

int j=0,b,i;

for(i=0;i<=EEPROM_MAX_ADDR;i+=5)

{

b=i;

for(int k=i,m=0;m<=3;k++,m++)

{

if(EEPROM.read(k)==255)

{

j=j+1;

}

}

if (j==4)

{

eeprom_write_string(b, RFIDValue);

PrintRFID(b);

Serial.print(": New Card added");

b=0;

LongBuzzer();

i=i+EEPROM_MAX_ADDR;

j=0;

delay(1000);

Restart();

}else

{

}

}

}

wdt_reset();

l=0; Clear=0;Write=0; digitalWrite(ledRed,LOW);

}

/*****************************************************************************************************/  //CLEARING

if ( rfid.antiCollision( data ) == MI_OK && Clear==1&&Write==0) {

wdt_enable(WDTO_1S);

memcpy( uid, data, 5 );

for(int i=0;i<4;i++)

{

RFIDValue[I]= uid[I];

}

delay(500);

for (int i = 0; i < BUFSIZE; i++) {

buf[I] = 0;

}

int j=0,b;

for(int i=0;i<=EEPROM_MAX_ADDR;i+=5)

{

b=i;

for(int k=i,m=0;m<4;k++,m++)

{



if(uid[m]==EEPROM.read(k))

{

j=j+1;



}

if (j==3)

{

int memo=b;

byte c = 0xFF;

for(memo;memo<(b+5);memo++)

{



EEPROM.write(memo, c);

}

b=0;

PrintDeleteRFID ();

Serial.print(": Card Deleted");

LongBuzzer();

delay(2000);

i=i+EEPROM_MAX_ADDR;

j=0;

}

}

}

l=0;Clear=0;Write=0;digitalWrite(ledRed,LOW);

Restart();

}

wdt_reset();

}

}

/******************************************************************************************************/

void ErrorBeep(String Err)

{


digitalWrite(ledRed, HIGH);

digitalWrite(ledGreen, LOW);

PrintDeleteRFID ();

Serial.print(" :"+ Err);

for(int i=0;i<5;i++)

{

digitalWrite(buzzer, LOW);  // turn the LED on (HIGH is the voltage level)

delay(180);  // wait for a second

digitalWrite(buzzer, HIGH);  // turn the LED off by making the voltage LOW

delay(180);

}

Restart();



}


/******************************************************************************************************/


void eeprom_erase_RFID(int EEPROM_Start_ADDR) {

int i;

byte b = 0xFF;

EEPROM_End_ADDR =EEPROM_Start_ADDR+10;


for (i = EEPROM_Start_ADDR; i <= EEPROM_End_ADDR; i++) {

EEPROM.write(i, b);

}

}

boolean eeprom_is_addr_ok(int addr) {

return ((addr >= EEPROM_MIN_ADDR) && (addr <= EEPROM_MAX_ADDR));

}


boolean eeprom_write_bytes(int startAddr, const byte* array, int numBytes) {

int i;

if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) {

return false;

}

for (i = 0; i < numBytes; i++) {

EEPROM.write(startAddr + i, array[I]);

}


return true;

}

boolean eeprom_read_bytes(int startAddr, byte array[], int numBytes) {

int i;


// both first byte and last byte addresses must fall within

// the allowed range

if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) {

return false;

}


for (i = 0; i < numBytes; i++) {

array[I] = EEPROM.read(startAddr + i);

}


return true;

}

boolean eeprom_write_int(int addr, int value) {

byte *ptr;


ptr = (byte*)&value;

return eeprom_write_bytes(addr, ptr, sizeof(value));

}

boolean eeprom_read_int(int addr, int* value) {

return eeprom_read_bytes(addr, (byte*)value, sizeof(int));

}

boolean eeprom_write_string(int addr, const char* string) {

// actual number of bytes to be written

int numBytes;


// we'll need to write the string contents

// plus the string terminator byte (0x00)

numBytes = strlen(string) + 1;


return eeprom_write_bytes(addr, (const byte*)string, numBytes);

}

boolean eeprom_read_string(int addr, char* buffer, int bufSize) {

byte ch;

int bytesRead;

if (!eeprom_is_addr_ok(addr)) {

return false;

}

if (bufSize == 0) {

return false;

}

if (bufSize == 1) {

buffer[0] = 0;

return true;

}

bytesRead = 0;

ch = EEPROM.read(addr + bytesRead);

buffer[bytesRead] = ch;

bytesRead++;

while ( (ch != 0x00) && (bytesRead < bufSize) && ((addr + bytesRead) <= EEPROM_MAX_ADDR) ) {

ch = EEPROM.read(addr + bytesRead);

buffer[bytesRead] = ch;

bytesRead++;

}

if ((ch != 0x00) && (bytesRead >= 1)) {

buffer[bytesRead - 1] = 0;

}

return true;

}


int start_time;

int stop_time;

String Str;

void start_timing() {

start_time = millis();

}


void print_elapsed() {

stop_time = millis();

Serial.print("Time elapsed (ms): ");

Serial.println(stop_time - start_time);

}


void buzzer_trigger()

{

digitalWrite(ledRed,LOW);

digitalWrite(ledGreen,HIGH);

digitalWrite(buzzer,HIGH);

delay(300);

digitalWrite(buzzer,LOW);

digitalWrite(ledGreen,LOW);

}

void LongBuzzer()

{

digitalWrite(ledRed,LOW);

digitalWrite(ledGreen,HIGH);

digitalWrite(buzzer,HIGH);

delay(1000);

digitalWrite(buzzer,LOW);

digitalWrite(ledGreen,LOW);

}


void BlinkLed (const int LedRed,const int LedGreen,int mode)

{


unsigned long currentMillis = millis();

if(currentMillis - previousMillis >= interval) {

previousMillis = currentMillis;

if (ledState == LOW)

ledState = HIGH;

else

ledState = LOW;

if (mode==1)

{

digitalWrite(ledGreen, ledState);



}else if (mode==2)

{

digitalWrite(ledRed, ledState);

digitalWrite(buzzer,ledState);

}

else if (mode==3)

{



digitalWrite(ledGreen, ledState);

digitalWrite(ledRed, ledState);

digitalWrite(buzzer,ledState);

}

}}


void ButtonPress()

{


while (digitalRead(ButtonPin)==LOW)

{

digitalWrite(ledGreen, LOW);

digitalWrite(ledRed, LOW);

unsigned long currentMillis = millis();

if(currentMillis - previousMillis <= 2000&&currentMillis - previousMillis >= 10)

{

digitalWrite(ledGreen,HIGH);

digitalWrite(ledRed, HIGH);

Counter++;

Write=1;

Clear=0;

delay(10);

}

if(currentMillis - previousMillis <=5000 && currentMillis - previousMillis >= 2000)

{

digitalWrite(ledGreen, LOW);

digitalWrite(ledRed, HIGH);

Write=0;

Clear=1;

delay(10);

}

}


previousMillis = 0;

digitalWrite(ledGreen, LOW);

digitalWrite(ledRed, LOW);

}


void Delay()

{

unsigned long currentMillis = millis();

if(currentMillis - CpreviousMillis > DelayTime) {

// save the last time you blinked the LED

CpreviousMillis = 0;

wdt_enable(0);

wdt_reset();

if (Write==1)

{

Write=0;

}

else if (Clear==1)

{

Clear=0;


}

}

}


void Restart()

{


wdt_enable(0);

wdt_reset();

}


void PrintRFID (int loc)

{



for(int i=loc;i<loc+4;i++)

{

Serial.print(byte(EEPROM.read(i)),HEX);

RFIDValue[I]= EEPROM.read(i);



}



}


void PrintDeleteRFID ()

{

for(int i=0;i<4;i++)

{

Serial.print(byte(RFIDValue[I]),HEX);





}



}
[/I][/I][/I][/I][/I][/I][/I][/I][/I][/I][/I][/I][/I]
Regards,
Ajin nadh
 
Last edited by a moderator:

DickCappels

Joined Aug 21, 2008
6,461
This is without formatting which makes it difficult to follow. Can you post or attach a formatted version, or at least only show the EEPROM routines?

What compiler are you using?

Also, did you read and understand read section 8.4.2 Preventing EEPROM Corruption in the datasheet?
 

Thread Starter

AJIN NADH

Joined Dec 18, 2014
84
This is without formatting which makes it difficult to follow. Can you post or attach a formatted version, or at least only show the EEPROM routines?

What compiler are you using?

Also, did you read and understand read section 8.4.2 Preventing EEPROM Corruption in the datasheet?

Sir can u please forward the data sheet i Searched it but not getting
 

Thread Starter

AJIN NADH

Joined Dec 18, 2014
84
Hi sir,
I found that the issue is due to the serial , When a Glitch occurs , it is erasing the eeprom,

C:
if (Serial.available()>0)
  {
   String str= Serial.readString();
   Serial.println(str);
   if(str=="AT+C")

   {
  
   Serial.println("Empty Database");
    eeprom_erase_all();
    LongBuzzer();
}
   }
So i disabled this section, Thanks for the support.

Ajin nadh

Mod note: added code tags
 
Last edited by a moderator:
Top