Solved

C Preprocessor Macro not working as expected

Posted on 2008-10-23
4
307 Views
Last Modified: 2011-09-20
Hi There,

In my code below, i'm trying to write a few macros that will automatically calculate a timer value for using in a PIC microcontroller project. Basically, you just set the oscillator frequency, and the macro should calculate the value to load into the timer for a 10us delay. I have defined 6 macros for this: FOSC, FCY, F_10us, DELAY_10us_0, ROUND_FIX_10us and DELAY_10us. The value for DELAY_10us_0 is rounded down, so I calculate whether the real divide would be closer to a round-up, and if so, add 1 to the delay value. This gives the nearest approximation to 10us delay.

This all works fine, and as expected, except for the last macro.  When I add DELAY_10us_0 to ROUND_FIX_10us, the answer is always just ROUND_FIX_10us (either 0 or 1). No addition takes place as far as I can see. I believe I am probably missing something in how the preprocessor is actually calculating these values, so any clarification would be very helpful.

Thanks for any help
Rob Smith
/*****************************/
/* define the clock settings */
/*****************************/
#define FOSC	1600000L		// Oscillator Frequency
#define FCY	FOSC/4		// Instruction Clock Frequency
 
 
/**********************/
/* Delay calculations */
/**********************/
// 10us
#define F_10us		100000L		// For 10us, freq will be 100KHz
#define DELAY_10us_0	FCY/F_10us	// The number of instructions that go into 10us (Rounded-Down)
#define ROUND_FIX_10us	(FCY - (DELAY_10us_0 * F_10us)) > (F_10us/2) ? 1:0
#define DELAY_10us 	DELAY_10us_0 + ROUND_FIX_10us

Open in new window

0
Comment
Question by:salukibob
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22786783
First of all, you need to add a lot more ()'s. Macro's are expanded in-line, and that might change the meaning due to operator precedence.
/*****************************/
/* define the clock settings */
/*****************************/
#define FOSC    1600000L                // Oscillator Frequency
#define FCY     (FOSC / 4)          // Instruction Clock Frequency
 
 
/**********************/
/* Delay calculations */
/**********************/
// 10us
#define F_10us          100000L         // For 10us, freq will be 100KHz
#define DELAY_10us_0    (FCY / F_10us)      // The number of instructions that go into 10us (Rounded-Down)
#define ROUND_FIX_10us  (((FCY - (DELAY_10us_0 * F_10us)) > (F_10us / 2)) ? 1 : 0)
#define DELAY_10us      (DELAY_10us_0 + ROUND_FIX_10us)

Open in new window

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 22786890
With these added ()'s, your DELAY_10us is expanded like this :

        (((1600000L / 4) / 100000L) + ((((1600000L / 4) - (((1600000L / 4) / 100000L) * 100000L)) > (100000L / 2)) ? 1 : 0))

which is equal to :

        4

instead of what your code expanded to :

        1600000L/4/100000L + (1600000L/4 - (1600000L/4/100000L * 100000L)) > (100000L/2) ? 1:0

which is equal to :

        1
0
 

Author Comment

by:salukibob
ID: 22787514
Ok, thanks very much for the advice. I was aware you had to do that when passing in variables to macros, but not when using other macros. I suppose its the same though when expanded out as you've pointed out.

Thanks very much
Rob Smith
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22788233
Yes, in this specific case, the + has higher precedence than the >, so :

        a + b > c ? d : e

is the same as :

        ((a + b) > c) ? d : e

not the intended :

        a + ((b > c) ? d : e)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert image to byte array 8 226
Certified Pentester Exam - need assistance on compiling an exploit 6 424
Coverting 24 hour time to 12 hour in C++ 15 183
outlook office 365 8 160
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question