Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Can a parameter be defined in #if defined(********) and nowhere else in the code?

Status
Not open for further replies.

masoud23

New Member
Hi
I am an electronic engineer with little experiment of C programming but not programming micros. I am trying to understand the code in a micro in a hardware I am working with. This is one particle think I can not understand see the code below

Code:
public void wdog_mng_startup(void)
{
#if defined(WDOG_CLK_OCO)
	/* select on-chip osc. frequency */
	prc0 = 1;rocr = (WDOG_CLK_OCO_F2|WDOG_CLK_DIV_4);prc0 = 0;
	/* unprotect registers */
	prc1 = 1;
	/* watch-dog time-out creates reset */
	pm12 = 1;
	/* wdog clock is on-chip osc. */
	pm22 = 1;
	/* protect registers */
	prc1 = 0;
	/* start wdog counting */
	wdts = 0;
#elif defined(WDOG_CLK_CPU)
	/* unprotect registers */
	prc1 = 1;
	/* watch-dog time-out creates reset */
	pm12 = 1;
	/* protect registers */
	prc1 = 0;
  /* prescaler by 128 */
  wdc7 = 1;
	/* start wdog counting */
	wdts = 0;
#endif
}

“WDOG_CLK_OCO” is not defined any where in the code! “WDOG_CLK_CPU” is defined but no value is assign to it! How does the program know if the condition is true or false? Even if “WDOG_CLK_OCO” was defined I don’t really understand how this function works? There are in several other plases in the code there the name in
#if defined(********) are not define any where in the code. Is this an error or it can be this why? How?

Many tanks in advance
 
The directive #if defined simply checks if the name is in the label table, I.E. if it appears in the code somewhere or, more importantly, if the compiler defines it. In the case of pic chips the chip used gets defined by the compiler. You can also #undefined as well.

HTH

Mike.
 
In order for "#if defined(WDOG_CLK_OCO)" to resolve as true, earlier in the program, usually in a header there needs to be a "#define WDOG_CLK_OCO" definition. There's no value to the definition, but the if statement will resolve true and run the code. These defines are used for changing the program at compile time, usually for multiple types of hardware.

Usually these defines will always be left in the code, just commented/uncommented in order to change the code at compile time. Like this:
//#define WDOG_CLK_OCO //this define is commented out, so it will fall through to the else if
#define WDOG_CLK_CPU //this definition is not commented out, so it compile the else if code.

EDIT: Reading your text again, I think you might not understand that these #ifs are preprocessor commands and only evaluated at compile time. If neither of these definitions are in the definition table that code will not be compiled at all and it will be like it doesn't exist.
 
Last edited:
Tank you for your answer. Now I understand how this works.

Ps: I haven’t used this forum much. Is there a why for me to mark that I am satisfied with your answer?
 
On the bottom of the post, beside 'Promote to Article' there's a star image. If you click on that, you can add reputation or take it away.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top