Search code examples
cavr

Reading from EEPROM


Say I have 3 vectors :

int16_t v1[8];
int16_t v2[8];
int16_t v3[8];
int8_t lock = 0; 

I want to save the values of the arrays in the EEPROM. For that, here is what I've done:

int i = 0; 
uint16_t * j =  (uint16_t*) 2 ;
short ratio=0;  
for ( i =0; i<8;i++){


    v1[i] = 22500;
    v2[i] = 10500;
    v3[i] = 20888;
}

I want save the values like:

//  .startat at the 3rd byte 

//  v1[0]v2[0]v3[0]v1[1]v2[1]v3[1] ----------> v1[7]v2[7]v3[7]



for ( i = 0 ; i <8; i++ ){
    //printf("j = %d \n",j);
    eeprom_update_word (j++, v1[i]);
    eeprom_update_word (j++, v2[i]);
    eeprom_update_word (j++, v3[i]);
}

To check that the values were correctly saved, I tried to print them out like :

for (i=1; i < 26;i++ ){
    ratio =(short)eeprom_read_word((uint8_t*)i);
    printf(" WORD  %d     %d \n", i,ratio);
}

and I really don't understand the output:

 WORD  1     -7168
 WORD  2     22500
 WORD  3     1111
 WORD  4     10500
 WORD  5     -26583
 WORD  6     20888
 WORD  7     -7087
 WORD  8     22500
 WORD  9     1111
 WORD  10     10500
 WORD  11     -26583
 WORD  12     20888
 WORD  13     -7087
 WORD  14     22500
 WORD  15     1111
 WORD  16     10500
 WORD  17     -26583
 WORD  18     20888
 WORD  19     -7087
 WORD  20     22500
 WORD  21     1111
 WORD  22     10500
 WORD  23     -26583
 WORD  24     20888
 WORD  25     -7087

Any idea how to get this the correct way?

I'v eexpend the for loop and the output looks half correct ! ! :

 WORD  1     -7168
 WORD  2     22500
 WORD  3     -4009
 WORD  4     22000
 WORD  5     9813
 WORD  6     21030
 WORD  7     -7086
 WORD  8     22500
 WORD  9     -4009
 WORD  10     22000
 WORD  11     9813
 WORD  12     21030
 WORD  13     -7086
 WORD  14     22500
 WORD  15     -4009
 WORD  16     22000
 WORD  17     9813
 WORD  18     21030
 WORD  19     -7086
 WORD  20     22500
 WORD  21     -4009
 WORD  22     22000
 WORD  23     9813
 WORD  24     21030
 WORD  25     -7086
 WORD  26     22500
 WORD  27     -4009
 WORD  28     22000
 WORD  29     9813
 WORD  30     21030
 WORD  31     -7086
 WORD  32     22500
 WORD  33     -4009
 WORD  34     22000
 WORD  35     9813
 WORD  36     21030
 WORD  37     -7086
 WORD  38     22500
 WORD  39     -4009
 WORD  40     22000
 WORD  41     9813
 WORD  42     21030
 WORD  43     -7086
 WORD  44     22500
 WORD  45     -4009
 WORD  46     22000
 WORD  47     9813
 WORD  48     21030
 WORD  49     82
 WORD  50     0
 WORD  51     0

I cant explain what happens here !

**UPDATE ** after changing the loop to :

for (i=1; i < 25;i++ ){
            ratio =eeprom_read_word(j);
            printf(" WORD  %d     %d \n", i,ratio);
            j  = j +2; 
        }

the output is now :

 WORD  1     22500
 WORD  2     21030
 WORD  3     22000
 WORD  4     22500
 WORD  5     21030
 WORD  6     22000
 WORD  7     22500
 WORD  8     21030
 WORD  9     22000
 WORD  10     22500
 WORD  11     21030
 WORD  12     22000
 WORD  13     22500
 WORD  14     22000
 WORD  15     0
 WORD  16     0
 WORD  17     22500
 WORD  18     22000
 WORD  19     0
 WORD  20     0
 WORD  21     22500
 WORD  22     22000
 WORD  23     0
 WORD  24     0

still I don't get where the 0s come from ?


Solution

  • You have 24 16-bit values, 48 bytes total, stored in EEPROM starting at address 2, and the last byte is stored at address 49. Reading these bytes, you start at address 1, which you have not populated. You increment by 1 but read two bytes each time, right?

    I would recommend allocating i as a short* type, so incrementing goes up by sizeof(short). Plus I'd fix that start address for reading.

    Oh, for 24 values, the loop end value needs fixing.