saving an array in EEPROM

594 views Asked by At

I've a vectorx[8] and I want to save its value in the EEPROM of an XMEGA for that here is what I've done ::

............................
    int16_t  vec1[8]; 
    int16_t  vec2[8]; 
    int16_t  vec3[8];
    int i =  0, j =1; 
    for ( i =0 ; i<8; i++){ // ini 
       vec1[i] = 23500; 
       vec2[i] = 20000;
       vec3[i] = 20000;
    }


for ( i =0; i<8 ; i++) {
 eeprom_update_word (( uint16_t *)j++, vec1[i]);
 eeprom_update_word (( uint16_t *)j++, vec2[i]);
 eeprom_update_word (( uint16_t *)j++, vec3[i]);
}

Now when I try to use the saved values using this : ................................

int16_t tempX[8];
int j  =2 ;
for ( i = 0 ; i < NUMBEROFSENSORS ; i++ ){

        tempX[i] = (int16_t) eeprom_read_word(j);
        j=  j+3;  // reading only vec1 
        printf(" j  read Value is : %d  \n",(int16_t)tempX[i]);
    }

I get -13158 instead of 23500 , so my question is what I doing wrong here ?

âfter changing K to a point I I get this output :

read Value is : 2627 

read Value is : 2714 read Value is : 2714 read Value is : 2714 read Value is : 23450 read Value is : -3584 read Value is : 31744 read Value is : 11008

1

There are 1 answers

1
i486 On BEST ANSWER

Define j as uint16_t * j. Then ++ operation will increase it properly with 2 and not 1. And you will not need casting: eeprom_update_word (j++, vec1[i]).