NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Bitwise Operations( Binary representation of a floating pin)

September 09, 2011
by dgikuljot
dgikuljot's Avatar

Hey guys, So lately i have been taking time to return to learning c programming for mcu's. I have finally understood how to read binary numbers and all that. And i understand the fact that lets say if i wanted to pb4 as an output that the binary number for this would be 00000100. And that I have to do left/ right shifts to turn pins high and low, but where my confusion lies is that if lets say on portb pb4 is not high or low(floating), how is that represented in Binary. When i am doing lets say a left shift but was the original value of that register in binary, so i can know how much to shift by?

I am sorry if the qustion is confusing but after this clarification microcontroller programming will start making sense to me . Essentially all i am asking is what is the binary represenatation of a floating pin. Thanks, Kuljot Dhami

September 09, 2011
by dgikuljot
dgikuljot's Avatar

Also guys i was wondering why all the c tutorials and books for avr use hexadecimal instead of binary. I just learned Binary and dont know anything about Hexadecimal. Is there any advantage of using hexadecimal over binary, is it the industry standard? Thanks, Kuljot Dhami

September 10, 2011
by Rick_S
Rick_S's Avatar

Actually to make Port B4 an output with all other Port B pins inputs, you would have to do:

DDRB = 0b00001000;

If you only want to make PB4 an output without effecting the other PortB pins, you would do:

DDRB |= (1<<PB4);

or

DDRB |= (1<<4);

or

DDRB |= 8;

These statements all translate to the same thing.

If you look at your copy of the chips datasheet, you will find that the register controlling the direction of the ports (DataDirectionRegister) is 8 bits wide. Each bit is for controlling the direction of the data on a given pin.

There is also an 8 bit PORT register for each of the ports. This register controlls the pullup/tristate when in input mode or causes the pin to go high or low in output mode.

Lastly there is an 8 bit PIN register for reading the status of a pin when in input mode.

My suggestion to you would be to thoroughly read through the guide several times (especially the sections related to i/0). Look through the sample programs in the code download and really try to understand what is being done. Search the forum and library here on the website. There is a wealth of information here including what I just provided. Download the code for some of the tutorial items and try to understand what is being done there. It will come to you.

As for binary vs: Hex, it's a matter of preference. Hex is definately shorter to type than binary. Just remember to preface it with the correct prefix 0b for binary or 0x for Hex.

Rick

September 10, 2011
by bretm
bretm's Avatar

Hex is handy because each hex digit corresponds to exactly four bits, so it's easy to convert back and forth, and it's easier to read because the long strings of 1's or 0's can get hard to count.

1<<4 is 16, not 8, which illustrates exactly why we use the shift operator instead of a value.

September 10, 2011
by Ralphxyz
Ralphxyz's Avatar

Rick that has to be the best, most concise explanation I have seen.

I am still working on "DDRB |= 8;" but for now I'll take your word for it.

dgikuljot, as Rick said [quote] This register controlls the pullup/tristate when in input mode or causes the pin to go high or low in output mode. [/quote]

As far as I understand the only time the tristate mode applies is in input mode so there is no binary representation needed.

You will not have a tristate mode when the pin is configured as output.

Concerning [quote] so i can know how much to shift by? [/quote] you only/always left shift by the pin, 0 based, bit count for a individual pin.

Please someone correct me if I am wrong.

Ralph

September 10, 2011
by dgikuljot
dgikuljot's Avatar

Thanks guys, For all the explanations. I will definetly need to practise more to grasp this concept clearly, but atleast i actually understand binary now which i didnt before.

September 10, 2011
by Rick_S
Rick_S's Avatar

Whoops, yep bretm, I did goof. 1<<4 is 16 not 8 and is also 0b00010000 not 0b00001000. Thanks for the catch hopefully I didn't add to dgikuljot's confusion BigGrin_Green

September 10, 2011
by Ralphxyz
Ralphxyz's Avatar

Wow, I was wondering about "DDRB |= 8;" thanks bretm, so that should have been DDRB |= 0b00010000;

Now that makes more sense.

Ralph

September 10, 2011
by bretm
bretm's Avatar
Shift       After          Binary      Hexadecimal  Decimal
Expression  Preprocessing  Equivalent  Equivalent   Equivalent

1 << PB0    1 << 0         0b00000001     0x01          1
1 << PB1    1 << 1         0b00000010     0x02          2
1 << PB2    1 << 2         0b00000100     0x04          4
1 << PB3    1 << 3         0b00001000     0x08          8
1 << PB4    1 << 4         0b00010000     0x10         16
1 << PB5    1 << 5         0b00100000     0x20         32
1 << PB6    1 << 6         0b01000000     0x40         64
1 << PB7    1 << 7         0b10000000     0x80        128
September 11, 2011
by Ralphxyz
Ralphxyz's Avatar

Thanks bretm, that table should definitely go in the Library. It is such a good reference.

Speaking of the Library I just saw your "Atmega 168-328 Pin Definitions".

WOW!! that is a work of art, thank you so much.

Ralph

September 11, 2011
by bretm
bretm's Avatar

Thanks!

September 11, 2011
by dgikuljot
dgikuljot's Avatar

@ Bretm, Rick, and Ralph, Thanks for all the information. I will definently play around with all this information and see what i can learn from it when i have free time from all my ap classes and work. Thanks, DGI Kuljot

Post a Reply

Please log in to post a reply.

Did you know that SPDT stands for "Single Pole, Double Throw"? Learn more...