Watch ADCSRA bit 6 (ADSC) or bit 4 (ADIF)?

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

Is there any benefit of one over the other?

while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
do_stuff()

compared to...

while ((ADCSRA & 0x40)!=0);
do_stuff()

and could that be simplified to

while (ADCSRA & 0x40);
do_stuff()

Edward

PS. Would it be better to #define my constants or declare them with const?

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

I usually use ADSC since it is one less step.

Quote:
PS. Would it be better to #define my constants or declare them with const?

You will usually want to use #define. It is easier for the compiler to optimize it that way. Also, depending on what compiler you are using, they may already be defined, though usually as bit numbers rather than bit masks. Using (1<<ADSC) will produce identical code to 0x40.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
while ((ADCSRA & 0x40)!=0); 

is better coding style than:

while (ADCSRA & 0x40);

and required for example for MISRA-C compliance. It just shows much better what the author exactly wants wants to do.
Look at:

if(test & 0x03)

This will be true for test values 0x01, 0x02 and 0x03. Is this really what the author wanted to achieve?
This becomes much clearer with

if((test & 0x03)!=0x00)  // same as above
..
if((test & 0x03)==0x03)  // true, if test==0x03
..

Jörg.

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

Quote:

and required for example for MISRA-C compliance.

If you are experienced in coding to MISRA standards, does that make you MISRA-able, or simply MISRAble?

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

JBecker wrote:

if((test & 0x03)!=0x00)  // same as above
..
if((test & 0x03)==0x03)  // true, if test==0x03
..

Jörg.


for the second expression wouldn't it evaluate as true if test == xxxxxx11 with the xs being either 1 or 0? Whereas the first would be true if either bit 0 or 1 is 1, right?

Edward

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

I am just using a very good (and expensive) compiler :-) with builtin MISRA-C compliance testing and I often use that to find weaknesses in my coding.

The (MISRA conforming) code sometimes becomes MISRAble though, because some often used constructs are not allowed, but I think these guys had their ideas about why these things should better not be done.

You really learn to think again about very basic things.

volatile unsigned int value;

if ((value>100)&&(value<200))
...

is not allowed (though often used) because 'value' might change its value between the two comparisons.
What most of us really want to do if writing the above code is better done with:

volatile unsigned int value;
unsigned int tmp;

tmp = value;
if ((tmp>100)&&(tmp<200))
...

Might make a difference if this code is for a safety-critical application and unwanted behaviour has to be avoided under all circumstances.

Jörg.
(.... feeling MISRAble about using this damned expensive I__ compiler :wink: )