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

evaluate int16

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







evaluate int16
PostPosted: Wed Jun 17, 2009 1:13 am     Reply with quote

Here is my code:

Code:
int16 k=450;
while(1){
   k++;
   if(k>=500)
      k=0;
}


After the last line I'll expect that k=0 but this doesn't happen. If I substitute 500 with 200 the code works fine. Why?
lollo
Guest







PostPosted: Wed Jun 17, 2009 1:33 am     Reply with quote

Sorry,
the initialization value is greater then 500, e.g. 505.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Jun 17, 2009 8:23 am     Reply with quote

When are you "looking" at the value of k? Notice that you are ALWAYS inside the while loop so the value of k is going to get incremented every time around the loop. If you set k = 450 initially of couse it will ALWAYS be >= 200 every time around the while loop and you will ALWAYS get k = 0 as a result.

If you set the "if" condition to k >= 505 then k will be incremented from 450 until it hits 505, at which point it will be reset to zero by your "if" statement. You are still in the while loop. Now k will count from zero to 505 and be resetted. So you are going to get a different number for k every time around the loop.
lollo
Guest







PostPosted: Wed Jun 17, 2009 1:49 pm     Reply with quote

my code is:
Code:

int16 k=505;
while(1){
   k++;
   if(k>500)
      k=0;
}


After the first cycle I'll expect that k=0, but k is 506. If I replace "if(k>500)" with "if(k>200)", k=0 after the first cycle.
I've the same trouble if I write this code:
Code:
int16 k=505,j=500;
while(1){
   if(k>j)
      k=0;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 17, 2009 1:59 pm     Reply with quote

1. Post a small test program that has that code in main(). Post the
#include line for the PIC, post the #fuses, #use delay(), and main().

2. Post your compiler version.

3. Post how you are testing this problem. Are you using a Watch
window with a debugger ? Are you single-stepping it ? Are you
using the CCS IDE or MPLAB to do this ?
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Wed Jun 17, 2009 2:36 pm     Reply with quote

I tried this test program using PCH 4.064 compiler:
Code:

#include <18F2525.h>
#device ADC=10  //set for 10 bit A/D
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ENABLE = PIN_B0, ERRORS)


void main(void)

{
int16 k=505;

while(1)
{ //breakpoint set here
   k++;
   if(k>500)
      k=0;

   }     
}//void main(void)


I added a WATCH from MPLAB SIM and set a breakpont right after the while(1) expression. I hit RUN to get the simulator to get to the breakpoint. Then I use the STEP_INTO button and I see k = 505, and I hit STEP_INTO again and I see k = 506. Hit STEP_INTO again I see k = 0. Repeating the process k is increased ( 1, 2, 3 4, etc.).

So I don't think there is anything wrong. Do you see the same thing using the simulator?
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