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

nid help on LED blinking

i have problem with the led blinking. I using the exmaple program given by keil using uVision3. When i run the program, the LED on the development board didnt blink, it just stay on! wat should i do?

thanks!

Parents Reply Children
  • But if you had followed my advice and used the little search field in the upper right corner of this page, you would have known that you should not use a for loop to count time.

    And you would also have found code that did implement delays without using a dumb for loop.

  • 
      unsigned char j;                    /* LED var */
    
    .
    .
    .
    
        for (j=0x01; j< 0x80; j<<=1)
        {
          .
          .
          .
        }
    

    Because j is an unsigned char, the test for j<0x80 will always be true and you will never exit the loop.

    Pass#1 j = 0x01
    Pass#2 j = 0x02
    Pass#3 j = 0x04
    Pass#4 j = 0x08
    Pass#5 j = 0x10
    Pass#6 j = 0x20
    Pass#7 j = 0x40
    Pass#8 j = 0x80
    Pass#9 j = 0x00
    ...
    Pass#n j = 0x00

  • Whoops - Sorry - My mistake, I didn't read the code closely enough

  • is there any wrong with this code?

    /* BLINKY.C - LED Flasher for the Keil MCBx51 Evaluation Board with 80C51 device*/
    
    #include <REG8252.H>
    
    
    // When you have enabled the option Stop Program Execution with Serial
    // Interrupt, the Monitor-51 uses the serial interrupt of the UART.
    // It is therefore required to reserve the memory locations for the interrupt
    // vector.  You can do this by adding one of the following code lines:
    
     //char code reserve [3] _at_ 0x23;   // when using on-chip UART for communication
    //char code reserve [3] _at_  0x3;   // when using off-chip UART for communication
    
    
    void wait (void)  {                   /* wait function */
    #define TF0_VECTOR 1
    
    int R7=20;
    TR0=0;
    TF0=0;
    
    TMOD=TMOD | 0xF0; //Timer 0,16 bit Timer
    TMOD=TMOD & 0x01;
    
    TL0=0xAF; //Timer 0, set 1 second
    TH0=0x3C;
    TR0=1; //start timer
    
    while(R7!=0)
    {
    if(TF0!=0) //test if the timer overflow
    {
    TR0=0; //stop timer
    TF0=0; //clear the overflow flag
    R7--;
    TR0=1;
    }
    }
                                 /* only to delay for LED flashes */
    }
    
    void main (void)  {
      unsigned int i;                     /* Delay var */
      unsigned char j;                    /* LED var */
    
      while (1) {                         /* Loop forever */
       for (j=0x01; j< 0x80; j<<=1)  {   /* Blink LED 0, 1, 2, 3, 4, 5, 6 */
    
              P1 = j;
                        /* Output to LED Port */
          for (i = 0; i < 10; i++)  {  /* Delay for 10000 Counts */
           wait ();                       /* call wait function */
          }
        }
    
        for (j=0x80; j> 0x01; j>>=1)  {   /* Blink LED 6, 5, 4, 3, 2, 1 */
          P1 = j;
                                    /* Output to LED Port */
          for (i = 0; i < 10; i++)  {  /* Delay for 10000 Counts */
           wait ();                       /* call wait function */
          }
        }
      }
    
    }