IAR AVR #if #elif problem

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to do the following in IAR AVR :

#ifdef __IOM16_H
  #define __disable_watchdog() WDTCR |= (1<<WDTOE);  WDTCR &= ~(1<<WDE);
#elif __IOM8515_H
  #define __disable_watchdog() WDTCR |= (1<<WDCE); WDTCR &= ~(1<<WDE);
#else
  #error your processor is not listed, Atmega8515 and ATmega16 are supported, add your own lines.
#endif

The compiler reports an error:
Error[Pe029]: expected an expression

When I modify

#elif __IOM8515_H

to

#elif defined(__IOM8515_H)

the error goes away, what is this all about? Why should I use "defined(...)"?

Also, I noticed another thing:

#define RELOAD_TIMER0 ((__UNSIGNED_CHAR_MAX__ - (XTAL_CPU / 64 * 1e-3)))

#if (RELOAD_TIMER0 < 255)
#error Out of range
#error internal algorithm error
#endif

The compiler reports an error:
Error[Pe029]: expected an expression

It doesn't want to do the compare..! How can I then do it?

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#if expression ---- executes if the expression is non-zero
#ifdef symbol ---- executes if the symbol is in the symbol table

defined(symbol) evaluates to true if symbol is in the table.

Personally I prefer using #if defined() to #ifdef.

You can create some complex expressions with #if.
#ifdef takes one argument and that's all.

Incidentally, there are better ways of detecting a MCU in IAR. Your method only works if you have actually included a specific header file.

#if defined(__ATmega8515__)

Note that IAR changes its project file format with every release. It has also changed its predefined MCU symbol several times over the years.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Interesting, in the header file of iom8515, you will find:

#if !defined(__IOM8515_H) || defined(__IAR_SYSTEMS_ASM__)
#define __IOM8515_H

This is why I used it :)

Still I need to see how to do this:

#define RELOAD_TIMER0 ((__UNSIGNED_CHAR_MAX__ - (XTAL_CPU / 64 * 1e-3)))

#if (RELOAD_TIMER0 > 255)
#error Out of range
#error internal algorithm error
#endif

I saw that this is possible in Keil compiler while coding for an ARM, thought it was the case with IAR :(

But, I still think that the documentation of IAR lacks the good examples.

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Look in for the current predefined MCU symbols.

You very seldom need to use a RELOAD_TIMER0 value.
The AVR has a CTC mode that lets the hardware RELOAD perfectly.

All the same, I think recognise your conditional.
It obviously requires that all its constituent symbols are already defined. e.g. you need to #include the system header files first.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

All the same, I think recognise your conditional.
It obviously requires that all its constituent symbols are already defined. e.g. you need to #include the system header files first.

David.

You are right, ioavr.h
thanks a lot david. But honestly, I did not understand the last part. what system header file that allows me to do such comparison?

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Think about it. You need to know __UNSIGNED_CHAR_MAX__ and XTAL_CPU before you can evaluate RELOAD.

XTAL_CPU is probably defined from the F_CPU that you put in your Makefile.
The __UNSIGNED_CHAR_MAX__ will come from
or similar system header file but UCHAR_MAX sounds a more likely name.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

this is not the problem indeed. I think both are found in the limits file. But the problem is that IAR refuses to compile a comparison directive.

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please explain your theory, preferably with a compilable example.

IAR obeys the standard rules for the preprocessor.

The condition will be evaluated and the following code preprocessed.

I do not know what you mean by the "comparison" directive.

The expression understands operators and follows the standard arithmetic rules and precedence. e.g. > < <= >= == etc.

If you have trouble with the preprocessor, chose Options -> C Compiler -> Preprocessor -> Preprocessor output to file.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've been burned too many times with various compilers and "#ifdef foobar"

seems better to use an equation like

#if (ITEM_NAME != 0)

or some such, so that ITEM_NAME must exist, rather than some random typo hosing you up.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define RELOAD_TIMER0 ((255 - (XTAL_CPU / 64 * 1e-3))) 

#if (RELOAD_TIMER0 > 255) 
#error Out of range 
#error internal algorithm error 
#endif 

I have already tried this code, but the compiler keeps complaining, it doesn't seem to accept "RELOAD_TIMER0 > 255" operation, is it that this compiler doesn't support this operation for directives? I don't see any reason why it can't do it, I don't see any undefined parts in this piece of code.

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

CVAVR also seems to have a problem with the above (integer expression expected on the #if), but

#define XTAL_CPU 8000000
#define RELOAD_TIMER0 ((255 - (XTAL_CPU / 64 / 1000)))

#if (RELOAD_TIMER0 > 255)
#error Out of range
#error internal algorithm error
#endif

is fine

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The preprocessor is quite capable of evaluating:

#if (((255 - (XTAL_CPU / 64 * 1e-3))) > 255)

#if (((255 - (8000000 / 64 * 0.001))) > 255)
#if (((255 - (125000 * 0.001))) > 255)
#if (((255 - (125.0))) > 255)
#if (((130.0)) > 255.0)
#if (0)

I can't see any problem at all. (providing XTAL_CPU has a numeric value)

Have you created a preprocessor output file ?

I recognise the condition from an Atmel app note. Over the years I have probably compiled most app notes with most compilers. Since most are written for IAR, they are guaranteed to compile out of the box for IAR.

David.

Edit. I am using Firefox on Win7. Does anyone know how to get English spelling ? It expects colonial spelling e.g. recognize.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Edit. I am using Firefox on Win7. Does anyone know how to get English spelling ? It expects colonial spelling e.g. recognize.

It seems you need to add a dictionary: http://support.mozilla.org/en-US... (haven't tried this myself).

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Johan, that makes me a lot happier.

Mind you, there are some strange anomalies. It accepts some very rude words, yet rejects less rude ones!

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

metal wrote:
this is not the problem indeed. I think both are found in the limits file. But the problem is that IAR refuses to compile a comparison directive.
just being pedantic but there is no question of the compiler ever having any visibility of anything to do with the preprocessor. so nothing here is ever compiled.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MartinM57 wrote:
CVAVR also seems to have a problem with the above (integer expression expected on the #if), but
#define XTAL_CPU 8000000
#define RELOAD_TIMER0 ((255 - (XTAL_CPU / 64 / 1000)))

#if (RELOAD_TIMER0 > 255)
#error Out of range
#error internal algorithm error
#endif

is fine

Thanks a lot indeed, the problem was using 1e-3 :)

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oops. My mistake. I was sure that I have come across f-p expressions in #if conditionals.

CodeVision, IAR, avr-gcc all object.

So it appears that you can only use integral expressions. The preprocessor will use 32-bit integers, so you seldom have intermediate overflows.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

no problem, we learn a new thing everyday, no matter how experienced we are, thanks for your support :- )

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..