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 CCS Technical Support

Addition Loop Error

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



Joined: 14 Sep 2003
Posts: 2

View user's profile Send private message

Addition Loop Error
PostPosted: Mon Jul 25, 2005 11:31 pm     Reply with quote

Hi All,

I keep getting an wrong addition total from the folloing loop:

int32 pos_peak;
pos_peak = 0;
for(i=32; i!=0; i--){
pos_peak += 50000;
}

The total should be 1,600,000 but I get 2148283648. Stepping through it I get the correct result but at full speed it's always wrong. Can someone tell me why and how to fix it? Is it the ADDWFC instruction?

Thanks,
David P
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 25, 2005 11:59 pm     Reply with quote

Here's a full test program. I tested this with the MPLAB 7.20 simulator
(with UART1 writing to the Output Window), and also with the
PicDem2-Plus board writing to a terminal window on the PC.
Both displayed this:
Quote:
result = 1600000


Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//====================================
void main()
{
int32 pos_peak;
int8 i;

pos_peak = 0;

for(i=32; i!=0; i--)
   {
    pos_peak += 50000;
   }

printf("result = %lu \n\r", pos_peak);   

while(1);
}
Ttelmah
Guest







PostPosted: Tue Jul 26, 2005 4:37 am     Reply with quote

The 'error' result, is suspiciously close to 2^31 away from the real result (in fact 800000 away from it). If the sum was subtracting not adding, I'd say there was a confusion between a signed/unsigned integer somewhere...

Best Wishes
davie1_1



Joined: 14 Sep 2003
Posts: 2

View user's profile Send private message

PostPosted: Sat Jul 30, 2005 12:48 pm     Reply with quote

for some reason I added i++ after the loop and now I get the correct answer in full speed. If I remove the i++ it does it again. I don't know why this would be but increment is a single instruction so I'll leave it.

David P
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