|
|
View previous topic :: View next topic |
Author |
Message |
Thomas Hauff Guest
|
How are int16 and int32 stored?? |
Posted: Thu May 09, 2002 6:47 am |
|
|
I need a sanity check.
I though that if i use a pointer to point to the address of a int16 or a int32 it would point to the MSB byte.
How are int16 and int32 stored?
For int16 MSB byte (pointer address) LSB byte (pointer address +1)?
For int32 MSB byte (pointer address) byte (pointer address +1)
byte (pointer address +2) LSB byte (pointer address +3)?
This what i'm up to.
The below code fragments are to write a int16 to EEPROM.
I have a cast from int32 to int16 that i don't know that is doing what i want it to.
What i'm see is that if the value passed to the write function is 1B1h in EEPROM address 01 is 1B address 02 is 01.
So the read gets 1B01h not the 1B1h.
Am i missing some thing??
int32 value1;
float value2;
int16 value3;
int16 value4;
long cal_value[10];
int32 avg_counter;
int cal_mode;
int overload;
void EEPROM_write(int address,int16 data) { //Sub to write a long value to the EEPROM
int i; //i is a loop counter; address is the eeprom offset address
for (i = 0; i < 2; i++) //Set up loop to write the two address with the data
write_eeprom (i + address, *(&data +i)); //Write data to the eeprom using the address point to by &data + 1
}
int16 EEPROM_read(int address) { //Sub to read a int16 value from the EEPROM
int i; //i is a loop counter; address is the eeprom address that has the data to read
int16 data; //the value at the eeprom address
for (i = 0; i < 2; i++) //Set up loop to read the two address with the data
*(&data + 1) = read_eeprom (i + address); //put the data from the eeprom in the address pointed to by &data
return(data); //Return the data to the calling sub
}
Cal_Voltage:
value3 ++; //int the array pointer for reading of the voltage or current set #
cal_mode ++; //inc eeprom address value for next voltage put; Start is 1
value1 = avg_counter = 0;
for (avg_counter = 0; avg_counter < 5000; avg_counter ++) //Read and avrage voltage reading on AN0
value1 += read_adc();
//Example if the reading is 946 for value1 then 946*5000=4730000
//The next three lines are used to store the cal value (int16) for storage in two EEPROM locations using
//
value1 = value1/avg_counter;
//value4 = (int16) (value1/avg_counter); //Value2=(Value1=4730000/avg_count=5000)=int16=946
//946 is the value that will be stored and read out if the EEPROM
EEPROM_write(cal_mode, (int16) Value1); //Call EEPROM sub to write int16 to the EEPROM
cal_mode ++; //inc used eeprom address part two of the first address put voltage
//This will loop three times for the three voltages of 125,115 and 100.
//So this will store three values for the three voltages.
//this IF statement causes a loop unit all three voltages have been calibrated
if (cal_mode<6)
goto Cal_Voltage;
avg_counter=1; //Setup array pointer for loop and put cal_value array
value3 = 1; //Setup pointer for EEPROM address pointer for the reading of the data and put in array
//This loop will read the cal values from the EEPROM
while (avg_counter < 10)
{
cal_value [avg_counter] = EEPROM_read((int) Value3); //Call EEPROM read and store in cal_value array
value3 = value3 + 2; //Increment EEPROM address by two for the next value
avg_counter++; //Increment the loop for the next read and write
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 4256 |
|
|
Tomi Guest
|
Re: How are int16 and int32 stored?? |
Posted: Thu May 09, 2002 10:16 am |
|
|
The storing method is little-endian (Moto uses -and you are about- big-endian) so the pointer points to the least significant byte:
int16 var16;
int32 var32;
float fval;
#byte var16L = var16
#byte var16H = var16+1
#byte var32L = var32
#byte var32H = var32+1
#byte var32HL = var32+2
#byte var32HH = var32+3
#byte fvalL = fval
#byte fvalH = fval+1
#byte fvalHL = fval+2
#byte fvalHH = fval+3
So if you have a struct e.g.:
typedef struct {
int16 val1;
float val2;
} mystruct;
mystruct myvar;
char *p = &myvar;
After this line p points to the least significant byte of the val1.
:=I need a sanity check.
:=I though that if i use a pointer to point to the address of a int16 or a int32 it would point to the MSB byte.
:=How are int16 and int32 stored?
:=For int16 MSB byte (pointer address) LSB byte (pointer address +1)?
:=For int32 MSB byte (pointer address) byte (pointer address +1)
:=byte (pointer address +2) LSB byte (pointer address +3)?
:=This what i'm up to.
:=The below code fragments are to write a int16 to EEPROM.
:=I have a cast from int32 to int16 that i don't know that is doing what i want it to.
:=What i'm see is that if the value passed to the write function is 1B1h in EEPROM address 01 is 1B address 02 is 01.
:=So the read gets 1B01h not the 1B1h.
:=Am i missing some thing??
:=
:=int32 value1;
:=float value2;
:=int16 value3;
:=int16 value4;
:=long cal_value[10];
:=
:=int32 avg_counter;
:=int cal_mode;
:=int overload;
:=
:=void EEPROM_write(int address,int16 data) { //Sub to write a long value to the EEPROM
:= int i; //i is a loop counter; address is the eeprom offset address
:= for (i = 0; i < 2; i++) //Set up loop to write the two address with the data
:= write_eeprom (i + address, *(&data +i)); //Write data to the eeprom using the address point to by &data + 1
:= }
:=
:=int16 EEPROM_read(int address) { //Sub to read a int16 value from the EEPROM
:= int i; //i is a loop counter; address is the eeprom address that has the data to read
:= int16 data; //the value at the eeprom address
:= for (i = 0; i < 2; i++) //Set up loop to read the two address with the data
:= *(&data + 1) = read_eeprom (i + address); //put the data from the eeprom in the address pointed to by &data
:= return(data); //Return the data to the calling sub
:=}
:=
:=Cal_Voltage:
:= value3 ++; //int the array pointer for reading of the voltage or current set #
:= cal_mode ++; //inc eeprom address value for next voltage put; Start is 1
:= value1 = avg_counter = 0;
:=
:= for (avg_counter = 0; avg_counter < 5000; avg_counter ++) //Read and avrage voltage reading on AN0
:= value1 += read_adc();
:=//Example if the reading is 946 for value1 then 946*5000=4730000
:=//The next three lines are used to store the cal value (int16) for storage in two EEPROM locations using
:=//
:= value1 = value1/avg_counter;
:= //value4 = (int16) (value1/avg_counter); //Value2=(Value1=4730000/avg_count=5000)=int16=946
:= //946 is the value that will be stored and read out if the EEPROM
:= EEPROM_write(cal_mode, (int16) Value1); //Call EEPROM sub to write int16 to the EEPROM
:= cal_mode ++; //inc used eeprom address part two of the first address put voltage
:=
:=//This will loop three times for the three voltages of 125,115 and 100.
:=//So this will store three values for the three voltages.
:=
:=//this IF statement causes a loop unit all three voltages have been calibrated
:= if (cal_mode<6)
:= goto Cal_Voltage;
:=
:=
:= avg_counter=1; //Setup array pointer for loop and put cal_value array
:= value3 = 1; //Setup pointer for EEPROM address pointer for the reading of the data and put in array
:=
:=//This loop will read the cal values from the EEPROM
:= while (avg_counter < 10)
:= {
:= cal_value [avg_counter] = EEPROM_read((int) Value3); //Call EEPROM read and store in cal_value array
:= value3 = value3 + 2; //Increment EEPROM address by two for the next value
:= avg_counter++; //Increment the loop for the next read and write
:= }
___________________________
This message was ported from CCS's old forum
Original Post ID: 4260 |
|
|
Thomas Hauff Guest
|
Re: How are int16 and int32 stored?? |
Posted: Thu May 09, 2002 10:57 am |
|
|
Thanx for the help
This all makes sense.
I cut my teeth on Motorola chips.
Thanx
___________________________
This message was ported from CCS's old forum
Original Post ID: 4261 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|