We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
This code below is for 89s2051. It produces uneven on and off times. I viewed the port pin with oscilloscope and the on time is 3.2ms and off time is 2.5ms. The delay being the same should it not produce waveform with equal on off times.
#include <AT89X51.H> #include <INTRINS.H> /*---------------------------------------------------- ****** port and bit assignment for LCD ******* ----------------------------------------------------*/ typedef unsigned char bit_8; /*------------------------------- *** Main programe starts here *** -------------------------------*/ void wait (void) { /* wait function */ ; /* only to delay for LED flashes */ } void main() { unsigned int i; i = 0; P1_3 = 0; while(1) { P1_3 = 0; for (i = 0; i < 200; i++) wait(); P1_3 = 1; for (i = 0; i < 200; i++) wait(); } } Thanks & Regards, Jai
I figured it out the change below produces the even on and off required:
while(1) { for (i = 0; i < 200; i++) wait(); P1_3 = 1; for (i = 0; i < 200; i++) wait(); P1_3 = 0; }
Regards, Jai
They may be OK for trivial exercises like this, but are not generally a Good Thing - especially in the presence of Compiler Optimisations
See: www.8052.com/.../162556
See also: www.8052.com/.../175943
Note that the above are not specific to Keil or the 8051/2
Andy Thanks for valuable TIP.
700 us delay difference just if the while loop ends after a delay or after a signal toggle?
I would like to see the assembler output of these two loops...
But as noted - delays should not be made in software using a high-level language. Using assembler makes it possible to count instruction clock cycles. The only time when it may be ok to implemnt a delay in C is if the need is for a very very short delay that can be fulfilled with a few nop() intrinsics - this obviously requires that the compiler supports a nop() intrinsic.