Wednesday 6 May 2015

converting Decimal to HEX values in Arduino

while i am searching for a way to send the HEX values rather than Int / char values using Arduino Serial communication. i find that Serial.print() will do the work for me.

void setup(){
int a = 17;
Serial.begin(9600);
Serial.print("value of a :"); Serial.println(a);
Serial.print("HEX equalent of a :"); Serial.println(a,HEX);


}

void loop(){
//do nothing
}

and the output is like
hex equa

it is good enough to do the work for me But there is a chance of error if i wanted to send a value in the middle of array.

the problem arise when we wanted to print leading zeros for fixed two byte and 4 byte HEX values
if we wanted to print values 1 and 1000,we get hex equivalents as
leading zero problem

which is printed by omitting zero's. and doesn't specify the no of HEX digits in outputs

so serial.print(var,HEX) won't print the HEX values with Leading zeros. we need to find a way to do it.

In C there is a function called sprintf() which will be the saviour of our day.
It is used to format a string which is used to print at the output

the syntax of sprintf is
sprintf(char *str,const char* format)

But first we wanted to print hex values so we need to know the different HEX format specifiers.
we can simply know by compiling and running this simple code

#include
int main() {
int data = 29;
printf("%x\n", data); // just print data
printf("%0x\n", data); // just print data ('0' on its own has no effect)
printf("%8x\n", data); // print in 8 width and pad with blank spaces
printf("%08x\n", data); // print in 8 width and pad with 0's

return 0;

and the result is
HEX format specifier

by using both format specifiers and sprintf we can print the hex value as it is

void setup(){
Serial.begin(9600);
int a = 0x0094;
Serial.println(a);
Serial.print("serialprint using HEx : ");
Serial.println(a,HEX);
print_hex(a);
}
void loop(){

}

void print_hex(int val){
char str[20];
sprintf(str,"%04x",val);
Serial.print("serialprint using print_hex function : ");
Serial.println(str);
}//end of printhex() function


and the output is
print_hex function


But what if we wanted to convert a decimal value to HEX and sent it as HEX?
we can use the below code to do that.

/*
Author : Kunchala Anil
converting Decimal value into HEX
*/
#define max_hex_length 4

char data_buffer[20];
int hex[4];

void setup(){
Serial.begin(9600);
}//end of main()


void loop(){
int a = serial_ask();
Serial.print("the value you entered is :");
Serial.println(a);
convert_to_hex(a);

}//end of loop

void convert_to_hex(int a){
for (int i=0; i = 0; j--){
if (hex[j] <= 9){
Serial.print(hex[j]);
}
else{
switch(hex[j]){
case 10 :
Serial.print('a');
break;

case 11 :
Serial.print('b');
break;

case 12 :
Serial.print('c');
break;

case 13 :
Serial.print('d');
break;

case 14 :
Serial.print('e');
break;

case 15 :
Serial.print('f');
break;

default :
Serial.println("something wrong");
}//end of switch
}//end of If Else Condition

}//end of for loop
Serial.println();

}//end of convert_to_hex() function


int serial_ask(){
Serial.println("enter the Integer decimal value coverted to the HEX");

while(!data_received()){
//wait until full data is received
}
return(atoi(data_buffer));

}//end of serial_ask() fucntion

boolean data_received(){
while(!Serial.available()){
//wait until user enters the data
}
if(Serial.available()){
static byte index = 0;
char input = Serial.read();
if(input != '\r'){
data_buffer[index] = input;
index ++;
}
else {
data_buffer[index] = 0;//terminating with NULL to make char array as string
index = 0;
return true;

}//end of If Else condition

}//end of IF condition
return false;
}//end of data_received() function



and the output is
dec to hex

No comments:

Post a Comment