Does it make any sense?I provided you with a hint to the solution and you didn't use it. The hint was the word "toggle" in the title.
You forgot to debounce the push-button on the second time pressed.
There is no difference between the first press and the second press. See if you can generalize this for a simpler algorithm,
#include<REGX51.h>
#define TRUE (1)
#define FALSE (0)
#define FOREVER (TRUE)
#define LED_OFF (0)
#define SWITCH_PRESSED (0)
/*set bit P3^2 to Switch*/
sbit Switch = P3^2;
/*set bit P1^0 to LED*/
sbit LED = P1^0;
/* This is delay function */
void Delay (unsigned long n)
{
unsigned int i;
for (i = 0; i < n; i++);
}
void main (void)
{
LED = LED_OFF;
while (FOREVER)
{
if (Switch == SWITCH_PRESSED)
{
LED = ~LED;
Delay (20000);
}
}
}
Can you please tell me What's wrong in program?. Does it not toggle LED with push button?You are going around in circles aimlessly seeking out any solution that might have a chance of working but with no concrete basis for correct design.
Where is your switch debounce?
Why do you need Delay (20000)?
What do you need to accomplish in your Initialization?
You need to create a single Switch_Pressed() function, not simply (Switch == SWITCH_PRESSED)
If I held the button down continuously. LED continuously Turn ON and turn OFFWhat happens if you held the button down continuously?
#include<REGX51.h>
/*set bit P3^2 to Switch*/
sbit Switch = P3^2;
/*set bit P1^0 to LED*/
sbit LED = P1^0;
#define TRUE 1
#define FALSE 0
#define FOREVER TRUE
#define Switch_Pressed 0
#define Switch_Not_Pressed 1
/* This is Debounce time */
void Debounce_Time (unsigned long n)
{
unsigned int i;
for (i = 0; i < n; i++)
{
}
}
unsigned int check_Switch(void)
{
if(Switch == 0) /* Switch is pressed */
{
Debounce_Time(2000); /* wait 20 msec */
if(Switch == 0) /* check switch again */
return Switch_Pressed; /* switch is really pressed */
}
}
int main(void)
{
unsigned int old_state = check_Switch();
while (TRUE)
{
unsigned int New_state = check_Switch();
if(New_state != old_state && New_state == Switch_Pressed)
{
LED = ~LED;
}
old_state = New_state ;
}
}
OK Please tell me, what's wrong in program? Where does it fail?No. It doesn't make any sense.
.
#include<REGX51.h>
/*set bit P3^2 to Switch*/
sbit Switch = P3^2;
/*set bit P1^0 to LED*/
sbit LED = P1^0;
#define TRUE 1
#define FALSE 0
#define FOREVER TRUE
#define Switch_Pressed 0
#define Switch_Not_Pressed 1
#define LED_OFF 0
/* This is Debounce time */
void Debounce_Time (unsigned long n)
{
unsigned int i;
for (i = 0; i < n; i++)
{
}
}
void Initialization()
{
Switch = Switch_Not_Pressed ;
LED = LED_OFF;
}
/* check switch */
bit check_Switch(void)
{
if(Switch == Switch_Pressed) /* Switch is pressed */
{
Debounce_Time(2000); /* wait 20 msec */
if(Switch == Switch_Pressed) /* check switch again */
return Switch_Pressed; /* switch is really pressed */
}
return Switch_Not_Pressed;
}
unsigned int wait_for_a_switch(void)
{
unsigned int return_value;
if(!Switch)
{
return_value = check_Switch();
}
return return_value;
}
unsigned int wait_for_no_switches(void)
{
unsigned int retuarn_value;
retuarn_value = Switch_Not_Pressed;
return retuarn_value;
}
int main(void)
{
unsigned int old_state = wait_for_a_switch();
Initialization();
while (TRUE)
{
unsigned int New_state = wait_for_a_switch();
if(New_state != old_state && New_state == Switch_Pressed)
{
LED = ~LED;
}
old_state = New_state ;
}
}
Yes I agree with you. very soon I will purchase microcontroller and electronic componentsAh, also, OP needs to use an actual microcontroller and some electronic components, instead of blindly guessing the flow in the simulator.
Are you using a simulator? I didn't catch that.Yes I agree with you. very soon I will purchase microcontroller and electronic components
But right now I need help in program. I am just waiting for reply. Is there anything wrong in program? Now does it make any sense ?
Yes I am using simulator software. I don't have microcontroller and other electronic component. There is no electronic market, where I live. I have to go for away from my place And They sell very expensive items but I will purchase very soon. I have just financial issues.Are you using a simulator? I didn't catch that.
You cannot test the operation of a push-button with a simulator. You need to use real-world components.
Test with real-world components.
Sorry but I didn't understand that. What you want. you want me to draw flow chart or you want me to write program.Write a procedure for the following:
Edit: This is not what we want but we will get to that later.
I know I'm coming to this thread late and that you have a whole bunch of flowchart/code versions beyond this initial one, but I am getting little sense that you are making much effort to understand what the shortcomings of each of the versions. Instead, you seem to be making almost random changes hoping that, at some point, something will just happen to work. Design-by-happening is not a methodology that has a long track record of success.Hi
Push button and LED is connected with 8051 Microcontroller. When the push-button press, the LED turns ON. Release the push-button and there is no change to the status of the LED. Press the push-button again and the LED turns OFF. Repeat forever.
Does this flow chart match with above Statements ?
View attachment 137333
Good to see your reply. I made many flow chart wrong. I don't want that some how it should be work by making random flow chart. I always tried my best effort to solve problem. Do you see any improvement in me before starting. I want to tell you that I want to learn. That's why I follow your and other advice. I can never compare between you and me because you have lot of year experience. you have learned lot over timeI know I'm coming to this thread late and that you have a whole bunch of flowchart/code versions beyond this initial one, but I am getting little sense that you are making much effort to understand what the shortcomings of each of the versions. Instead, you seem to be making almost random changes hoping that, at some point, something will just happen to work. Design-by-happening is not a methodology that has a long track record of success.
Initially I made flow chart which was wrong. After that I didn't make flow chart. I wrote directly program and I have tested that program in simulator. It works as I wanted to do. I wanted to know that what's wrong in program. any logical error or any mistakes .So let's examine this flowchart, and let's assume that our switch has no bounce issues and that we catch each event perfectly. Under those ideal conditions, this flowchart still will not do what we want. Why not?
After initializing the system I ask if the button is pressed. Let's say that it is. So you turn on the LED. Now you check if the button is pressed and let's say that it is not. So you go back to the top of the loop and check if the button is pressed. Let's say that it is. Now you WANT to turn the LED off, but your flowchart has you turn the LED on again.
So you see that? More importantly, do you see how I walked around the flowchart doing what it told me to do and compared that to what I wanted to have happen?