Why LED is not turning OFF - 8051 Timers

Thread Starter

Djsarakar

Joined Jul 26, 2020
317
That is the POWER LED.
Many thanks to all of you for helping. Ok now we have hardware and code that is working.

P1.0 connected to LED
P0.7 connected to button

When I press button ,LED is turning ON and when I releasing button, LED is turning OFF

My only mistake was that I should have used other port pins
 

trebla

Joined Jun 29, 2019
209
Ok. very good, you have got your hardware work!

Now you can test your debounce code, but it needs to be corrected first at point when button release is detected. I suggest checking button instead of if() statement with while() loop. Do you know how to use while() loop for testing?
 

JohnInTX

Joined Jun 26, 2012
4,281
I don't know how many ms of delay I am getting but LED toggles at some intervals
My only mistake was that I should have used other port pins
I don't think so. Your mistakes are rooted in the fact that you don't stay with something long enough to debug it and understand it before trying something else with a whole new set of problems. You had the LED flashing earlier in the thread. Why not leave that alone and work on the code?

I'll leave you alone, now.
Good luck!
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
317
Ok. very good, you have got your hardware work!

Now you can test your debounce code,
I just tested my previous code and now its working
C:
#include<reg51.h>

sbit LED = P1^0;
sbit Button = P0^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 =0x80;
    P3 =0x00;
    P1 =0x00;
    P2 =0x00;
   

  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(10000); //Delay for 424ms *2
              LED = 0; //LED OFF
              Wait(10000); //Delay for  424ms *2

            }    
     }
  }
}
, but it needs to be corrected first at point when button release is detected. I suggest checking button instead of if() statement with while() loop. Do you know how to use while() loop for testing?
Yes I know while loop I will try myself to write program for toggling LED at every push
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
317
I don't think so. Your mistakes are rooted in the fact that you don't stay with something long enough to debug it and understand it before trying something else with a whole new set of problems.
I was not familiar with keil debugging tool. honestly telling I have never debug code before. I have spent lot of time to understand how debug tool work work in keil.

My all attention is on the below points
1. Flow chart
2. state diagram
3. debug

I want to use these three points to solve the problem. I hope i'm better than before
 
Last edited:

trebla

Joined Jun 29, 2019
209
My intention is to show to you practical approach from setting goal (toggling led with each button press with debouncing), drawing flowchart, writing code and testing it. Next goal will be non-blocking debounce routine with remembering different states which has more practical for real time systems. But before we must reach this first goal. If you want to learn how embedded systems work, you must be more consistent. Your code in #152 is not LED toggling routine, it is led blinking on trigger routine.
 

JohnInTX

Joined Jun 26, 2012
4,281
I have never debug code before. I have spent lot of time to understand how debug tool work work in keil.
I do know that and you have made good progress towards learning a useful tool. My comment was referring to how many times you changed the code here looking for a solution instead of drilling down into the problem to find the solution. You showed the same tendencies in the 'state diagram' thread - you would start with something that showed promise, hit a bump, and start again with something completely different with its own set of problems. Several members, including me, pointed out that that was not a good way to do it but you didn't take the advice.

My specific point here was that you already had an LED working on the hardware but it was flashing at the wrong rate. The solution was to leave the LED hooked up as is and fix the flash rate. Instead, you apparently ripped it all out then started again and introduced new problems that had already been solved earlier. That just introduces confusion and makes the project much, much harder to complete.

I want you to know that I am aware of the great effort you are putting into this and I am not scolding you. But I also know that you would have more success if you directed your efforts with more consistency. You have a big project coming up and you're going to have to work smart, not hard.

With that I will leave you in @trebla 's good hands and wish you..
Good luck!

EDIT: Tag me if you want me to run something up in the simulator to check your work.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
317
I do know that and you have made good progress towards learning a useful tool. My comment was referring to how many times you changed the code here looking for a solution instead of drilling down into the problem to find the solution. You showed the same tendencies in the 'state diagram' thread - you would start with something that showed promise, hit a bump, and start again with something completely different with its own set of problems. Several members, including me, pointed out that that was not a good way to do it but you didn't take the advice.
I just want to keep my point. If I have a big problem and I am not able to find a solution, I divide problems it into small pieces. You can also see it in my old threads. For example you can see thread for state diagram where I was asking to make state diagram for bus control system and traffic light system. This was a difficult topic I chose, When I was doing Struggle, I felt that I should solve a very small problem. That's why I was trying to make state diagram LEDs and pushbuttons.

