Generally curious - Logic Operation 'feature' in constants

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

Hello!

I just had a project in which I wanted the ADC clock division to be calculated by the preprocessor rather than the user/me.

So I did what I normally do in assembler in AVR GCC which gave me weird errors, so I reverted to this older trick, which gave the same errors:

#define    ADC_CLOCK_PRESCALER    4;
/*
0 = div 2 (100kHz to 400kHz I/O clock)
1 = div 2 (100kHz to 400kHz I/O clock)
2 = div 4 (200kHz to 800kHz I/O clock)
3 = div 8 (400kHz to 1600kHz I/O clock)
4 = div 16 (800kHz to 3200kHz I/O clock)
5 = div 32 (1600kHz to 6400kHz I/O clock)
6 = div 64 (3200kHz to 12800kHz I/O clock)
7 = div 128 (6400kHz to 25600kHz I/O clock)
*/

int main(void)
{
    // some code
    ADCSRA = (0b11001000 | (ADC_CLOCK_PRESCALER & 0b00000111));
    // more code
}

This throws the error "Expected ')' before ';'." on that line.
I can take the logic operation out of the code and put it into a definition: Same error.
I can do:

 ADCSRA = (0b11001000 | ADC_CLOCK_PRESCALER);

And it compiles and runs without problems, independent of whether the logic operation is in code or in a constant definition (which is logical, as the preprocessor usually merely replaces constants by their definition).

But no matter how I want to add the & operation to it, it will keep throwing that error, even if I do:

#define   ADC_CLOCK_PRESCALER    4;
#define   ADC_PRESC_MSKD   (ADC_CLOCK_PRESCALER & 0b00000111);
#define   ADCSRA_CONST_DEF    (0b11001000 | ADC_PRESC_MSKD);

Any time the & is used in conjunction with the | no matter how many parantheses I add or remove from the formula it throws the error, while the same method works fine in avr assembler.

Anyone out there have a clue why this is? Just curious really, not a real problem.

Robert!

Embedded design is as much a life choice as any other and I demand the right to legally marry my work.
-------
If it helps, I can PM you my answer in a number of different languages. Ask if you have trouble reading my high-speed-babble in English.

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

Classic error - you have put a semi-colon on the end of the #define - only about 1 time in 1000 do you actually want to do that!

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

Right, I'm just not sure if that's actually also in the code, but it is very possible (didn't copy paste, just at work waiting for the traffic jams to dissipate and thought I'd post the question.)

You're probably right though. I'll shut up now.

Let's just all pretend I posted a bit of code for people to use when flexibly using the chipclock while also doing ADconversion.

K?

:-)

Embedded design is as much a life choice as any other and I demand the right to legally marry my work.
-------
If it helps, I can PM you my answer in a number of different languages. Ask if you have trouble reading my high-speed-babble in English.