Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

Silly question, I'm sure for ATmega8 WinAVR

Silly question, I'm sure for ATmega8 WinAVR

2008-01-25 by englsprogeny

I understand bitwise operations but I don't get the following code.

DDRB|= (7<<3)

I understand that this is to setup the direction for port B  BUT why
are we doing [what appears to be] 7 shifted to the left 3 times
instead of 56 decimal

Is this the same?

DDRB|= (56)

OR are we shifting just the 7th bit left three times?  I saw in the
ATmega8 datasheet that the DDR registers are 8 bit.  Can you shift bit
7 left 3 times?


Heres's another one:

PORTB=PORTB|(1<<DDB5)

Why wouldn't we just say?

PORTB=PORTB|(DDB5)

or

PORTB=PORTB|(32)

I am new to AVR and WinAVR.

Where can I learn this information?   [books /internet/ datasheeets]

Thank you for your inputs.

David

Re: Silly question, I'm sure for ATmega8 WinAVR

2008-01-25 by John

Now, the (7 << 3) for DDRB is just silly.  

Generally, the Atmel convention is to define the bits of a register by
their offset into the register.  Therefore, DDB5 would be
Data-Direction-portB-bit5.  Therefor to set DDB5 as an output, DDRB |=
(1 << DDB5) will set the bit associated with Port B.5.

If DDB5 were defines as 0x20, then you could DDRB |= DDB5.  The one
you choose is entirely up to you, Atmel convention seems to be to
define the bit number, not the mask value.

NOTE in all these instances, DDB5 is defined as a constant (equate
actually,) and therefor the compiler will translate the code to the
optimal DDRB |= 0x20.

For Port Direction and Port I/O registers, this may seem a bit silly.
 For other registers, like SCSR for instance, this does actually help
make the code more readable.  

Take for instance:
    if (SCSR & (1 << TDRE)) ...
is much easier to decipher than
    if (SCSR & 0x20) ...
in the first case, you get a general feel for what you are looking
fore without having to consult the datasheet.

John C


--- In AVR-Chat@yahoogroups.com, "englsprogeny" <englsprogeny@...> wrote:
Show quoted textHide quoted text
>
> I understand bitwise operations but I don't get the following code.
> 
> DDRB|= (7<<3)
> 
> I understand that this is to setup the direction for port B  BUT why
> are we doing [what appears to be] 7 shifted to the left 3 times
> instead of 56 decimal
> 
> Is this the same?
> 
> DDRB|= (56)
> 
> OR are we shifting just the 7th bit left three times?  I saw in the
> ATmega8 datasheet that the DDR registers are 8 bit.  Can you shift bit
> 7 left 3 times?
> 
> 
> Heres's another one:
> 
> PORTB=PORTB|(1<<DDB5)
> 
> Why wouldn't we just say?
> 
> PORTB=PORTB|(DDB5)
> 
> or
> 
> PORTB=PORTB|(32)
> 
> I am new to AVR and WinAVR.
> 
> Where can I learn this information?   [books /internet/ datasheeets]
> 
> Thank you for your inputs.
> 
> David
>

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.