I have been trying with different data types and nothing is working.
I am working with I2C communication in Arduino mega to Arduino uno. And I tried sending sample data and it was successful. The main agenda is sending 64 bits via I2C. I want to send in HEX and receive in HEX and i want to process as BINARY. Can anyone help me out in this ?
I have been trying with different data types and nothing is working.
How about posting what you have tried and describing what does not work as you want ?
I want to send in HEX and receive in HEX and i want to process as BINARY
HEX representation of numbers is only a way of making the value easier to read for humans. All values in the program are inherently binary behind the scenes. Unless, of course, you want to send and receive a series of characters representing the HEX value
The decimal number 1110 can be written as C16 in hexadecimal or 10111 in binary.
When you say that you want to send and receive in "HEX" I suspect you mean that you want to send and receive printing ASCII characters which represent the hexadecimal equivalent of a number.
The ASCII character ‘C’ actually has a numeric value which is 64 decimal , 43 hexadecimal and 0100 0011 binary.
You probably want to transmit the ASCII characters ‘0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’, ’A’, ’B’, ’C’, ’D’, ’E’, ’F’ to represent hexadecimal digits.
When you receive a character you need to decide which ASCII character it is then use the actual hex number it represents to calculate your overall number.
I had a version of Forth many years ago where you could set what number base it worked in. Whenever you asked it to print out the current number base it always printed 10, no matter what you had previously set it to.
I used to ask students to explain why on the exam paper.
I am using this code to convert hex to binary. In this code I have initialized a sample character array with 4 characters, but in real scenario, the code would have more than 20 characters.
So i have used a separate function to convert the HEX to binary. Another function to separate the digits. and the code returns garbage value. can anyone help me in this ?
here is the code:
char received_data[4] = {'2','A','3','8'};
int received_bits[4]= {0,0,0,0};
void setup() {
Serial.begin(9600);
}
void loop()
{
unsigned long first_byte_upper = convert_binary(received_data[1]);
int *first_bits_upper = convert_digits(first_byte_upper);
for(int x =0; x <= 3; x++)
{
received_bits[x] = *(first_bits_upper);
Serial.print(received_bits[x]);
first_bits_upper++;
} //Serial.println("");
unsigned long convert_binary(char a)
{
unsigned long converted;
if (a == '2')
{
converted = 0010;
return converted;
}
if (a == 'A')
{
converted = 1010;
return converted;
}
if (a == '3')
{
converted = 0011;
return converted;
}
if (a == '8')
{
converted = 1000;
return converted;
}
}