Re: "PORTING C" > How to assign a value to a define statement?
Robby <Robby@discussions.microsoft.com> wrote:
I am running into a situation where I need some advice on how to do
things. Perhaps I am seeing this the wrong way. In summary, from
main(), I would like to call a defined macro called
"Config_delay_timers", and in that macro I would like to do some
calculations where the result can be assigned to the value of another
#define statement.
This makes no sense. Macros a just text substitutions, performed at =
compile time. If you want to calculate a value at run time, you need a =
variable for it.
Please view the following code:
=
==========================
=main.h
#ifndef TEST_H
#define TEST_H
// *** SYSTEM OSCILLATION *** //
#define Config_delay_timers(extCrys, fpllidiv, fpllmul, fpllodiv) \
FREQ = =
(((float)(extCrys/fpllidiv)*(float)(fpllmul/fpllodiv))*1000000)
#define CURR_SYS_OSC FREQ
#define OSC_PERIOD (1.0/CURR_SYS_OSC)
#endif // TEST_H //
=
==========================
============main.c
#include <stdio.h>
#include "test.h"
int main()
{
Config_delay_timers(8, 2, 21, 8);
//SYSTEMConfig(CURR_SYS_OSC, SYS_CFG_ALL);
return 0;
}
Well, since you are hardcoding all the constants anyway, why not =
something like this:
#define extCrys 8
#define fpllidiv 2
#define fpllmul 21
#define fpllodiv 8
#define FREQ =
(((float)(extCrys/fpllidiv)*(float)(fpllmul/fpllodiv))*1000000)
If, on the other hand, you expect these values to change from run to =
run, then you need real functions to calculate them and real variables =
to store them.
So how is one supposed to carry out a particular
calculation via a define macro and then further assign this result to
other define variables in the program.
One is not supposed to.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not =
necessarily a good idea. It is hard to be sure where they are going to =
land, and it could be dangerous sitting under them as they fly overhead. =
-- RFC 1925