Simple Binary to Decimal conversion code not working!!!
2011-08-12 by rastavibration86
Hi!
I have a problem with this source code which is going to run in ATmega1284P.
It is a part of another source code, but this simple part is not working and I can not find the reason.
I am using AVR Studio and I have used both AVR Simulator and AVR Simulator 2 as debug platform and I have also tried with JTAGII. I obtain the same results: nothing.
The problem is that no proper conversion is done and when executing:
Dec = Dec + binarypower;
Debug seems not to be working any more.
Here is the source code. I know it is too simple and that's why I can't understand why it is not working as expected.
Looking forward for your help. Thanks a lot!
#include <avr/io.h>
#include "BitOperationMacros.h"
float Power(float, int);
float Bin2Dec(uint8_t, int);
int main(void)
{
float Decimal = 0;
uint8_t V = 0x0B;
Decimal = Bin2Dec(V, 8);
return 0;
}
float Power(float base, int exponent)
{
float volatile result=1;
for(int i=1; i<=exponent; i++)
{
result = result*base;
}
return result;
}
float Bin2Dec (uint8_t Value, int nbits)
{
float volatile Dec = 0;
int volatile i=0;
float volatile binarypower=0;
while(i<=(nbits-1))
{
if(CHECKBIT(Value,i) == 1)
{
binarypower=Power(2.0,i);
Dec = Dec + binarypower;
}
i++;
}
return Dec;
}