• AVR Freaks

AnsweredHot!Preprocessor variables wrongly defined in Microchip's C99 header files

Author
sxanthony06
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2018/06/09 11:50:30
  • Location: Curacao
  • Status: offline
2019/07/20 06:25:02 (permalink)
0

Preprocessor variables wrongly defined in Microchip's C99 header files

I want to preface a possible discussion in this thread by saying that I don't have extensive knowledge about C so my issue might seem trivial because I might be missing something. Honestly, it seems kind of clear to me but ya never know.
So in my project using Bosch sensors' BMP180 API, I am using the #define directive to create a few macro's:
/*! @brief Macros holding the bound values for the temperature in degree celsius */
#define BMP180_ST_PLAUSIBLE_TEMP_MIN UINT8_C(0)
#define BMP180_ST_PLAUSIBLE_TEMP_MAX UINT8_C(45)
    
/*! @brief Macros holding the bound values for the pressure in hPa */
#define BMP180_ST_PLAUSIBLE_PRESS_MIN UINT16_C(900)
#define BMP180_ST_PLAUSIBLE_PRESS_MAX UINT16_C(1100)
 
#define BMP180_ST_TEMPERATURE_RESOLUTION UINT8_C(10)
#define BMP180_ST_PRESSURE_RESOLUTION    UINT8_C(100)

 
These macro's I use in the following conditional where pressure is uint32_t variable:
if ((pressure < (BMP180_ST_PLAUSIBLE_PRESS_MIN * BMP180_ST_PRESSURE_RESOLUTION)) ||
                (pressure > (BMP180_ST_PLAUSIBLE_PRESS_MAX * BMP180_ST_PRESSURE_RESOLUTION)))
            /* implausible pressure */

 
However, my compiler is giving me the following error:
warning: overflow in expression; result is -21072 with type 'int' [-Winteger-overflow]
which mathematically as I see it doesn't make sense since both are supposed to be unsigned long and the result can't go higher than 100000.
So I went to check UINT32_C() definition in xc8 v2.05 included header files and I saw the following:
#define UINT8_C(c) c
#define UINT16_C(c) c
#if defined(__NEED_uint24_t)
#define UINT24_C(v) (v ## UL)
#endif
#ifdef __XC8__
#define UINT32_C(c) c ## UL
#else
#define UINT32_C(c) c ## U
#endif

 
Now check the preprocessor variables in bold. This is why I added that preface, because according to me these are wrongly defined. The MPLAB XC8 C Compiler User Guide for PIC pdf that comes with the download of xc8 v2.05 defines a few preprocessor macros that indicate some of the used compiler info. They are on page 162 and 163. They are __XC8, __XC, __XC_MODE_ and __XC_VERSION. There is no __XC8__ anywhere in the guide.  I think that this variable was from a previous xc compiler, maybe a version predating v2.0.
 
Anyway, if I remove the UINT_C macro and just put UL after the constants, the compiler warning goes away. Question is, am I missing something?
post edited by sxanthony06 - 2019/07/20 07:07:55
#1
1and0
Access is Denied
  • Total Posts : 9989
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Preprocessor variables wrongly defined in Microchip's C99 header files 2019/07/20 06:57:40 (permalink)
0
Why are you even use those macros?  What value is  BMP180_ST_PRESSURE_RESOLUTION?
 
#2
sxanthony06
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2018/06/09 11:50:30
  • Location: Curacao
  • Status: offline
Re: Preprocessor variables wrongly defined in Microchip's C99 header files 2019/07/20 07:09:36 (permalink)
0
I forgot to include them in the post, I just added it. I didn't define those macros. To be clear, I am using the Bosch sensor API mostly as is. The API is not made to work exclusively with PIC microcontrollers, so a lot of stuff are added for portability to many platforms.
#3
1and0
Access is Denied
  • Total Posts : 9989
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Preprocessor variables wrongly defined in Microchip's C99 header files 2019/07/20 07:20:37 (permalink) ☼ Best Answerby sxanthony06 2019/07/20 09:34:14
+2 (2)
   (BMP180_ST_PLAUSIBLE_PRESS_MIN * BMP180_ST_PRESSURE_RESOLUTION)
= 900 * 100
= 90000
is uint16*uint16 which overflows an uint16 of 65535 max.
is int16*int16 which overflows an int16 of 32767 max.

   (BMP180_ST_PLAUSIBLE_PRESS_MAX * BMP180_ST_PRESSURE_RESOLUTION)
= 1100 * 100
= 110000
again is uint16*uint16 which overflows an uint16 of 65535 max.
again is int16*int16 which overflows an int16 of 32767 max.
 
You got two options:
1. Change those #define to UL.
2. Cast the macro to (uint32_t) when using them.
post edited by 1and0 - 2019/07/20 07:27:33
#4
sxanthony06
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2018/06/09 11:50:30
  • Location: Curacao
  • Status: offline
Re: Preprocessor variables wrongly defined in Microchip's C99 header files 2019/07/20 07:28:35 (permalink)
0
Ok, I was thinking about that just now. Thanks for the reply. I tested UINT32_C() and UL, both worked. So I am kind of confused of where __XC8__ is defined in the header files.
post edited by sxanthony06 - 2019/07/20 07:32:42
#5
1and0
Access is Denied
  • Total Posts : 9989
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Preprocessor variables wrongly defined in Microchip's C99 header files 2019/07/20 07:47:14 (permalink)
+1 (1)
sxanthony06
So I am kind of confused of where __XC8__ is defined in the header files.

It is mentioned in Table 2-1 on page 39 of the legacy XC8's User Guide.
 
#6
Jump to:
© 2019 APG vNext Commercial Version 4.5