#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;
}