This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Why is this On & Off uneven.

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


Parents Reply Children
  • Andy Thanks for valuable TIP.

    Regards,
    Jai

  • 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.