Another bit setting question (should be short)

I've never quite internalized setting bits, so after some time away..... I see an example and don't have the confidence in my understanding to just say its wrong so:

I don't recall where I copied this snippet from but looking a it, it seems wrong.

would this not set ADEN and ADPS2 to 1 and all others to 0?

  ADCSRA = 
            (1 << ADEN)  |     // Enable ADC 
            (1 << ADPS2) |     // set prescaler to 16, bit 2 
            (0 << ADPS1) |     // set prescaler to 16, bit 1
            (0 << ADPS0);      // set prescaler to 16, bit 0

making

            (0 << ADPS1) |     // set prescaler to 16, bit 1
            (0 << ADPS0);      // set prescaler to 16, bit 0

meaningless.

I believe the correct way is ....
To set ADEN & ADPS2 to 1, ADPS1 & ASPS0 to 0 one should code:

  ADCSRA = ASCSRA |
            (1 << ADEN)  |     // Enable ADC 
            (1 << ADPS2) |     // set prescaler to 16, bit 2 
            (0 << ADPS1) |     // set prescaler to 16, bit 1 
            (0 << ADPS0);      // set prescaler to 16, bit 0

Is my understanding correct?

Thanks

You are correct, in theory :slight_smile:

But I think the guy who wrote this, just wanted to make it visually clear that all 3 bits of ADPS had the desired values.

This however : ADCSRA = ASCSRA |, may not be what he wanted to do. Maybe he really wanted to clear the value of ADCSRA.

Thanks,

As I was thinking more, "OR" ing in a 0 makes no sense.

Note: these two are not equivalent...

Its my understanding:

ADCSRA = 0b00001111
Will set the leftmost 4 bits to zero

ADCSRA = ADCSRA | 0b00001111
Will leave the leftmost 4 bits at their current value.

I've seen a number of examples where a register is set by the first example above. And is successful because many / most / all? registers default to all zero's on reboot. But still wrong IMHO

1 Like

You got it.

is bugging me.

Googling

      ADCSRA avr

finds

ADCSRA Register

Googling

      ASCSRA avr

finds nothing like that, so is that a typo? Or is my google broken?

Part of the |= syntax and its friends

ADCSRA |= 0b00001111;

is that it is one less place to wonder if terms in an expression are the same, or should be, or if they are indeed different. And one less place to make a typo, I suppose.

a7

Is an ADC register in the ATTiny84. I guess google is not the all powerful Oz. We should look behind the curtain.

1 Like

Which is not mentioned does not turn up when searching the datasheet. Whereas ADCSRA does.

a7

I didn't notice, but I suppose it was meant to be ADCSRA = ADCSRA | and the S is just JohnRob who made a typo while writing the first post

1 Like

Paris in the the spring.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.