Why LED is not turning OFF - 8051 Timers

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
#71
#81
Help->Debug
@JohnInTX I am trying to simulate behavior of timer0 & mode1

1 tick take 1.085 us
100 * 1.085 = 108.5 us
65535 - 100 = 65435 ( 1111111110011011)
TH0 = 0xFF
TL0 = 0x9B

Timer will overflow at every 108.5 us

Program suppose to toggle port2 pin0 at each timer overflow

C:
#include<reg51.h>

sbit PIN =  P2^0;

void main (void)  // Program start
{
    P0 = 0;
    P1 = 0;
    P2 = 0;
    P3 = 0;
 
    TMOD = 1;         //Timer mode1
   
      TH0 = 0xFF;      
    TL0 = 0x9B;
   
      TR0 = 1;          // Start timer0
   
      TF0 = 0;          // clear timer overflow flag
 
  while(1)
  {
       if (TF0 == 1)   // check Timer0 overflow flag
        {                      
           PIN =~ PIN;               // Toggle PIN P2^0 //                                                          
           TF0 = 0;                // clear Timer0 overflow flag
                   
                      TH0 = 0xFF;      
            TL0 = 0x9B;
         }
  }// while
}//main
 

Attachments

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
It is your code, what do you think it is?
#61
Program : Turn ON LED when button will pressed

C:
#include<reg51.h>

sbit LED = P2^0;
sbit Button = P3^7;

void Wait(int); //Function prototype declaration


void main (void)
{
  Button = 1; // Button PIN input
  LED = 0; //LED off initially

  while(1) //infinite loop
  {
    if(Button == 0 ) //If Button pressed
    {
            Wait(2000); //Delay
            if(Button == 0 ) //If Button pressed
            {             
        LED = 1; //LED ON
        Wait(2000); //Delay
        LED = 0; //LED OFF
  
      }         
        }
  }
}

void Wait (unsigned long int DelayTime)    /* Function to generate delay time */
{
    unsigned long int Count = 0;

    for (Count = 0; Count < DelayTime; Count++)
    {
        /* Do nothing */
    }
}
I think 2000 value gives 106401us (106ms) delay ?

1599674334454.png

1599674371211.png


@JohnInTX When I run program LED is ON for forever ?
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Code is faulty. Debug it.

C:
#include<reg51.h>

sbit LED = P2^0;
sbit Button = P3^7;

void Wait (long unsigned  int DelayTime)    /* Function to generate delay time */
{
    unsigned long int Count = 0;

    for (Count = 0; Count < DelayTime; Count++)
    {
        /* Do nothing */
    }
}

void main (void)
{
  P0 =0x00;
    P1 =0x00;
    P2 =0x00;
    P3 =0x80;


  while(1) //infinite loop
  {
    if(Button == 0 ) //If Button pressed
    {
       Wait(1000); //Delay
         if(Button == 0 ) //If Button pressed
            {             
              LED = 1; //LED ON
              Wait(5000); //Delay
              LED = 0; //LED OFF
 
            }         
     }
  }
}
I don't understand why code stuck at line 26 in simulation

1599678179098.png
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Program waits until Button input changes to zero, check Port 3 state
Have I corrected mistake ?

C:
#include<reg51.h>

sbit LED = P2^0;
sbit Button = P3^7;

void Wait (long unsigned  int DelayTime)    /* Function to generate delay time */
{
    unsigned long int Count = 0;

    for (Count = 0; Count < DelayTime; Count++)
    {
        /* Do nothing */
    }
}

void main (void)
{
  P0 =0x00;
    P1 =0x00;
    P2 =0x00;
    P3 =0x80;


  while(1) //infinite loop
  {
    if(Button == 1 ) //If Button pressed
    {
       Wait(1000); //Delay
         if(Button == 1) //If Button pressed
            {             
              LED = 1; //LED ON
              Wait(5000); //Delay
              LED = 0; //LED OFF
 
            }         
     }
  }
}
 

