Why LED is not turning OFF - 8051 Timers

jpanhalt

Joined Jan 18, 2008
11,087
I have already tested the LED. The LED works well. The problem can only be in the switch button and in the related code. But I do not understand how to find the real problem
Here's a test circuit for your switch:

1599731140565.png

Does it work as expected? If yes, then problem is not switch. If no, get a new switch and start over at post #1. ;)
 

jpanhalt

Joined Jan 18, 2008
11,087
The problem can only be in the switch button and in the related code.
So now, you have determined the problem is in the code. Agree?

Let me suggest until you get the code working for everything except debouncing that you use a hardware debounce on the switch. IC's are made for that or a simple resistor/capacitor can be used.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
For this simple test program routine in #132 you do not need any debounce code. Simply read button and turn on LED.
I have tested following code When I press onboard switch button LED is not turning ON

C:
#include<reg51.h>

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


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

    while (1)
    {

      if (Sensor == 0)
       {
            LED = 1;

       }
      else
       {
           LED = 0;
       }
     }
}
When I change if (Sensor == 0) into if (Sensor == 1) still problem is same LED is not turning on
 

trebla

Joined Jun 29, 2019
542
No, in this code you must pull the pin down with button. Remember, MCU has internal pull-up resistor and button must then be between input pin and ground.
 

trebla

Joined Jun 29, 2019
542
Do you have a breadboard? You need set button on breadboard and take two wires to your MCU board, one to ground and second to MCU input.
 

JohnInTX

Joined Jun 26, 2012
4,787
According to the schematic in #139, you are sourcing current into the LED to turn it on. The 89C51 will only source 60uA at 2.4V typical. Consider driving your LED by sinking current to turn it on. PORT0 will sink more current than PORTS 1, 2 or 3 so think about that.

You show a 56 ohm resistor in the schematic and 10K in your breadboard photo, if I read it right. Which is it? How many mA do you need to drive the LED and what should the resistor value be?

The pin driving the LED is labeled '3'. Pin 3 on a DIP-40 package is P1.2 The code expects the LED on pin 21, P2.0

You can see if your I/O circuitry is working by using your meter on the pin. @trebla pointed out in #140 that your input pin doesn't change it's logic level when you push the button. That is something you could have determined by measuring the voltage at the pin while pushing the button on and off. Same with the LED output. You can see if your code in #138 is following the button by pulling the LED out of the circuit and just measuring the voltage at the pin as you push the button.
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
The last one, #138. If you use different input do not forget modify your code also.
Code
C:
#include<reg51.h>

sbit LED = P2^0;
sbit Sensor = P0^7;


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

    while (1)
    {

      if (Sensor == 0)
       {
            LED = 1;

       }
      else
       {
           LED = 0;
       }
     }
}
Hardware Result - LED is turning ON without pressing switch button


IMG_20200910_175510.jpg
Mod edit: do not edit a post after someone has commented on it. Make a new post to keep the timeline valid - JohnInTX
 
Last edited by a moderator:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
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
542
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,787
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
489
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
542
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,787
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
489
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:
Top