0

I am writing this program to write byte of data to each memeory location and then read and compare the stored bytes. However, when i am writing the data through FOR loop, the loop ends after 749 Writes and reset the Micro. i am resetting the WDT so that shouldnt be an issue. its a fairly simple programme and I was expecting it to work smoothly. here is the code:

I am using PIC24FJ256GB210 with 24LC512K serial EEPROM.

void memorytest2 (void)
{

    unsigned int number;  
    unsigned int data =100;

    ResetCOP();

    if (loop == 1)
    {


        for (number=500; number < 1500; number++)
        {
            ResetCOP();
            WriteEEByte(0xAA, EEPROMStart+data);
        }
        ResetCOP();
        data++;

    }

    loop =0;
    data =0;
    number =500;
}

void WriteEEByte (unsigned char source,unsigned int dest)
{
    I2C2CONbits.RCEN = 0;       // disable rx MODE AS MASTER
    StartWrite(dest);           // start write process at address y
    I2C2TRN = source;           // put data in buffer
    while (I2C2STATbits.TRSTAT);// wait for transmit to complete - including ack
    I2C2CONbits.PEN = 1;        // send stop condition to terminate write
    while (I2C2CONbits.PEN);
    ReadBusy();                 //write delay (~5mS)
}
void ResetCOP (void)
{
    asm("clrwdt");
}
Dan1138
  • 1,150
  • 8
  • 11
Jamal2189
  • 21
  • 2
  • 1
    If you have some pins for debugging, you could use them to see in which loop your code is stuck, if any. – the busybee Jun 22 '20 at 06:09
  • "i am resetting the WDT so that shouldnt be an issue" How do you know? Errors like these are very commonly caused by WDT reset. What is the specified erase + write times of the eeprom and what is the WDT timeout? Does the MCU support viewing the reset cause? – Lundin Jun 22 '20 at 08:25
  • 1
    You could look at the bits in the RCON register of the PIC24FJ256GB210 to determine what event caused the reset to occur. – Dan1138 Jun 22 '20 at 17:49
  • 1.)Why you are writing to same eeprom address 1000 times using for loop. You may exhaust write cycles to eeprom with this type of sample code. 2.) How you are telling that loop is running for 749 times before controller reset? – Babajan Jun 29 '20 at 21:10

0 Answers0