CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Help, am I stupid or what ?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
MikeW



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

Help, am I stupid or what ?
PostPosted: Tue Nov 29, 2005 3:57 am     Reply with quote

I just cant see the wood for the trees, why wont this go round the loop 50 times ?, it just falls through.

I can see why, because the BRA instruction is an unconditional branch.


is it me, or is it a compiler error V3.238 pchw, PIC18F4480


------------

for (LED_Counter=50;LED_Counter>=0;LED_Counter--)
{
Flow_LED_Brightness=LED_Counter;
}


------------

compiles to

................... for (LED_Counter=50;LED_Counter>=0;LED_Counter--)
183C: MOVLW 32
183E: MOVWF LED_Counter
.................... {
....................
.................... Flow_LED_Brightness=LED_Counter;
1840: MOVFF LED_Counter,Flow_LED_Brightness
....................
.................... }
1844: DECF LED_Counter,F
1846: BRA 1840
.....
Ttelmah
Guest







PostPosted: Tue Nov 29, 2005 4:47 am     Reply with quote

Change LED_COUNTER, to a signed type.
The problem is that for an unsigned value, LED_COUNTER>=0, must _always_ be true. Hence no test is done, and there is a permanent loop.

Best Wishes
MikeW



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

dont like to used signed
PostPosted: Tue Nov 29, 2005 5:12 am     Reply with quote

thanks for your reply.

I think i prefer to do this though...


for (LED_Counter=50;LED_Counter!=0;LED_Counter--)
{
Flow_LED_Brightness=LED_Counter;
}


Mike
Ttelmah
Guest







PostPosted: Tue Nov 29, 2005 5:18 am     Reply with quote

However this gives one less count. If you want to have the same number of counts as before, you need to increase the initial value by one.

Best Wishes
MikeW



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

PostPosted: Tue Nov 29, 2005 5:50 am     Reply with quote

Ttelmah, once again, many thanks.

the actual count in this instance is not important for me since I am just testing the PWM'ing of some LEDS to test varying brightness, but i do take your point.

Mike
gremlin
Guest







PostPosted: Tue Nov 29, 2005 8:39 am     Reply with quote

It is actually quite interesting, that the compiler optimisation is 'smart' enough, to realise that the original test, could never go 'false'. This is one of an increasing number of cases, where this sort of thing happens. I'd have expected that there may actually be a 'warning', that the condition is always true (there are quite a few warnings like this, that only appear if the error reporting is turned up, since they are normally an 'annoyance').

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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