Same in this thread, I was solving the problem, then you gave the idea of debugging. It was very difficult for me to simulate the timer as I did not know anything about the keil debugging tool. That's why I tried to debug code of sensor and led instead of timer program.

My intention to change topics is that I wanted to solve a simple problem first than next solve the difficult problem.

I understood your point that I should stay on the same topic till I get the solution to the problem. I will take care of this. Thank you so much for help
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
317
My intention is to show to you practical approach from setting goal (toggling led with each button press with debouncing), drawing flowchart, writing code and testing it. Next goal will be non-blocking debounce routine with remembering different states which has more practical for real time systems. But before we must reach this first goal. If you want to learn how embedded systems work, you must be more consistent. Your code in #152 is not LED toggling routine, it is led blinking on trigger routine.
@trebla I have understood how the LED will toggle at the each press of a push button. My program is working but it has a problem that there is too much denounce time. I am working on simulator to get 20ms - 30 ms debounc time.

I'll post the code when I have the exact debounc time
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
317
Working code on hardware
C:
#include<reg51.h>

sbit LED = P1^0;
sbit Button = P0^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 =0x80;
    P3 =0x00;
    P1 =0x00;
    P2 =0x00;
    

  while(1) //infinite loop
  {
    if(Button == 0) //If Button pressed
    {
             LED =~ LED;
            
       Wait(450); //debounce 24ms
            
         if(Button == 0) //If Button pressed
            {         
               Wait(450); //debounce 24ms

            }     
     }
  }
}
I am not confirmed that I am getting 24ms of debounce time but it seems I am getting 24ms

1599753215548.png
 

trebla

Joined Jun 29, 2019
209
Look at flowchart and compare your code against it:

Check if button pressed = code line 26
Toggle LED = code line 28
Delay for press debounce = code line 30

Looks good so far but:
Check if button released - if not, check again tasks are not working as we want because:
Mistake 1: you check if button pressed, but if button is hold down, then LED is toggled again after last delay (line 34) and first button check (line 26)
Mistake 2: if you change it to button released checking, but button is still pressed then delay in line 34 is not executed and LED is toggled again in line 26.

I asked you about using while() loop for checking button release, but you are trying do this with if() statement which is for this type debounce routine somewhat harder and demands adding state flag variables. We will do it later but now is simpler solution to use :

line 26: while(Button == Pressed); // wait until button released
next line: Wait(450); // button release debounce delay

Try it and see how it works.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
317
Look at flowchart and compare your code against it:
code for flow chart
C:
#include<reg51.h>

sbit LED = P1^0;
sbit Button = P0^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 =0x80;
    P3 =0x00;
    P1 =0x00;
    P2 =0x00;
  

  while(1) //infinite loop
  {
    while(Button == 0); //If Button pressed

        LED =~ LED;
          
    Wait(450); //debounce 24ms
          
    while(Button == 0); //If Button pressed
  
    Wait(450); //debounce 24ms

  }
}
When I run the code if button is hold down, then LED is toggled and When I press the button then LED is toggled for few ms

Edit: correction checking for 0
 
Last edited:

trebla

Joined Jun 29, 2019
209
In line 26 you want to wait until button is first time pressed, this means that you dont want to toggle LED until button is not pressed and you must write in line 26:

while(Button != 0); // wait until button pressed
 

trebla

Joined Jun 29, 2019
209
while() loop runs until equation in parentheses remains true and exits loop when false. If equation in parentheses returns a nonzero value then in C it means true condition, if returned value is 0 then it means false condition.

For example you can use while(1) loop for keep your program loop run forever because 1 represents allways true condition and nothing except some memory error can change it. If you write while(0) loop to anything inside this loop will be never executed.
And because this is while(check condition); empty-body loop very useful to put program wait until this "check condition" goes false and program can continue.

This method has of course absolutely blocking nature and can be used only when we must absolutely wait some conditions passed until program can continue, for example in hardware initialization/startup sequence.
 
Top