Combining 8 bit integers to 32 bit long integer

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
dariods
Posts: 25
Joined: 26 Apr 2023 07:12

Combining 8 bit integers to 32 bit long integer

#1 Post by dariods » 10 Aug 2023 10:41

I have split a 32 bit long integer to 8 bits and saved to 4 locations on the EEPROM. The For loop code doesn't work, there seems to be problem with "Distance |=" but I do not know how to solve it. Please guide

Code: Select all

// PIC16F886
void read_saved_odo(){
   unsigned long int Distance = 12345678;
   unsigned int i;
   unsigned int j = 11;   
  
   
    // this does not work
    for(i = 3; i = 0; i--){
    Distance |= (unsigned long int)((EEPROM_Read(j)) << (i * 8));
    j--;
     } 
}

michig54
Posts: 2
Joined: 13 Aug 2023 03:04

Re: Combining 8 bit integers to 32 bit long integer

#2 Post by michig54 » 13 Aug 2023 15:13

Why are you initializing Distance to 12345678? The |= will or any values read from the eeprom with this value. You should probably initialize it to 0, unless this is your intention.

paulfjujo
Posts: 1558
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: Combining 8 bit integers to 32 bit long integer

#3 Post by paulfjujo » 13 Aug 2023 19:17

hello,

where do you store data in eeprom ; wich adresse ..?
we can suppose start adresse is 11 ?
remark: a pair adresse is always better ...;

Code: Select all

 unsigned char *p1;

 p1=&Distance;
 for(i = 0; i<3; i++)
 {
   *(p1+i)=EEPROM_Read(j+i);
 }
but not checked !!!

Post Reply

Return to “mikroC PRO for PIC General”