Narendra1190
Member level 2
Hi Geeks!!
I am working on project with Atmega8 and want to communicate with another MCU using USART.
I am able to communicate with USART at 19200 Baud rate at 8MHz frequency.
But i am getting values from another MCU in HEXADECIMAL.
I have written Code to convert hex to decimal.I am closer,But getting little Error.
Each next incremental digit i am getting -1 error in result.In Method-2 code i found there is an error in Pow function of math.
Can anyone help me out.
Posting code below.
Method 1
Method 2
Thanks for your time.
I am working on project with Atmega8 and want to communicate with another MCU using USART.
I am able to communicate with USART at 19200 Baud rate at 8MHz frequency.
But i am getting values from another MCU in HEXADECIMAL.
I have written Code to convert hex to decimal.I am closer,But getting little Error.
Each next incremental digit i am getting -1 error in result.In Method-2 code i found there is an error in Pow function of math.
Can anyone help me out.
Posting code below.
Method 1
Code:
#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;
}
Method 2
Code:
#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;
}
Thanks for your time.