I want to send register commands through an I2C connection for a sensor. The sensor's registers accepts decimal code, but my Arduino needs firstly to ,,create" the command binary and then send it as a decimal value, so I must program a convertor.
The problem is that the code doesn't appear to function properly at all. (I introduce the equivalent of 25 and it gives me 26, I put 33 and it gives me 1)
And the code is just fine.
This is the program:
void setup()
{
Serial.begin(9600);
}
void loop()
{
int unsigned bin=100110; //binary code + i.e
byte output; //decimal converted number
for(byte a; bin>0; a++)
{
if(bin%10==1)
{
output+=(1<<a);
}
bin/=10;
}
Serial.println(output); //printing the data through serial
}
The binary was just an example. I wrote that in the comment section.
I'll give you just an example:
In the datasheet there is a register called 0x32, and that register can hold 8 bits.
The 1st and the 2nd bit is for Mode selecting(4 modes. 00/01/10/11).
Then the 3rd bit is for trigger.(0/1)
And then the other 4 bits are for other input.(0000...1111)
The communication is made over I2C and it is an ADXL345, as I said earlier.
The idea is that in my Arduino I select what I have to, transform all the values into a binary one, and then send them over I2C as a decimal.
If there's another option I'm glad hear it. (I don't want to use other's library)
The range is 0-255. So when I send 255, the Arduino sends it as a binary code (11111111), because this is how the sensor perceps.
I mean, this is what the datasheet is telling me. An 8-bit code which can be translated into a decimal.
Sorry for my mistake regarding the registers.
Is there a more simplistic approach? If it would be, I'll be much glad.
I need a binary-to-decimal convertor. That's all. The configuration is made trough 0's and 1's in Arduino. Then the configuration must be printed to i2c as a decimal value. If it would have existed a Serial.print(DEC) for I2C I wouldn't have bothered now doing this.
We are struggling to work out why you need one. The compiler converts all decimal values to binary anyway.
Perhaps if you posted your full code (the one you posted has no mention of any I2C or whatnot), and also explained what you are trying to do, i.e. where this weird decimal representation of binary comes from, we might be able to help.
http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf - this is the datasheet from sparkfun of ADXL345 sensor.
Go to page 17, in the top right corner, and you'll see for example register 0x31.
Look at the table: you see D7, D6, D5, D4, D3, D2, D1, D0. Every bit of this byte has a function in this register. (enable and disable).
For example if I want to enable the 3-pin SPI at position D6 I have to send 01000000 code. (in case the other bits are 0's). The equivalent in decimal is 64. But if I want to enable both the SPI and Justify bit I have to send 01000100 which in decimal equals 68.
The problem is that I cannot send the values directly over I2C binary, because the Wire doesn't let me do it. I just can send one value at a time. And the solution is to convert the 8-bit code into a decimal and then print it over I2C. That's why I need a Binary-Decimal convertor. Mine looks ok, but I don't understand why it isn't working.
And...tell me if I say something wrong.
Thank you,
RobertEagle
Take a look at the syntax and parameters. They say "a value to send as a single byte".
If I would print 10111011, the Wire would translate it in a binary code, because he is considering the given value a decimal.
And yes, like 64 or 68.