Hello everyone, I am not able to figure out the maximum delay value i can give.If i give to high the simulator stops at that function,please suggst me the maximum delay i can give. Here's the code:
//square wave generator #include <reg52.h> #define on 1; #define off 0; void delay(void); sbit switchpin = P1^0; sbit sqgen = P2^1; void main(void) { P2 = off; while(1) { if(switchpin == 1) { sqgen = on; delay(); sqgen = ~sqgen; } else sqgen = off; } } void delay(void) { int i=0; for(i=0;i<=29000;i++); }
The value of 'i' in the delay function is what i am referring to. Thanks in advance, Arun
Thanks for the reply.As you may have noticed i am taking my first few baby steps in embedded c. Yeah i figured it will be a 16 bit int,so i tried 32767,but it gives the same effect. Later i realized that my int wasn't unsigned.Once i changed it,it worked perfectly
-What's more, you should be careful whether an inner/outer Watchdog is employed.
I am not familiar with this "inner/outer Watchdog" ,could you please explain it in detail.
-What's more and more, why you declare i=0 and then initial i=0 again?
Old c habit of mine lol ,when ever i declare an integer ,i initialize it to 0 to avoid any future logical errors/garbage related to that variable int.
So don't fall into the newbie trap of relying upon HLL delay loops:
http://bit.ly/YPhiRO
Thanks for that Andrew,will do it right away.
"so i tried 32767,but it gives the same effect."
But that means you actually tried 32768. Why? Because the loop should repeat as long as i was <= 32767. The only way to get i > 32767 is to get it to reach 32768. Which doesn't exist for a 16-bit 2-complement integer. Always keep track of the end limits. It's too easy to get hurt by off-by-one errors.