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.

Support Forum » Binary output

February 09, 2010
by DrNoExpert
DrNoExpert's Avatar

I found a chip which can increase output pins for an led display by decoding binary input. for example, input a=1, b=0, c=1, then output pin 5 is on. How can I program pins to output binary in sync?

February 09, 2010
by N3Roaster
N3Roaster's Avatar

If all the pins you want to use are on the same port (that is, controlled by the same register), just set all the bits as you want them at the same time. To use your example, if you wire it up such that PB0 on the MCU goes to c, PB1 goes to b, and PB2 goes to a (or flipped around if I guessed wrong on which side is the most significant bit, hard to tell with 101 as the example), you could set those pins as outputs and write

PORTB = 5;

If there are other pins in the port that are being used for other purposes you'll want to use a bitwise operations to make sure that you're only changing the pins that you're using for this purpose. To stick with this example, if we only want to change the rightmost three bits while leaving all other bits the same, that can be done with something like:

PORTB = (PORTB & 248) | 5;

Now, that's a cryptic line. It should be commented. In binary, 248 is represented as 11111000. The 5 is your 101 and would change depending on what you wanted in those last three bits. The

PORTB & 248

portion keeps the left 5 bits as they were while clearing the right 3 bits. The

| 5

portion then sets those last three bits to 101. The assignment operator then writes the new value to PORTB and the three pins change to reflect the new value all at once.

Post a Reply

Please log in to post a reply.

Did you know that you can read diagnostic data from some cars with a NerdKit? Learn more...