![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
gunking88
Joined: 18 Jun 2004 Posts: 11 Location: WV
|
here is my question again,...look inside , thanks |
Posted: Tue Aug 10, 2004 9:05 am |
|
|
part of my code..
...
signed int16 error=0; // real error read from ADC
...
signed int16 vout=0;
signed int16 vref=204; //204 in 8 bits for 4V, output at 12V
every 50us following executes
...
vout=read_adc(adc_read_only);
error=vref-vout;//difference between ref and output
if (error>11){error=11;}
if (error<-10){error=-10;}
here is my question,
I set vout=FF, and I used the watch to check error, I found
error=FFCD, after error=vref-vout
but error=0X0B after if if (error>11){error=11;}
it seems that the error was treated as positive value here.
why this happened? it is the problem of the debugger mode or my code?
thanks.. |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Tue Aug 10, 2004 9:39 am |
|
|
try this:
if ((signed int16)error>(signed int16)11) {error==11;}
From my experiance with the compiler it is entirely possible that it thinks error is an unsigned value. So if this works I would go one more step and try:
vout=(signed int16)read_adc(adc_read_only);
Also try different versions of the compiler, some versions do things different than others.
Trampas |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
Re: here is my question again,...look inside , thanks |
Posted: Tue Aug 10, 2004 10:03 am |
|
|
gunking88 wrote: | part of my code..
...
signed int16 error=0; // real error read from ADC
...
signed int16 vout=0;
signed int16 vref=204; //204 in 8 bits for 4V, output at 12V
every 50us following executes
...
vout=read_adc(adc_read_only);
error=vref-vout;//difference between ref and output
if (error>11){error=11;}
if (error<-10){error=-10;}
here is my question,
I set vout=FF, and I used the watch to check error, I found
error=FFCD, after error=vref-vout
but error=0X0B after if if (error>11){error=11;}
it seems that the error was treated as positive value here.
why this happened? it is the problem of the debugger mode or my code?
thanks.. |
It is a problem with the compiler, and one that I have seen occur several times in different compiler versions. The 'key' is that the CCS compiler does not 'cast forward' when doing either arithmetic, or comparisons. Instead, at some times, the 'lowest' type is taken. Try this:
if (error>(signed int16)11L){error=11L;}
if (error<-10L){error=-10L;}
The problem is that the '11', by default is an unsigned short. In some (quite a lot) of the compiler versions, this is seen, and the comparison is then only done on the low byte. 0xCD is greater than 11, so error gets set to 11. Generally, when dealing with mixed types, my 'rule' now is to allways explicitly cast every value, using the 'L' declaration for constants, and forcing 'signed' when required. Doing this, 99% of the compiler versions work. Without this, the success rate falls massively..
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|