#include <avr/io.h>
#include <inttypes.h>
#include<util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/eeprom.h>
#include <math.h>
char Digits[] ="0123456789abcdef";
int a,b,j,len,i,val,power=0,test=10;
long int decimal;
char stringOfData[50];
char stringOfReadData[4];
char data;
char hexbuff[4];
char testbuff[2];
char jj[2];
void USARTInit(uint16_t ubrr_value)
{
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);
UCSRC=(1<<URSEL)|(3<<UCSZ0);
UCSRB=(1<<RXEN)|(1<<TXEN);
}
char USARTReadChar()
{
while(!(UCSRA & (1<<RXC)))
{
}
return UDR;
}
void USARTWriteChar(char data)
{
while(!(UCSRA & (1<<UDRE)))
{
}
UDR=data;
}
void USARTWriteStr(unsigned char * str)
{
while(*str)
{
UDR = *str++;
while(!(UCSRA&(1<<UDRE)));
}
}
void htod()
{
for(i=3; i<=0; i--)
{
/*search currect character in Digits array */
for(j=0; j<16; j++)
{
if(hexbuff[i] == Digits[j])
{
USARTWriteStr("\n\r");
USARTWriteStr("no=");
itoa(j,jj,10);
USARTWriteStr(jj);
}
}
power++;
}
}
void main()
{
USARTInit(25);
// USARTWriteStr("start");
while(1)
{
decimal = 0;
power = 0;
USARTWriteStr("Enter value=");
for(a=0; a<=3; a++)
{
hexbuff[a] = USARTReadChar();
USARTWriteChar(hexbuff[a]);
}
// USARTWriteStr("\n\r");
USARTWriteStr("Value=");
USARTWriteStr(hexbuff);
// htod();
for(i=3; i>=0; i--)
{
/*search currect character in Digits array */
for(j=0; j<=15; j++)
{
if(hexbuff[i] == Digits[j])
{
decimal += j*pow(16,power);
ltoa(decimal,stringOfData,10);
USARTWriteStr("D=");
USARTWriteStr(stringOfData);
USARTWriteStr("\n\r");
}
}
power++;
}
/*b= 3;
val = pow(16,b);
itoa(val,testbuff,10);
USARTWriteStr("\n\r test=");
USARTWriteStr(testbuff);
USARTWriteStr("\n\r");
itoa(decimal,stringOfData,10);
USARTWriteStr("D=");
USARTWriteStr(stringOfData);
USARTWriteStr("\n\r");*/
}
return 0;
}
#include <avr/io.h>
#include<util/delay.h>
#include <avr/io.h>
#include <math.h>
char Digits[] ="0123456789abcdef";
int a,b,j,len,i,val,power=0,test=10;
int decimal,varg,calcn;
unsigned char vargData[50];
unsigned char stringOfnData[50];
unsigned char stringOfReadData[50];
char data;
unsigned char hexbuff[4];
unsigned char testbuff[2];
char jj[2];
void USARTInit(uint16_t ubrr_value)
{
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);
UCSRC=(1<<URSEL)|(3<<UCSZ0);
UCSRB=(1<<RXEN)|(1<<TXEN);
}
char USARTReadChar()
{
while(!(UCSRA & (1<<RXC)))
{
}
return UDR;
}
void USARTWriteChar(char data)
{
while(!(UCSRA & (1<<UDRE)))
{
}
UDR=data;
}
void USARTWriteStr(unsigned char * str)
{
while(*str)
{
UDR = *str++;
while(!(UCSRA&(1<<UDRE)));
}
}
void main()
{
USARTInit(25);
// USARTWriteStr("start");
while(1)
{
decimal = 0;
varg =0;
power = 0;
USARTWriteStr("Enter value=");
for(a=0; a<=3; a++)
{
hexbuff[a] = USARTReadChar();
USARTWriteChar(hexbuff[a]);
}
USARTWriteStr("\n\r");
USARTWriteStr("Value=");
USARTWriteStr(hexbuff);
USARTWriteStr("\n\r");
for(i=3; i>=0; i--)
{
/*search currect character in Digits array */
for(j=0; j<=15; j++)
{
if(hexbuff[i] == Digits[j])
{
varg = pow(16,power);
itoa(varg,vargData,10);
USARTWriteStr("v=");
USARTWriteStr(vargData);
USARTWriteStr(" ");
calcn = j*varg;
itoa(calcn,stringOfnData,10);
USARTWriteStr("c=");
USARTWriteStr(stringOfnData);
USARTWriteStr(" ");
decimal = decimal +calcn;
itoa(decimal,stringOfReadData,10);
USARTWriteStr("D=");
USARTWriteStr(stringOfReadData);
USARTWriteStr("\n\r");
}
}
power++;
}
b= 3;
val = pow(16,b);
itoa(val,testbuff,10);
USARTWriteStr("\n\r test=");
USARTWriteStr(testbuff);
USARTWriteStr("\n\r");
}
return 0;
}
i am getting -1 error in result
#include
.
.
char Digits[] ="0123456789abcdef";
char hexbuff[4];
for(i=3; i>=0; i--)
{
/*search currect character in Digits array */
for(j=0; j<=15; j++)
{
if(hexbuff[i] == Digits[j])
{
decimal += j*pow(16,power);
ltoa(decimal,stringOfData,10);
USARTWriteStr("D=");
USARTWriteStr(stringOfData);
USARTWriteStr("\n\r");
}
}
power++;
}
.
.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int power_multiplier = 1; for(i=3; i>=0; i--) { /*search currect character in Digits array */ for(j=0; j<=15; j++) { if(hexbuff[i] == Digits[j]) { decimal += j*power_multiplier; ltoa(decimal,stringOfData,10); } } //power_multiplier *= 16; //both examples are suitable power_multiplier <<= 4; }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int alphahex_to_int(char *h) { int result = 0; int tmp; while(*h) { if ((*h & 0xf0) == 0x30) tmp = *h - '0'; else tmp = (*h & 0x0f) + 9; result = (result << 4) + tmp; h++; } return result; }
Pow is a floating point function. Besides the fact that mega doesn't have dedicated FPU (that makes compiler to create a huge amount of code for mega's integer ALU, something about 2K of memory) the floating point numbers tend to be erroneous, so when you do 16^2 you get not 256, but 255.99997 (not exactly this, but similar), after converting it to int (that just truncates everything after the decimal point, but not rounds it) you get 255, and so forth with 16^3 etc.
var = var / 100;
or
var = var / 0x64;
or
var = var / 0b01100100;
unsigned var = 151;
unsigned newvar1 = var / 100; //newvar1 will be 1
unsigned newvar2 = (var + 50) / 100; //newvar2 will be 2 (rounded up)
unsigned var2 = 149;
unsigned newvar3 = (var2 + 50)/100; //newvar3 will be 1 (not rounded up)
double a = 0.3;
double b = 3 * a + 0.1;
if(b == 1.0)
printf("Ho ho ho, think again\n");
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?