a weird problem of "FOR loop"

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

a weird problem of "FOR loop"

3,695 Views
eos33
Contributor I
got a weird problem when i use the dual FOR loop in my program.
this program will turn off the LED2 when it finished.
but i found if the variable i and j greater than some value,the FOR loop couldn't quit.
the entire program will loop forever..~___~"
i had checked the C reference book,think the syntax should be correct.
the attached file is my test project.
could anyone please try it if it happened as well?
 
 
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#define LED2 PTBD_PTBD7
 
void MCU_init(void); /* Device initialization function declaration */
 
void test(void){
 short int i,j;
 for(i=0;i<=600;i++){
  for(j=0;j<=600;j++){
   
   LED2 = 0;
   
  }  //j end
 }  //i end
 
} //test end
 
void main(void) {
 
 /* Uncomment this function call after using Device Initialization
    to use the generated code */
 /* MCU_init(); */
 
 //EnableInterrupts; /* enable interrupts */
 
 /* include your code here */
 PTBDD_PTBDD7 = 1; //Set PTB7 as an output
 
 LED2 = 1;  //turn off LED2
 test();      //turn on LED2
  LED2 = 1; //turn off LED2
 

 for(;:smileywink: {
   __RESET_WATCHDOG(); /* feeds the dog */
 } /* loop forever */
 /* please make sure that you never leave this function */
}
Labels (1)
Tags (1)
0 Kudos
5 Replies

462 Views
eckhard
Contributor V
Hello,
 
are you shure your loop won´t take too long for the watchdog ?
You could try to set this statement __RESET_WATCHDOG();
ínto the loop of the test function.
 
void test(void){
 short int i,j;
 for(i=0;i<=600;i++){
  for(j=0;j<=600;j++){
   
   LED2 = 0;
   
  }  //j end
__RESET_WATCHDOG();
 }  //i end
like this.
 
Eckhard
0 Kudos

462 Views
airswit
Contributor III
i think a short int is 1 byte right? which means that the max value it'll get is +127, meaning that the loop is always going to execute. try a regular integer, and see if that does it (an int, if 2 bytes, can count to +32767). hope that does it for you
0 Kudos

462 Views
peg
Senior Contributor IV

Hi,

I copied your function from your post and dropped it into a CW5 example project and it works for me!

The short int is implemented as two bytes on the stack so 600 is no trouble.

The double loop takes about 9.5 million cycles on an s08GB60 so yes the COP is going to cause you grief if it is enabled.

So apart from that I don't know what your problem is.

Regards Peg

 

0 Kudos

462 Views
eos33
Contributor I

Hi all,

thank you for your kindly help.

it's really helpful for me as the beginner.

juz as you mentioned,when i disable the SOPT1_COPE.

everything works fine now...:smileyvery-happy:

thank you all again...

0 Kudos

462 Views
peg
Senior Contributor IV

Hi,

Glad to hear you found your problem,

When you get "weird" problems like this you should run your code in a simulator or debugger, in this case you would have got your answer fairly quickly.

Regards Peg

0 Kudos