Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

LED Toggling Only One Time by 89c52

Status
Not open for further replies.

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
817
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,492
Please let me know that I made a program for LED toggling by making P3^3 as a switch(as a pulse switch) but problem is that (when I turn on switch LED toggling starts) but I want that if I continuous turn on switch the LED should toggle only one time and stop what should i do?
 

In my assembly code, I just use a flag (a bit in a bit-addressable RAM byte).
While the switch is off, I set this flag (say ReadyToToggle).
When the switch is ON AND the ReadyToToggle flag is set, a subroutine is called to let the LED turn ON for a certain desired time and the flag ReadyToToggle is reset.
Even if the switch stays ON the subroutine will not be called again since ReadyToToggle is cleared.
Obviously when the switch becomes off again, ReadyToToggle is set so that the previous steps could be repeated.
 
Last edited:

A workaround, since you likely program the code using C or another high language (I work with assembly only), is to use a whole byte as a flag by checking its stored value if it is zero or not zero. Let us call this byte ReadyToToggle. To set it, we just store in it any number (say 1) and when it needs to be cleared (in the LED subroutine) we store 0 in it. I guess you know how to check a byte if it is zero or not.

Good luck.
 

Put your code here
did you put your code in a loop ?
which language you are used to write the code ?
 

Hello,
Post your code first, there will be some minor changes!!!
or use:
Code:
#include<reg52.h>
sbit sw =P1^1;
sbit led = P1^0;
void delay(unsigned int d);
main()
{while(1)
{
 led =0;
 if(sw==0)
{
 led = 1;
 delay(1);

  if(sw==0)
  {
    led =0;
	while(1)
	{
	if(sw==1)
	   {break;};
         }	
}
} 
}
}

void delay(unsigned int d)
 {
  unsigned int i,j,k;
  for(i=0;i<d;i++)
   {
    for(j=0;j<255;j++)
   	{
	 for(k=0;k<100;k++)
   	{}
	}
   }
 }
 

View attachment Hold.txt
This is my program for LED toggling.

Code:
#include<reg52.h>
sbit SW=P3^3;
sbit t0m2=P1^3;
void to_m2(unsigned int);
void LED_Toggle();
void main()
{
SW=1;
if(SW==0)
LED_Toggle();
}
void LED_Toggle()
{
t0m2=1;
to_m2(1); 	   // 1 sec
t0m2=0;
to_m2(1);	   // 1 sec
}
void to_m2(unsigned int f)
{
unsigned int x;
f=f*10000;
for(x=0;x<=f;x++)
{
TMOD=0x02;
TH0=0xA4;    // -92
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}
}
 

This is just an example

#include<reg52.h>
sbit SW=P3^3;
sbit t0m2=P1^3;

// any MCU pin not used can be treated as a bit flag
// This is just to present the idea in general
sbit flag=P3^1; // supposed not used

void to_m2(unsigned int);
void LED_Toggle();
void main()
{
SW=1;
flag=1; // ready to toggle

if(SW==0)
{
if(flag==1)
{
LED_Toggle();
flag=0; // to disable repeated call
}
}
else
{
flag=1; // ready for a new SW==0, here SW==1
}

void LED_Toggle()
{
t0m2=1;
to_m2(1); // 1 sec
t0m2=0;
to_m2(1); // 1 sec
}

void to_m2(unsigned int f)
{
unsigned int x;
f=f*10000;
for(x=0;x<=f;x++)
{
TMOD=0x02;
TH0=0xA4; // -92
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}
}
 
code.JPG
This is my program
 

Do : after one toggle make your program to go in forever loop until switch is released... i.e. after led =1 >>> led =0 make if loop such that it will remain in it till switch is released
if(sw==0)
{
led =0;
}
 

But I did not use forever loop i.e while(1) or for(;;) and actually I want that after one execution of LED toggle the program should be stop even I the switch continuous turn on.
 

But I did not use forever loop i.e while(1) or for(;;) and actually I want that after one execution of LED toggle the program should be stop even I the switch continuous turn on.

Do you mean, you did try your program after updating it as on post #8 and you didn't get what you are looking for?
 
ok
Code:
#include<reg52.h>
sbit SW=P3^3;
sbit t0m2=P1^3;
void to_m2(unsigned int);
void LED_Toggle();
void main()
{
int a=0; 
SW=1;
if(SW==0)
{ 
  if(a==0)
   {
    LED_Toggle();
   a=1;                                      // after one toggle it will stop because now a is not zero
}
  else
 {(do nothing)}                              // do nothing - may be your program to do some other things
}                                      
void LED_Toggle()
{
t0m2=1;
to_m2(1); 	   // 1 sec
t0m2=0;
to_m2(1);	   // 1 sec
}
void to_m2(unsigned int f)
{
unsigned int x;
f=f*10000;
for(x=0;x<=f;x++)
{
TMOD=0x02;
TH0=0xA4;    // -92
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}
}
 

Problem solved thanku sir
I used a break command for that purpose.

---------- Post added at 01:09 ---------- Previous post was at 01:06 ----------

Thanku for your help but problem solved by which did u post me i.e by BREAK command.



Thank you
 

You dont need to write thank you etc.. Just click the helped me tab a the bottom(Its specially for this)

Good Luck :)
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top