This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using enumeration constants with uVision3

The argument I have always heard for using enumerations to
define constants in C is that it allows the tool chain to
pass the symbols to one's debug environment. According to
earlier discussions I have seen on this forum, that is not
true of any of the C51 tools.

The following example uses a typedef and an enumeration to
simplify representation of some data:

#include <c8051f040.h>  /* SFR declarations */
#include <stdio.h>      /* printf function  */

typedef enum { IL,
               OH,
               WI,
               IN } STATE;
STATE mySt;

void printState(STATE outState); /* Function prototype */

void main(void) {

  SFRPAGE = 0;
  WDTCN     = 0xDE;
  WDTCN     = 0xAD;
  for(;;) {
    for(mySt=IL; mySt<=IN; mySt++)
      printState(mySt);
  }
}  /* END main() */

void printState(STATE outState) {
  char *stateName = { "Illinois",
                      "Ohio",
                      "Wisconsin",
                      "Indiana"
                    };

  printf("%s\n", stateName[outState]);
  return;
} /* END printState */
This compiles & links without error, but even in my compiler
listing, the symbolic constants have vanished:

for(mySt=IL; mySt<=IN; mySt++) | ?C0001: CLR     A
                               |         MOV     mySt,A
                               | ?C0003: MOV     A,mySt
                               |         SETB    C
                               |         SUBB    A,#03H
                               |         JNC     ?C0001
In most of my applications, the constants do appear in the
link listing, but without their values:

  IL . . . . . . . .  E_CONST  -----  U_CHAR   -----  1
  OH . . . . . . . .  E_CONST  -----  U_CHAR   -----  1
  WI . . . . . . . .  E_CONST  -----  U_CHAR   -----  1
  IN . . . . . . . .  E_CONST  -----  U_CHAR   -----  1
(The foregoing example is faked. I could not get this
example to comply for unknown reasons.)

Since I can't get the debugger to use the symbols, and I
can't get the compiler to tell me what values it assigned to
my literals, I usually wind up scribbling a hand-written
guess to help me debug. I am starting to wonder if I
wouldn't be better off returning to the more primitive
approach:

  #define IL 0
  #define OH 1
  #define WI 2
  #define IN 3
where that data will at least be available in my source.

Has anybody else wrestled with this one? Any advice?
============================================================
Gary Lynch            |     To send mail, change no$pam in
lynchg@no$pam.com     |     my domain name to stacoenergy

0