View previous topic :: View next topic |
Author |
Message |
lollo Guest
|
evaluate int16 |
Posted: Wed Jun 17, 2009 1:13 am |
|
|
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
|
|
Posted: Wed Jun 17, 2009 1:33 am |
|
|
Sorry,
the initialization value is greater then 500, e.g. 505. |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Wed Jun 17, 2009 8:23 am |
|
|
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
|
|
Posted: Wed Jun 17, 2009 1:49 pm |
|
|
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
|
|
Posted: Wed Jun 17, 2009 1:59 pm |
|
|
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
|
|
Posted: Wed Jun 17, 2009 2:36 pm |
|
|
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? |
|
|
|