I've tried something like this but it just flags the warning all the time...
Something to keep in mind is that the preprocessor can't see "compiler space" declarations, and it doesn't flag any errors when you violate that so things don't work as expected.
I'm constantly forgetting about that myself, and I always run afoul of it wondering why it's "not working".
Any of the '#<whatever>' preprocessor statements only work with other '#' definitions like #const, #var, #define, #option, etc. For example,
Code: Select all
const a = 10
#if (a <> 10)
#warning "test"
#endif
will never work since 'const a' isn't seen by the preprocessor... you'd have to use '#const a = 10'
On trick I use to check compile-time settings is to use macro functions not to generate any code, but just to verify things. Here's an example...
Code: Select all
clock = 48
const FOSC = _clock * 1000000 // get rid of a zero here to see the macro pass
const PWMRATE = 100
const TimerRValue as longint = 65536 - ( Fosc / PWMRate )
const TimerReloadValue as word = TimerRValue
// compile-time macro for checking various parameters
const
etError = 0,
etWarning = 1
macro CheckTimerReloadValues()
if (TimerRValue < 0) then
checkparam(etWarning, "Timer reload is negative")
endif
if (TimerRValue <> TimerReloadValue) then
checkparam(etError, "Timer reload inaccuracy")
endif
end macro
// validate timer reload values
// note: since a macro "generates code" the compiler is finicky about where
// you put this statement. if there are variable declarations after it you'll get
// a 'symbol not expected' error, so I usually put it in the "code" section
// towards the end of the module, or in the "module init" section at the very end
CheckTimerReloadValues()
The nice thing about that is you can do stuff in the macro that you can't use with '#if'.
Here's some of the items you can use with the macro CheckParam() facility
Code: Select all
// values that can be used in CheckParam()
Const
cpConst = $01,
cpVariable = $02,
cpArray = $04,
cpSize01 = $08,
cpSize08 = $10,
cpSize16 = $20,
cpSize32 = $40,
cpInteger = $0100,
cpReal = $0200,
cpString = $0400,
cpChar = $0800,
cpBoolean = $1000,
etError = 0,
etWarning = 1,
etMessage = 2,
etHint = 3
//example
Macro movlw(val)
if not CheckParam(val, cpConst or cpInteger or cpsize01 or cpSize08) then
CheckParam(etError, "Value must be an 8 bit constant")
endif
asm
movlw val
end asm
End Macro