the function utoa() is used to do the all work for you.
ex:
Thursday, 7 May 2015
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 loop(){
//do nothing
}
and the output is like

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

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
and the result is

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

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.
and the output is
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

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

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

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

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
Tuesday, 21 April 2015
Unary Minus Operator Vs Binary Minus
we know minus sign(-) which is used for subtraction(binary minus operator), But what is mean by unary minus operator?
first.. What's the meaning of unary operator?
when minus sign is used as unary operator the value of variable is negated.
and the output is

of all the arithmetic operators unary minus has a highest precedence level
first.. What's the meaning of unary operator?
a unary operation is an operation with only one operand, i.e. a single input
when minus sign is used as unary operator the value of variable is negated.
void main(){
int num = 4;
int negeted_num = -num; //unary operator
printf("Demonstration of Unary Minus Operator\n");
printf("the value of num is %d\n",num);
printf("the value of negeted number \n");
printf("negeted_num = - num --> %d",negeted_num);
getch();
}
and the output is

of all the arithmetic operators unary minus has a highest precedence level
Integer Truncation Error in C
the integer truncation error is displayed using the following simple program
and the output is

which displays the result of (5/2)*2 = 4 which is wrong.
void main(){
int a = 5;
int b = a/2;
int c = b*2;
printf("integer truncation error in C\n");
printf("the value of a is %d\n",a);
printf("the value of b = a\\2 is %d\n",b);
printf("the value of c = b*2 is %d\n",c);
getch();
}
and the output is

which displays the result of (5/2)*2 = 4 which is wrong.
Points to Remember when Writing a C program
When Declaring a Variable :
Declaration must be placed after the opening braces of the main() function.all declarations must come before the first executable(non-declaration) statements of the program.
all types of variables must be declared before they are used.
When Naming a Variable :
1.Variable name must begin with a letter of alphabet.In C underscore(_) is considered as a letter
2.Compiler treats uppercase and lowercase letters as different
3.No special characters, such as blank space,period,semicolon,comma, or slash are permitted in variable names
what is a variable ?
it is a symbolic name for a memory location in which assigned value is stored
Comments:
It is easier to put in the comments as you code the program rather than after the entire program has been completed.
Variable Initialization :
It is not sufficient to declare a variable before it is used, the variable must also be initialized.
the compiler doesn't automatically assign 0 to it when it is created.
prints the garbage value.when i try to run the output is

Integer Truncation Error :
fractional parts are truncated into integers which leads to horrendous errors\
Note: printf the letter f in that stands for either function or formatted
[In Progress]
Declaration must be placed after the opening braces of the main() function.all declarations must come before the first executable(non-declaration) statements of the program.
all types of variables must be declared before they are used.
When Naming a Variable :
1.Variable name must begin with a letter of alphabet.In C underscore(_) is considered as a letter
2.Compiler treats uppercase and lowercase letters as different
3.No special characters, such as blank space,period,semicolon,comma, or slash are permitted in variable names
what is a variable ?
it is a symbolic name for a memory location in which assigned value is stored
Comments:
It is easier to put in the comments as you code the program rather than after the entire program has been completed.
Variable Initialization :
It is not sufficient to declare a variable before it is used, the variable must also be initialized.
the compiler doesn't automatically assign 0 to it when it is created.
void main(){
int a;
printf("%d",a);
getch();
}
prints the garbage value.when i try to run the output is

Integer Truncation Error :
fractional parts are truncated into integers which leads to horrendous errors\
Note: printf the letter f in that stands for either function or formatted
[In Progress]
Pointers in C
simple code to demonstrate the pointers in C.
Output

1.we have to assign a address to the pointer not a value
2."&"(ampersand) sign is used to get the address of the variable stored in the memory
3."*"(atrisk) is used to declare the pointer and to get the value from pointer
#include stdio.h
#include conio.h
void main(){
int *p_num; //define a pointer which holds the integer value
int num;
p_num = # // assign a value(address) to a pointer
printf("enter number : ");
scanf("%d",&num);
printf("the integer number assigned to variable num is: num = ");
printf("%d\n\n",num);
printf("the address integer variable num is stored: &num = ");
printf("%d\n\n",&num);
printf("the value stored in the pointer p_num is: p_num = ");
printf("%d\n\n",p_num);
printf("the address of pointer is in the memory is: &p_num = ");
printf("%d\n\n",&p_num);
printf("the value which address is stored in the pointer is:*p_num = ");
printf("%d\n\n",*p_num);
getch();//to hold the display
}//end of main
Output

1.we have to assign a address to the pointer not a value
2."&"(ampersand) sign is used to get the address of the variable stored in the memory
3."*"(atrisk) is used to declare the pointer and to get the value from pointer
char strings Vs Integers in C
simple program to demonstrate the inputting and displaying of character strings and integers in C
Result

Please note the & sign difference between char string and integer
#include stdio.h
#include conio.h
#include string.h
void main(){
char name[15];
int age;
printf("enter name : ");
scanf("%s",name); // please note there is no & sign for char string
printf("enter age : ");
scanf("%d",&age); // note the & sign for the integer
printf("%s\t%d",name,age);
getch();
}//end of main
Result

Please note the & sign difference between char string and integer
Subscribe to:
Posts (Atom)