Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Re: Simple Binary to Decimal conversion code not working!!!

2011-08-16 by R E Purcella

you have two i integer variables. One is in float power() and the other 
in float Bin2Dec() Normally that would not be a problem but, the one in 
Bin2Dedc is volatile. Is that making it seen as the same as the other 
"i" variable. Thus it changes the i in Bin2Dec unexpectedly.???? 
Volatile isn't needed except in interrupt routines where outside 
variables are manipulated. For that situation the outside variables need 
to be volatile. But, when passing a variable and a resulting value gets 
returned then volatile is not needed.

What result is main expected to return? 0 or decimal result?
rep

On 8/15/2011 11:03 PM, stevec wrote:
>
>
> yes, this is really not a good approach - using floating point.
>
> Suggest you get assistance from avrfreaks.net, user forum.
>
> --- In AVR-Chat@yahoogroups.com <mailto:AVR-Chat%40yahoogroups.com>, 
> "Cat" <catalin_cluj@...> wrote:
> >
> > I can’t say what’s wrong at first sight, but did you try it with 
> integers instead of floats?
> > It might be easier on the compiler, maybe debugger?
> >
> > Also, disabling all optimization might help the debugger.
> >
> > Good luck,
> >
> > Cat
> >
> > From: rastavibration86
> > Sent: Friday, August 12, 2011 4:35 AM
> > To: AVR-Chat@yahoogroups.com <mailto:AVR-Chat%40yahoogroups.com>
> > Subject: [AVR-Chat] Simple Binary to Decimal conversion code not 
> working!!!
> >
> > 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;
> > }
> >
> >
> >
> > ------------------------------------
> >
> > Yahoo! Groups Links
> >
> >
> >
> >
> >
> > [Non-text portions of this message have been removed]
> >
>
>

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.