View previous topic :: View next topic |
Author |
Message |
wangine
Joined: 07 Jul 2009 Posts: 98 Location: Curtea de Arges, Romania
|
Expecting a structure/union error |
Posted: Thu Sep 10, 2015 1:05 pm |
|
|
I try to search 1 day for this error but without result, in PICs manual don't explain about this. The structure looks like that
Code: |
typedef union {
unsigned int16 u16; // element specifier for accessing whole u16
signed int16 i16; // element specifier for accessing whole i16
struct {
unsigned int8 u8L; // element specifier for accessing low u8
unsigned int8 u8H; // element specifier for accessing high u8
} s16; // element spec. for acc. struct with low or high u8
} nt16; |
or like that
Code: | typedef union {
union{
unsigned int16 u16; // element specifier for accessing whole u16
signed int16 i16; // element specifier for accessing whole i16
struct {
unsigned int8 u8L; // element specifier for accessing low u8
unsigned int8 u8H; // element specifier for accessing high u8
} s16; // element spec. for acc. struct with low or high u8
};
} nt16; |
or like that
Code: | typedef union {
union{
unsigned int16 u16; // element specifier for accessing whole u16
signed int16 i16; // element specifier for accessing whole i16
struct {
unsigned int8 u8L; // element specifier for accessing low u8
unsigned int8 u8H; // element specifier for accessing high u8
} s16; // element spec. for acc. struct with low or high u8
};
} nt16__;
nt16__ nt16; |
and the function that access this structure
Code: |
pMeasurand->s16.u8H = data[0] = I2c_ReadByte(0);
pMeasurand->s16.u8L = data[1] = I2c_ReadByte(0);
|
Can everyone say me what i am do wrong ? If need i can post entire function, the controller is 18f4550 ![Embarassed](images/smiles/icon_redface.gif) |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 10, 2015 7:34 pm |
|
|
Your code doesn't even compile. It gives the "unidentified identifier" error
for this: pMeasurand
Post a test program. And post your compiler version. |
|
![](templates/subSilver/images/spacer.gif) |
wangine
Joined: 07 Jul 2009 Posts: 98 Location: Curtea de Arges, Romania
|
|
Posted: Fri Sep 11, 2015 7:45 am |
|
|
Has my mistake, too late, tired and frustrated i put one letter more,
Code: | typedef union {
unsigned int16 u16; // element specifier for accessing whole u16
signed int16 i16; // element specifier for accessing whole i16
struct {
unsigned int8 u8L; // element specifier for accessing low u8
unsigned int8 u8H; // element specifier for accessing high u8
} s16; // element spec. for acc. struct with low or high u8
} nt16;
|
and code who acces that
Code: |
//===========================================================================
int8 SHT2x_MeasureHM(etSHT2xMeasureType eSHT2xMeasureType, nt16 *pMeasurand)
//===========================================================================
{
int8 checksum; //checksum
int8 data[2]; //data array for checksum verification
int8 error=0; //error variable
int16 i; //counting variable
//-- write I2C sensor address and command --
I2c_StartCondition();
error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr
switch(eSHT2xMeasureType)
{ case HUMIDITY: error |= I2c_WriteByte (TRIG_RH_MEASUREMENT_HM); break;
case TEMP : error |= I2c_WriteByte (TRIG_T_MEASUREMENT_HM); break;
default: break;
// default: assert(0);
}
//-- wait until hold master is released --
I2c_StartCondition();
error |= I2c_WriteByte (I2C_ADR_R);
output_float(scl_pin); // set SCL I/O port as input
for(i=0; i<1000; i++) // wait until master hold is released or
{ DelayMicroSeconds(1000); // a timeout (~1s) is reached
if (input_state(scl_pin) == true) break;
}
//-- check for timeout --
if(input_state(scl_pin)==false) error |= TIME_OUT_ERROR;
//-- read two data bytes and one checksum byte --
pMeasurand->s16.u8H = data[0] = I2c_ReadByte(ACK);
pMeasurand->s16.u8L = data[1] = I2c_ReadByte(ACK);
checksum=I2c_ReadByte(NO_ACK);
//-- verify checksum --
error |= SHT2x_CheckCrc (data,2,checksum);
I2c_StopCondition();
return error;
} |
my mistake was here
Code: | int8 SHT2x_MeasureHM(etSHT2xMeasureType eSHT2xMeasureType, int16 *pMeasurand) |
i put in mistake int16 insted of nt16
Code: | int8 SHT2x_MeasureHM(etSHT2xMeasureType eSHT2xMeasureType, nt16 *pMeasurand) |
Is strange error, normal was a syntax error but the compiler know the int16 and return other error. Thanks |
|
![](templates/subSilver/images/spacer.gif) |
jeremiah
Joined: 20 Jul 2010 Posts: 1329
|
|
Posted: Fri Sep 11, 2015 9:00 am |
|
|
That would make sense. It is a normal error. Your function prototype indicated a pointer to an int16, so when you used the -> operator on it, it flagged it as an error as you would only do that with a struct or union pointer, not an int16 pointer. Remember, the compiler wouldn't know you intended have a struct or a union until you try to use it as one. Up until that point, it thinks you are using an int16. It can't see into the future unfortunately. |
|
![](templates/subSilver/images/spacer.gif) |
|