# Binary Coded Outputs

Hello all,

I am trying to configure a device through digital IO. The device has 5 inputs that I want to configure.

What I would do, is set each of the outputs high or low and this is translated to a binary code.
E.g. see picture.,

This will be translated as 10000 which is equal to 2.

But this does not seem like lean and fine programming. Is there a different way of changing inputs outputs to compensate for of the 32 combinations?

Best Regards

tsakitsan:
This will be translated as 10000 which is equal to 2.

how is 0b10000 equal to 2!?

tsakitsan:
But this does not seem like lean and fine programming. Is there a different way of changing inputs outputs to compensate for of the 32 combinations?

to do 32 combinations, if you need all 5 outputs to change SIMULTANEOUSLY then your best bet is “direct port manipulation” else you could implement something like this in your code:-

``````for(uint8_t cnt=0; cnt<32;++i){
digitalWrite(2,cnt&0x01);
digitalWrite(3,(cnt>>1)&0x01);
digitalWrite(4,(cnt>>2)&0x01);
digitalWrite(5,(cnt>>3)&0x01);
digitalWrite(6,(cnt>>4)&0x01);

delay(1000);//only here so that u can the see the output changing

}
``````

Also pay attention to your braces : the last one is one too many...
And the lines
`setup`and`loop`i.e. lines 4 and 15, don't mean anything and should be removed.

how is 0b10000 equal to 2!?

Sorry the opposite way 00001 equal 2 as 2^1 is 2.

00001 equals 1

In the DIO logic the same is DI1=HIGH DI2=LOW DI3=LOW DI4-LOW DI5=LOW which equals 2.

how is 0b10000 equal to 2!?

to do 32 combinations, if you need all 5 outputs to change SIMULTANEOUSLY then your best bet is “direct port manipulation” else you could implement something like this in your code:-

``````for(uint8_t cnt=0; cnt<32;++i){
``````

digitalWrite(2,cnt&0x01);
digitalWrite(3,(cnt>>1)&0x01);
digitalWrite(4,(cnt>>2)&0x01);
digitalWrite(5,(cnt>>3)&0x01);
digitalWrite(6,(cnt>>4)&0x01);

delay(1000);//only here so that u can the see the output changing

}

I will initially try direct port manipulation.

Is your code example equal to 00000? Can you write it being equal to 00001 so I fully understand how to use it?

Thank for the help guys, much appreciate it.

Unless it changed recently
00001 is equivalent to 1 (one)
00010 is 2 (two)

tsakitsan:
Is your code example equal to 00000? Can you write it being equal to 00001 so I fully understand how to use it?

Thank for the help guys, much appreciate it.

I hope you are familiar with what is a "for" loop. in my example 'cnt' increments from 0 (0b00000000) to 31 (0b00011111).

using the bit shift operator (>>), bits 0 to 4 of 'cnt' are output to DIO 2 to 6 receptively before each increment of 'cnt'.

I hope my explanation makes it clearer to you

lesept:
Unless it changed recently
00001 is equivalent to 1 (one)
00010 is 2 (two)

Yes it is! My bad! I will edit it!

I hope you are familiar with what is a "for" loop. in my example 'cnt' increments from 0 (0b00000000) to 31 (0b00011111).

using the bit shift operator (>>), bits 0 to 4 of 'cnt' are output to DIO 2 to 6 receptively before each increment of 'cnt'.

I hope my explanation makes it clearer to you

Thanks for the explanation I am not familiar but I take in information quickly. Cheers

If you want all five output pins to change at the same time, you must use direct port manipulation.

Access to the AVR port registers is fully supported in the Arduino environment which simplifies the program at the expense of readability. Just study the online tutorials for direct port manipulation, it’s quite simple once you understand the names and rules.

Here is you demo program, complete, using direct port access. It was tested on an Uno.

``````void setup() {
// set pins 2,3,4,5, and 6 as outputs
DDRD = DDRD | B01111100;
}

void loop() {
for ( byte cnt = 0; cnt < 32; ++cnt) {
// write binary value 1 to 31 to pins 2,3,4,5, and 6
// while preserving the contents of bits 7, 1 and 0.
PORTD = (PORTD & B10000011) | (cnt << 2);
delay(1000);
}
}
``````

WattsThat:
If you want all five output pins to change at the same time, you must use direct port manipulation.

Access to the AVR port registers is fully supported in the Arduino environment which simplifies the program at the expense of readability. Just study the online tutorials for direct port manipulation, it’s quite simple once you understand the names and rules.

Here is you demo program, complete, using direct port access. It was tested on an Uno.

``````void setup() {
``````

// set pins 2,3,4,5, and 6 as outputs
DDRD = DDRD | B01111100;
}

void loop() {
for ( byte cnt = 0; cnt < 32; ++cnt) {
// write binary value 1 to 31 to pins 2,3,4,5, and 6
// while preserving the contents of bits 7, 1 and 0.
PORTD = (PORTD & B10000011) | (cnt << 2);
delay(1000);
}
}

Thanks for the code mate, I will have a look and customize it to do quicker the exact manipulations I want.

Best Regards!