Atmega 328 EEPROM ISSUE

Discussion in 'Embedded Systems and Microcontrollers' started by AJIN NADH, Jan 5, 2016.

  1. AJIN NADH

    Thread Starter Member

    Dec 18, 2014
    79
    4
    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
     
  2. DickCappels

    Moderator

    Aug 21, 2008
    2,648
    632
    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.
     
    AJIN NADH likes this.
  3. AJIN NADH

    Thread Starter Member

    Dec 18, 2014
    79
    4
    Hi sir,
    Thanks for the Reply,
    Please find my code.......

    Code (C):
    1.  
    2. #include <avr/io.h>
    3.  
    4. #include <avr/wdt.h>
    5.  
    6. long previousMillis1 = 0;
    7.  
    8. long interval1 = 1000;
    9.  
    10. long DelayTime=20000;
    11.  
    12. int Write=0,Clear=0,Counter;
    13.  
    14. unsigned long previousMillis = 0,CpreviousMillis=0;
    15.  
    16. const long interval = 500;
    17.  
    18. const int EEPROM_MIN_ADDR = 0;
    19.  
    20. const int EEPROM_MAX_ADDR = 1015;
    21.  
    22. const int EEPROM_Start_ADDR = 0;
    23.  
    24. int EEPROM_End_ADDR = 0;
    25.  
    26. #include <EEPROM.h>
    27.  
    28. #include <SPI.h>
    29.  
    30. #include <MFRC522.h>
    31.  
    32. #define RFID_SS  10
    33.  
    34. #define RFID_RST 5
    35.  
    36. MFRC522 rfid( RFID_SS, RFID_RST );
    37.  
    38. char RFIDValue[3];
    39.  
    40. const int BUFSIZE =5;
    41.  
    42. char buf[BUFSIZE];
    43.  
    44. char RFIDbuf[4];
    45.  
    46. int mem,N=0,l=0;
    47.  
    48. boolean Found=false;
    49.  
    50. int buzzer=A0;
    51.  
    52. const int ledRed =  A1;  // the number of the LED pin
    53.  
    54. const int ledGreen = A2;
    55.  
    56. const int ButtonPin =  A3;
    57.  
    58. int buzzerState = LOW;  // ledState used to set the LED
    59.  
    60. int ledState = LOW;  // ledState used to set the LED
    61.  
    62. boolean Flag=false;
    63.  
    64. const int Green=1,Red=2,Yellow=3;
    65.  
    66. int i;
    67.  
    68. /*****************************************************************************************************/
    69.  
    70. void eeprom_erase_all(byte b = 0xFF) {
    71.  
    72. int i;
    73.  
    74. for (i = EEPROM_MIN_ADDR; i <= EEPROM_MAX_ADDR; i++) {
    75.  
    76. EEPROM.write(i, b);
    77.  
    78. }
    79.  
    80. }
    81.  
    82. /*****************************************************************************************************/
    83.  
    84. void setup() {
    85.  
    86. MCUSR=0;
    87.  
    88. wdt_disable();
    89.  
    90. SPI.begin();
    91.  
    92. Serial.begin(19200);
    93.  
    94. rfid.begin();
    95.  
    96. pinMode(buzzer, OUTPUT);
    97.  
    98. pinMode(ButtonPin, INPUT_PULLUP);
    99.  
    100. pinMode(ledRed, OUTPUT);
    101.  
    102. pinMode(ledGreen, OUTPUT);
    103.  
    104. }
    105.  
    106. /*****************************************************************************************************/
    107.  
    108.  
    109. void loop() {
    110.  
    111. byte uid[5];
    112.  
    113. byte data[BUFSIZE];
    114.  
    115. if (digitalRead(ButtonPin)==LOW)
    116.  
    117. {
    118.  
    119. ButtonPress();
    120.  
    121. }
    122.  
    123. if (Write==0&&Clear==0)
    124.  
    125. {
    126.  
    127. BlinkLed(ledGreen,ledRed,Green);
    128.  
    129. }
    130.  
    131. else if (Write==1&&Clear==0)
    132.  
    133. {
    134.  
    135. BlinkLed(ledGreen,ledRed,Yellow);
    136.  
    137. Delay();
    138.  
    139. }
    140.  
    141. else if (Write==0&&Clear==1)
    142.  
    143. {
    144.  
    145. BlinkLed(ledGreen,ledRed,Red);
    146.  
    147. Delay();
    148.  
    149. }
    150.  
    151. Serial.flush();
    152.  
    153. /*****************************************************************************************************/
    154.  
    155. if (Serial.available()>0)
    156.  
    157. {
    158.  
    159. String str= Serial.readString();
    160.  
    161. Serial.println(str);
    162.  
    163. if(str="AT+C")
    164.  
    165. {
    166.  
    167.  
    168.  
    169. Serial.println("Empty Database");
    170.  
    171. eeprom_erase_all();
    172.  
    173. LongBuzzer();
    174.  
    175. }
    176.  
    177. }
    178.  
    179. /*****************************************************************************************************/ //READING
    180.  
    181. if ( rfid.requestTag( MF1_REQIDL, data ) == MI_OK) {
    182.  
    183. if ( rfid.antiCollision( data ) == MI_OK  &&Write==0&&Clear==0 ) {
    184.  
    185. Flag=true;
    186.  
    187. wdt_enable(WDTO_1S);
    188.  
    189. memcpy( uid, data, 5 );
    190.  
    191. for(int i=0;i<4;i++)
    192.  
    193. {
    194.  
    195. RFIDValue[I]= uid[I];
    196.  
    197. }
    198.  
    199. for (int i = 0; i < BUFSIZE; i++) {
    200.  
    201. buf[I] = 0;
    202.  
    203. }
    204.  
    205. int j=0,b;
    206.  
    207. for(int i=0;i<=EEPROM_MAX_ADDR;i+=5)
    208.  
    209. {
    210.  
    211. b=i;
    212.  
    213.  
    214.  
    215. for(int k=i,m=0;m<4;k++,m++)
    216.  
    217. {
    218.  
    219. if(uid[m]==EEPROM.read(k))
    220.  
    221. {
    222.  
    223. j=j+1;
    224.  
    225.  
    226.  
    227. }
    228.  
    229.  
    230.  
    231. if (j==3)
    232.  
    233. {
    234.  
    235. N=1;
    236.  
    237. i=i+EEPROM_MAX_ADDR;
    238.  
    239. j=0;
    240.  
    241.  
    242.  
    243. }
    244.  
    245.  
    246.  
    247. }
    248.  
    249.  
    250.  
    251. }
    252.  
    253. if(N==0)
    254.  
    255. {
    256.  
    257. ErrorBeep(" Not Registered");
    258.  
    259. }
    260.  
    261.  
    262.  
    263. else if(N==1)
    264.  
    265. {
    266.  
    267. PrintRFID(b);
    268.  
    269. b=0;
    270.  
    271. N=0;
    272.  
    273. buzzer_trigger();
    274.  
    275. delay(1000);
    276.  
    277. Restart();
    278.  
    279. }
    280.  
    281. wdt_reset();
    282.  
    283. }
    284.  
    285. /*****************************************************************************************************/  ///WRITING
    286.  
    287. if ( rfid.antiCollision( data ) == MI_OK && Write==1 && Clear==0) {
    288.  
    289. wdt_enable(WDTO_1S);
    290.  
    291. for (int i = 0; i < BUFSIZE; i++) {
    292.  
    293. uid[I] = 0;
    294.  
    295. }
    296.  
    297. memcpy( uid, data, 5 );
    298.  
    299. for(int i=0;i<4;i++)
    300.  
    301. {
    302.  
    303. RFIDValue[I]= uid[I];
    304.  
    305.  
    306.  
    307. }
    308.  
    309.  
    310.  
    311. delay(500);
    312.  
    313. int j=0,b,z;
    314.  
    315. for(int i=0;i<=EEPROM_MAX_ADDR;i+=5)
    316.  
    317. {
    318.  
    319. b=i;
    320.  
    321. for(int k=i,m=0;m<4;k++,m++)
    322.  
    323. {
    324.  
    325.  
    326.  
    327. if(uid[m]==EEPROM.read(k))
    328.  
    329. {
    330.  
    331. j=j+1;
    332.  
    333.  
    334.  
    335. }
    336.  
    337. }
    338.  
    339. if (j==4)
    340.  
    341. {
    342.  
    343.  
    344.  
    345. ErrorBeep(" Card already exist");
    346.  
    347. delay(3000);
    348.  
    349. b=0;
    350.  
    351. j=0;
    352.  
    353. z=10;
    354.  
    355. }
    356.  
    357. else
    358.  
    359. {
    360.  
    361. z=0;
    362.  
    363. }
    364.  
    365. }
    366.  
    367. if (z!=10)
    368.  
    369. {
    370.  
    371. byte x = 0xFF;
    372.  
    373. int j=0,b,i;
    374.  
    375. for(i=0;i<=EEPROM_MAX_ADDR;i+=5)
    376.  
    377. {
    378.  
    379. b=i;
    380.  
    381. for(int k=i,m=0;m<=3;k++,m++)
    382.  
    383. {
    384.  
    385. if(EEPROM.read(k)==255)
    386.  
    387. {
    388.  
    389. j=j+1;
    390.  
    391. }
    392.  
    393. }
    394.  
    395. if (j==4)
    396.  
    397. {
    398.  
    399. eeprom_write_string(b, RFIDValue);
    400.  
    401. PrintRFID(b);
    402.  
    403. Serial.print(": New Card added");
    404.  
    405. b=0;
    406.  
    407. LongBuzzer();
    408.  
    409. i=i+EEPROM_MAX_ADDR;
    410.  
    411. j=0;
    412.  
    413. delay(1000);
    414.  
    415. Restart();
    416.  
    417. }else
    418.  
    419. {
    420.  
    421. }
    422.  
    423. }
    424.  
    425. }
    426.  
    427. wdt_reset();
    428.  
    429. l=0; Clear=0;Write=0; digitalWrite(ledRed,LOW);
    430.  
    431. }
    432.  
    433. /*****************************************************************************************************/  //CLEARING
    434.  
    435. if ( rfid.antiCollision( data ) == MI_OK && Clear==1&&Write==0) {
    436.  
    437. wdt_enable(WDTO_1S);
    438.  
    439. memcpy( uid, data, 5 );
    440.  
    441. for(int i=0;i<4;i++)
    442.  
    443. {
    444.  
    445. RFIDValue[I]= uid[I];
    446.  
    447. }
    448.  
    449. delay(500);
    450.  
    451. for (int i = 0; i < BUFSIZE; i++) {
    452.  
    453. buf[I] = 0;
    454.  
    455. }
    456.  
    457. int j=0,b;
    458.  
    459. for(int i=0;i<=EEPROM_MAX_ADDR;i+=5)
    460.  
    461. {
    462.  
    463. b=i;
    464.  
    465. for(int k=i,m=0;m<4;k++,m++)
    466.  
    467. {
    468.  
    469.  
    470.  
    471. if(uid[m]==EEPROM.read(k))
    472.  
    473. {
    474.  
    475. j=j+1;
    476.  
    477.  
    478.  
    479. }
    480.  
    481. if (j==3)
    482.  
    483. {
    484.  
    485. int memo=b;
    486.  
    487. byte c = 0xFF;
    488.  
    489. for(memo;memo<(b+5);memo++)
    490.  
    491. {
    492.  
    493.  
    494.  
    495. EEPROM.write(memo, c);
    496.  
    497. }
    498.  
    499. b=0;
    500.  
    501. PrintDeleteRFID ();
    502.  
    503. Serial.print(": Card Deleted");
    504.  
    505. LongBuzzer();
    506.  
    507. delay(2000);
    508.  
    509. i=i+EEPROM_MAX_ADDR;
    510.  
    511. j=0;
    512.  
    513. }
    514.  
    515. }
    516.  
    517. }
    518.  
    519. l=0;Clear=0;Write=0;digitalWrite(ledRed,LOW);
    520.  
    521. Restart();
    522.  
    523. }
    524.  
    525. wdt_reset();
    526.  
    527. }
    528.  
    529. }
    530.  
    531. /******************************************************************************************************/
    532.  
    533. void ErrorBeep(String Err)
    534.  
    535. {
    536.  
    537.  
    538. digitalWrite(ledRed, HIGH);
    539.  
    540. digitalWrite(ledGreen, LOW);
    541.  
    542. PrintDeleteRFID ();
    543.  
    544. Serial.print(" :"+ Err);
    545.  
    546. for(int i=0;i<5;i++)
    547.  
    548. {
    549.  
    550. digitalWrite(buzzer, LOW);  // turn the LED on (HIGH is the voltage level)
    551.  
    552. delay(180);  // wait for a second
    553.  
    554. digitalWrite(buzzer, HIGH);  // turn the LED off by making the voltage LOW
    555.  
    556. delay(180);
    557.  
    558. }
    559.  
    560. Restart();
    561.  
    562.  
    563.  
    564. }
    565.  
    566.  
    567. /******************************************************************************************************/
    568.  
    569.  
    570. void eeprom_erase_RFID(int EEPROM_Start_ADDR) {
    571.  
    572. int i;
    573.  
    574. byte b = 0xFF;
    575.  
    576. EEPROM_End_ADDR =EEPROM_Start_ADDR+10;
    577.  
    578.  
    579. for (i = EEPROM_Start_ADDR; i <= EEPROM_End_ADDR; i++) {
    580.  
    581. EEPROM.write(i, b);
    582.  
    583. }
    584.  
    585. }
    586.  
    587. boolean eeprom_is_addr_ok(int addr) {
    588.  
    589. return ((addr >= EEPROM_MIN_ADDR) && (addr <= EEPROM_MAX_ADDR));
    590.  
    591. }
    592.  
    593.  
    594. boolean eeprom_write_bytes(int startAddr, const byte* array, int numBytes) {
    595.  
    596. int i;
    597.  
    598. if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) {
    599.  
    600. return false;
    601.  
    602. }
    603.  
    604. for (i = 0; i < numBytes; i++) {
    605.  
    606. EEPROM.write(startAddr + i, array[I]);
    607.  
    608. }
    609.  
    610.  
    611. return true;
    612.  
    613. }
    614.  
    615. boolean eeprom_read_bytes(int startAddr, byte array[], int numBytes) {
    616.  
    617. int i;
    618.  
    619.  
    620. // both first byte and last byte addresses must fall within
    621.  
    622. // the allowed range
    623.  
    624. if (!eeprom_is_addr_ok(startAddr) || !eeprom_is_addr_ok(startAddr + numBytes)) {
    625.  
    626. return false;
    627.  
    628. }
    629.  
    630.  
    631. for (i = 0; i < numBytes; i++) {
    632.  
    633. array[I] = EEPROM.read(startAddr + i);
    634.  
    635. }
    636.  
    637.  
    638. return true;
    639.  
    640. }
    641.  
    642. boolean eeprom_write_int(int addr, int value) {
    643.  
    644. byte *ptr;
    645.  
    646.  
    647. ptr = (byte*)&value;
    648.  
    649. return eeprom_write_bytes(addr, ptr, sizeof(value));
    650.  
    651. }
    652.  
    653. boolean eeprom_read_int(int addr, int* value) {
    654.  
    655. return eeprom_read_bytes(addr, (byte*)value, sizeof(int));
    656.  
    657. }
    658.  
    659. boolean eeprom_write_string(int addr, const char* string) {
    660.  
    661. // actual number of bytes to be written
    662.  
    663. int numBytes;
    664.  
    665.  
    666. // we'll need to write the string contents
    667.  
    668. // plus the string terminator byte (0x00)
    669.  
    670. numBytes = strlen(string) + 1;
    671.  
    672.  
    673. return eeprom_write_bytes(addr, (const byte*)string, numBytes);
    674.  
    675. }
    676.  
    677. boolean eeprom_read_string(int addr, char* buffer, int bufSize) {
    678.  
    679. byte ch;
    680.  
    681. int bytesRead;
    682.  
    683. if (!eeprom_is_addr_ok(addr)) {
    684.  
    685. return false;
    686.  
    687. }
    688.  
    689. if (bufSize == 0) {
    690.  
    691. return false;
    692.  
    693. }
    694.  
    695. if (bufSize == 1) {
    696.  
    697. buffer[0] = 0;
    698.  
    699. return true;
    700.  
    701. }
    702.  
    703. bytesRead = 0;
    704.  
    705. ch = EEPROM.read(addr + bytesRead);
    706.  
    707. buffer[bytesRead] = ch;
    708.  
    709. bytesRead++;
    710.  
    711. while ( (ch != 0x00) && (bytesRead < bufSize) && ((addr + bytesRead) <= EEPROM_MAX_ADDR) ) {
    712.  
    713. ch = EEPROM.read(addr + bytesRead);
    714.  
    715. buffer[bytesRead] = ch;
    716.  
    717. bytesRead++;
    718.  
    719. }
    720.  
    721. if ((ch != 0x00) && (bytesRead >= 1)) {
    722.  
    723. buffer[bytesRead - 1] = 0;
    724.  
    725. }
    726.  
    727. return true;
    728.  
    729. }
    730.  
    731.  
    732. int start_time;
    733.  
    734. int stop_time;
    735.  
    736. String Str;
    737.  
    738. void start_timing() {
    739.  
    740. start_time = millis();
    741.  
    742. }
    743.  
    744.  
    745. void print_elapsed() {
    746.  
    747. stop_time = millis();
    748.  
    749. Serial.print("Time elapsed (ms): ");
    750.  
    751. Serial.println(stop_time - start_time);
    752.  
    753. }
    754.  
    755.  
    756. void buzzer_trigger()
    757.  
    758. {
    759.  
    760. digitalWrite(ledRed,LOW);
    761.  
    762. digitalWrite(ledGreen,HIGH);
    763.  
    764. digitalWrite(buzzer,HIGH);
    765.  
    766. delay(300);
    767.  
    768. digitalWrite(buzzer,LOW);
    769.  
    770. digitalWrite(ledGreen,LOW);
    771.  
    772. }
    773.  
    774. void LongBuzzer()
    775.  
    776. {
    777.  
    778. digitalWrite(ledRed,LOW);
    779.  
    780. digitalWrite(ledGreen,HIGH);
    781.  
    782. digitalWrite(buzzer,HIGH);
    783.  
    784. delay(1000);
    785.  
    786. digitalWrite(buzzer,LOW);
    787.  
    788. digitalWrite(ledGreen,LOW);
    789.  
    790. }
    791.  
    792.  
    793. void BlinkLed (const int LedRed,const int LedGreen,int mode)
    794.  
    795. {
    796.  
    797.  
    798. unsigned long currentMillis = millis();
    799.  
    800. if(currentMillis - previousMillis >= interval) {
    801.  
    802. previousMillis = currentMillis;
    803.  
    804. if (ledState == LOW)
    805.  
    806. ledState = HIGH;
    807.  
    808. else
    809.  
    810. ledState = LOW;
    811.  
    812. if (mode==1)
    813.  
    814. {
    815.  
    816. digitalWrite(ledGreen, ledState);
    817.  
    818.  
    819.  
    820. }else if (mode==2)
    821.  
    822. {
    823.  
    824. digitalWrite(ledRed, ledState);
    825.  
    826. digitalWrite(buzzer,ledState);
    827.  
    828. }
    829.  
    830. else if (mode==3)
    831.  
    832. {
    833.  
    834.  
    835.  
    836. digitalWrite(ledGreen, ledState);
    837.  
    838. digitalWrite(ledRed, ledState);
    839.  
    840. digitalWrite(buzzer,ledState);
    841.  
    842. }
    843.  
    844. }}
    845.  
    846.  
    847. void ButtonPress()
    848.  
    849. {
    850.  
    851.  
    852. while (digitalRead(ButtonPin)==LOW)
    853.  
    854. {
    855.  
    856. digitalWrite(ledGreen, LOW);
    857.  
    858. digitalWrite(ledRed, LOW);
    859.  
    860. unsigned long currentMillis = millis();
    861.  
    862. if(currentMillis - previousMillis <= 2000&&currentMillis - previousMillis >= 10)
    863.  
    864. {
    865.  
    866. digitalWrite(ledGreen,HIGH);
    867.  
    868. digitalWrite(ledRed, HIGH);
    869.  
    870. Counter++;
    871.  
    872. Write=1;
    873.  
    874. Clear=0;
    875.  
    876. delay(10);
    877.  
    878. }
    879.  
    880. if(currentMillis - previousMillis <=5000 && currentMillis - previousMillis >= 2000)
    881.  
    882. {
    883.  
    884. digitalWrite(ledGreen, LOW);
    885.  
    886. digitalWrite(ledRed, HIGH);
    887.  
    888. Write=0;
    889.  
    890. Clear=1;
    891.  
    892. delay(10);
    893.  
    894. }
    895.  
    896. }
    897.  
    898.  
    899. previousMillis = 0;
    900.  
    901. digitalWrite(ledGreen, LOW);
    902.  
    903. digitalWrite(ledRed, LOW);
    904.  
    905. }
    906.  
    907.  
    908. void Delay()
    909.  
    910. {
    911.  
    912. unsigned long currentMillis = millis();
    913.  
    914. if(currentMillis - CpreviousMillis > DelayTime) {
    915.  
    916. // save the last time you blinked the LED
    917.  
    918. CpreviousMillis = 0;
    919.  
    920. wdt_enable(0);
    921.  
    922. wdt_reset();
    923.  
    924. if (Write==1)
    925.  
    926. {
    927.  
    928. Write=0;
    929.  
    930. }
    931.  
    932. else if (Clear==1)
    933.  
    934. {
    935.  
    936. Clear=0;
    937.  
    938.  
    939. }
    940.  
    941. }
    942.  
    943. }
    944.  
    945.  
    946. void Restart()
    947.  
    948. {
    949.  
    950.  
    951. wdt_enable(0);
    952.  
    953. wdt_reset();
    954.  
    955. }
    956.  
    957.  
    958. void PrintRFID (int loc)
    959.  
    960. {
    961.  
    962.  
    963.  
    964. for(int i=loc;i<loc+4;i++)
    965.  
    966. {
    967.  
    968. Serial.print(byte(EEPROM.read(i)),HEX);
    969.  
    970. RFIDValue[I]= EEPROM.read(i);
    971.  
    972.  
    973.  
    974. }
    975.  
    976.  
    977.  
    978. }
    979.  
    980.  
    981. void PrintDeleteRFID ()
    982.  
    983. {
    984.  
    985. for(int i=0;i<4;i++)
    986.  
    987. {
    988.  
    989. Serial.print(byte(RFIDValue[I]),HEX);
    990.  
    991.  
    992.  
    993.  
    994.  
    995. }
    996.  
    997.  
    998.  
    999. }
    1000. [/I][/I][/I][/I][/I][/I][/I][/I][/I][/I][/I][/I][/I]
    1001.  
    Regards,
    Ajin nadh
     
    Last edited by a moderator: Jan 7, 2016
  4. DickCappels

    Moderator

    Aug 21, 2008
    2,648
    632
    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?
     
  5. AJIN NADH

    Thread Starter Member

    Dec 18, 2014
    79
    4

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

    Moderator

    Aug 21, 2008
    2,648
    632
  7. AJIN NADH

    Thread Starter Member

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

    Code (C):
    1.  
    2. if (Serial.available()>0)
    3.   {
    4.    String str= Serial.readString();
    5.    Serial.println(str);
    6.    if(str=="AT+C")
    7.  
    8.    {
    9.  
    10.    Serial.println("Empty Database");
    11.     eeprom_erase_all();
    12.     LongBuzzer();
    13. }
    14.    }
    15.  
    So i disabled this section, Thanks for the support.

    Ajin nadh

    Mod note: added code tags
     
    Last edited by a moderator: Jan 7, 2016
  8. DickCappels

    Moderator

    Aug 21, 2008
    2,648
    632
    I am glad you found the problem and solved it.
     
  9. AJIN NADH

    Thread Starter Member

    Dec 18, 2014
    79
    4
    Thank you sir
     
Loading...