#Define and multiple assignment statements

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

Can I assign multiple assignment statements a name? For example if currently want to turn on and off PWM, so I use:

For starting PWM

#define Set_COM1A1_Bits TCCR1A |= (1<<COM1A1)
#define Start_PWM1 TCCR1B |= (1<<CS11)

For stopping PWM

#define Clr_COM1A1_Bits TCCR1A &= ~(1<<COM1A1)
#define Stop_PWM1 TCCR1B &= ~(1<<CS11)

Could I instead use something to the order of:

#define Start_PWM1 ((TCCR1B |= (1<<CS11)), (TCCR1A |= (1<<COM1A1)))

I couldn't seem to find anything on #Define and multiple assignment statements, so I'm not sure if the syntax is correct of it that is even aloud.

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

Having just answered your other thread can I implore that you think again about the over-use of macros - it's a maintainer's nightmare.

If you really want multiple C statments in a macro remember that it's quite valid to include entire blocks of instructions and split lines with the '\' character:

#define Do_everything_to_start_PWM1 { \
  TCCR1B |= (1<<CS11); \
  TCCR1A |= (1<<COM1A1); \
}

but in what sense do you think this is actually "better" than:

static inline void Do_everything_to_start_PWM1 (void) { 
  TCCR1B |= (1<<CS11); 
  TCCR1A |= (1<<COM1A1); 
}

???

EDIT BTW if you are going for the macro route then for semi-colon protection at point of invocation actually use:

#define Do_everything_to_start_PWM1 \
do { \
  TCCR1B |= (1<<CS11); \
  TCCR1A |= (1<<COM1A1); \
} while(0)

Last Edited: Wed. Jun 2, 2010 - 04:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes. Of course you can. Either use the comma operator or enclose the statements in a block with braces.

#define SETBAUD(baud)   {int div = (SYSCLK/16)/baud; \
                        U0LCR = 0x83; U0DLM = div>>8; \
                        U0DLL=div&0xFF; U0LCR = 0x03; }

I would avoid using the comma operator. It can give you grief.

David.

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

NB regarding your other thread about simulator 2 note how David has had the good sense to at least call his macro "SETBAUD" rather than "SetBaud", "Setbaud", "stebaud" or whatever. At least this will give the code maintainer an idea that it's probably a macro.

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

Quote:
I couldn't seem to find anything on #Define and multiple assignment statements,
#define is simply text substitution. If what you have in the #define results in legal C code after the substitution is made by the preprocessor, then you can do it. But as others have said, just because you can do it, doesn't mean that you should.

Regards,
Steve A.

The Board helps those that help themselves.

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

Thanks for the information, I still have a great deal to learn about programming. I am an E.E.T. (Electronic Engineering Technology) student at Akron University, so my primary background is hardware. But I'm getting my hands on as much software as possible, it seems the current trend is moving more towards engineers who work well in both worlds or at least have a solid understanding of their counterpart areas. They don't do much with software at school, but I work with micro controllers at my student engineering job, so this learning experience is most valuable. Thank you all for you help.

I took Clawson's advice and put the all the PWM control operations into an inline function. Before this I had only heard of inline functions and never used them, so it had never occurred to me this would be a perfect applications for one. Thanks again every one.