djsfantasi

Joined Apr 11, 2010
9,163
Have I corrected mistake ?

C:
#include<reg51.h>

sbit LED = P2^0;
sbit Button = P3^7;

void Wait (long unsigned  int DelayTime)    /* Function to generate delay time */
{
    unsigned long int Count = 0;

    for (Count = 0; Count < DelayTime; Count++)
    {
        /* Do nothing */
    }
}

void main (void)
{
  P0 =0x00;
    P1 =0x00;
    P2 =0x00;
    P3 =0x80;


  while(1) //infinite loop
  {
    if(Button == 1 ) //If Button pressed
    {
       Wait(1000); //Delay
         if(Button == 1) //If Button pressed
            {            
              LED = 1; //LED ON
              Wait(5000); //Delay
              LED = 0; //LED OFF

            }        
     }
  }
}
I don’t think so...I’m assuming/thinking that you are simulating the code. I.e., you haven’t wired up the hardware.

So, how are you simulating the button press? What do you do in the simulator to “press the button”?
 

JohnInTX

Joined Jun 26, 2012
4,787
FWIW,
in #119:
Wait(1000) delays about 87ms
Wait(5000) delays about 424ms

As long as P3.7 is high, the LED will be ON about 424ms because of Wait(5000) and OFF about 87ms because of Wait(1000)
If P3.7 is low, the LED will stay OFF

It looks like Wait(1000) is part of a debouncer and as such shouldn't be used to time LED off. IMHO.

Good luck!
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
FWIW,
in #119:
Wait(1000) delays about 87ms
Wait(5000) delays about 424ms

As long as P3.7 is high, the LED will be ON about 424ms because of Wait(5000) and OFF about 87ms because of Wait(1000)
If P3.7 is low, the LED will stay OFF

It looks like Wait(1000) is part of a debouncer and as such shouldn't be used to time LED off. IMHO.

Good luck!
I think following code should be work but I don't understand why don't LED On/off 424ms when I press button ? why its stay on forever

C:
#include<reg51.h>

sbit LED = P2^0;
sbit Button = P3^7;

void Wait (long unsigned  int DelayTime)    /* Function to generate delay time */
{
    unsigned long int Count = 0;

    for (Count = 0; Count < DelayTime; Count++)
    {
        /* Do nothing */
    }
}

void main (void)
{
  P0 =0x00;
    P1 =0x00;
    P2 =0x00;
    P3 =0x80;


  while(1) //infinite loop
  {
    if(Button == 0 ) //If Button pressed
    {
       Wait(1000); //debounce 87ms
         if(Button == 0) //If Button pressed
            {         
              LED = 1; //LED ON
              Wait(5000); //Delay for 424ms
              LED = 0; //LED OFF
              Wait(5000); //Delay for  424ms

            }     
     }
  }
}
Edit : We have pull up resistor that's why checking to 0

when I change if(Button == 1) into if(Button == 0) LED is continuously blinking ?
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
In #123 by inspection:
While button is 0, it will flash the LED 424ms ON and (424+87)ms OFF.
If button is 1, it will just hang on line 26 waiting for button == 0.

A quick turn through the simulator confirms this action.
 
Last edited:

trebla

Joined Jun 29, 2019
547
Check the button and LED are connected to right ports. Check MCU datasheet for output capabilities, are outputs push-pull or open collector type? Open collector output can only sink current.
 

trebla

Joined Jun 29, 2019
547
The second sentence under Port3 heading says that it is a push/ pull (sink/source). Sink means switching to ground and source means switching to Vdd. If you want LED on by writing 1 to output then you should connect LED in series with resistor between output pin and ground.

To test MCU working state is simplest method to write simple LED flashing program. If it works it means MCUs clock is working and output is connected correctly. Then you can test programs with buttons.
 

trebla

Joined Jun 29, 2019
547
Test button with simple program: if button pressed, turn LED on, else turn LED off. Then you can see if input works and in which level activates LED output.
 
Top