0

Im looking for a way to modify a binary byte value on Arduino.

Because of the Hardware, its neccesarry, to split a two digit number into 2 4-bit.

the code to set output is wire.write(byte, 0xFF) which sets all outputs on High.

0xFF = binary 1111 1111

the formula should be convert a value like this:

e.g nr 35 is binary 0010 0011

but for my use it should displayed as 0011 0101 which would be refer to 53 in reality.

The first 4 bits are for a BCD-Input IC which displays the 5 from 35, the second 4 bits are for a BCD-Input IC which displays the 3 from 35.

Does anybody has a idea how to convert this by code, or like a mathematical formula? Possible numbers are from 00 to 59.

Thank you for your help

1
  • So you want to split the numbers up, convert them and 'glue' them back together again... Commented Aug 3, 2014 at 15:53

2 Answers 2

1

To convert a value n between 0 and 99 to BCD:

((n / 10) * 16) + (n % 10) 

assuming n is an integer and thus / is doing integer division; also assumes this will be stored in an unsigned byte.

(If this is not producing the desired result, please either explain how it is incorrect for the example given, or provide a different example for which it is incorrect.)

Sign up to request clarification or add additional context in comments.

3 Comments

i dont want to convert to bcd! i have to adjust the byte! My port expander has 8 Outputs, and you only can adress the whole byte! its about assemble 2 4-bit bytes to an single 8 one.
Do you mean you want the first byte to be (n/10) and the second one to be (n%10) or do you want them both in the same byte? If you want them both in the same byte then it is the BCD conversion the @Scott Hunter has given.
cup the byte is 0000 0000, the first 4 0 should contain digit 1, and the second 4 0 should contain digit 2
0
#include <string.h> int num = // Any number from 0 to 59 int tens = num/10; int units = num-(tens*10); // Make string array for binary string tensbinary; int quotient = tens; char buffer[1]; // Convert numbers for (int i = 0; i < 4; i++) { quotientint = quotientint % 2; sprintf(buffer, 1, "%d", quotientint); binary.append(buffer); } // Repeat above for the units // Now join the two together binarytens.append(binaryunits); 

I don't know if this will work, but still, you might be able to extrapolate based on the available information in my code.

The last thing you need to do is convert the string to binary.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.