[Icc-avr] Calculations
John Baraclough
j_baraclough at zetnet.co.uk
Mon Jan 7 07:29:33 PST 2008
My guess would be that the compiler has done the intermediate
calculation as 'int' which will give some strange results. When using
'long' or 'unsigned long' in calculations you should ensure that you
stay in that domain until the calculation is complete and then translate
to 'unsigned int'.
Try:
temp2 = (unsigned int)(0L/((16L*(long)baudrate)-1L));
That should work.
John
Bengt Ragnemalm wrote:
>
> Hi!
>
>
>
> I have spent some of my spare time during Christmas solving some
> strange problems I have had with a design. My problems was something
> completely different but I ended up with this:
>
>
>
> - I have found a calculation that may be a compiler error.
>
>
>
> This is my test code, baudrate is 38400:
>
>
>
> #define F_CPU 7372800
>
>
> temp2 = F_CPU/(16L*baudrate)-1; // Normal calculation, result OK
> temp2 = 0/(16L*baudrate)-1; // Results into 65535
>
> I guess that the last calculation error is because of some rounding
> problem but obviously the correct answer is 0.
>
>
>
> /Bengt
>
> ________________________________________________________________
>
> Bengt Ragnemalm Tel +46 13 22 24 97
>
> Research engineer FAX: +46 13 10
> 19 02
>
> Linköpings Universitet
> mailto:bengt.ragnemalm at imt.liu.se
>
> Inst. för Medicinsk Teknik Internet: http://www.imt.liu.se
> <http://www.imt.liu.se/>
>
> S-581 85 Linköping SWEDEN
>
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Icc-avr mailing list
> Icc-avr at imagecraft.com
> http://dragonsgate.net/mailman/listinfo/icc-avr
>
More information about the Icc-avr
mailing list