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.

problem related to toggle LED and timer

Status
Not open for further replies.

mf1364

Full Member level 3
Joined
Dec 4, 2007
Messages
185
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,700
Hi every body
I want to write codes in this way that with a micro switch, I turn LED ON/OFF. but in this way that when for the first time I press micro switch the LED should be turn on and right after I pick up my hand from micro switch, timer start calculating 1 sec, and if before 1 sec I press the micro switch again, the timer should reset, and calculating 1sec again also don’t toggle the LED, but if I press the micro switch after 1sec , the led should toggle and this process should continue .
At first I wrote a easier program and if this easier one work proper I expand it to the above question
I expected in this codes, The LED toggle when I press the micro switch , and when I press the Micro switch for the first time, the LED should turn ON and right after I pick my hand up from micro switch the timer start to calculating 1sec and after one second toggle the LED . Here is my codes:

Code:
char flag1=1;
char hhh=0;
char count=0;
void RegConfigration()
{
//Timer2 Registers Prescaler= 16 - TMR2 PostScaler = 16 - PR2 = 150
t2con=0b01111011;
PR2 = 150;
tmr2=0;
  PIR1.TMR2IF = 0;
  PIE1.TMR2IE = 1;
  INTCON.GIE = 1;
  INTCON.PEIE = 1;
}
void pinsconfigration()
{
   TRISB.f7 =0;
   PORTB.f7 =0;
   trisc.f3 =1;
}
void interrupt()
{
  if (PIR1.TMR2IF == 1)
  {
   count++;
   PIR1.TMR2IF = 0;
   if (count==100)
   {
PR2 = 150;
tmr2=0;
    PORTB.F7 =0;
    count=0;
    t2con.tmr2on=0;
  }
}
}
void main()
{
 pinsConfigration();
 RegConfigration();
       hhh=0;
       flag1=1;
       flag2=0;
  while(1)
  {
if (portc.f3==0) { if (hhh==0)
                 {  portb.f7=1;
                    flag1=0;
                    hhh=1;
                 }
                }
if (hhh==1)  { if (portc.f3==1)
            {
t2con.tmr2on=1;
}
}
if (portc.f3==0) { if (flag1==0)
                 {
                  portb.f7=0;
                  hhh=0;
                  flag1=1;
                  }
                  }
}
}

But my problem is that some times I should press several time for toggling the LED and I don’t know why micro switch don’t work sensitive? ! and the next problem is that the timer don’t work and don’t turn LED off after one second ! I am using miKro C compiler and PIC16f873
 
Last edited:

Hai...

I have not verified your code completely and i have not written the full code.

I am making a suggestion to you to connect the switch with the external interrupt pin (INT). So when ever you press the switch, your code will go to interrupt routine.

Now follow the code as below

Code:
void interrupt()
{
if (INTCON.INTF == 1)
{
	if(first_time == 0) //check are you visiting interrupt first time or not
	{
	LED = 1; 
	}
	else 
	{	
		t2con.tmr2on=0;
		restart_the_timer ();	
	}
        INTCON.INTF = 0;
}

else if (PIR1.TMR2IF == 1)
	{
		PIR1.TMR2IF = 0;
		LED = ~LED;
		t2con.tmr2on=0;
	}
}

This will help you to set the LED port pin at the first time. Then it will restart the timer.

All the best
 

Hai...

I have not verified your code completely and i have not written the full code.

I am making a suggestion to you to connect the switch with the external interrupt pin (INT). So when ever you press the switch, your code will go to interrupt routine.

Now follow the code as below

Code:
void interrupt()
{
if (INTCON.INTF == 1)
{
	if(first_time == 0) //check are you visiting interrupt first time or not
	{
	LED = 1; 
	}
	else 
	{	
		t2con.tmr2on=0;
		restart_the_timer ();	
	}
        INTCON.INTF = 0;
}

else if (PIR1.TMR2IF == 1)
	{
		PIR1.TMR2IF = 0;
		LED = ~LED;
		t2con.tmr2on=0;
	}
}

This will help you to set the LED port pin at the first time. Then it will restart the timer.

All the best

thank you very much but it is not reasonable to use external interrupt for a micro switch because as you know the just have one external interrupt so if i want to use more than one micro switch, what should do I ? and I think it is better to use when have serial data and some thing like that. you know I think this problem is solvable with such easy command like IF just i should make a delay with timer after pressing the micro switch in this way I think solve this problem, but i am not sure :grin:
 

thank you very much but it is not reasonable to use external interrupt for a micro switch because as you know the just have one external interrupt so if i want to use more than one micro switch, what should do I ?

Hai... If you are having plans to add more than 1 switch, then can you answer me for the following?

1. Will you press more than a switch at a time?
2. Are you going to assign more than 1 LED corresponding to the switch?
3. If you press more than a switch at a time, when the timer should start? I mean, consider you have 2 switch and pressed at a time. Now you released only one switch with in a sec. but the other switch is released after 3 sec. When the timer should start to calculate the delay? during the release of 1st switch or 2nd switch?
4. If you don't want to use interrupt, then are you not going to perform any other operations in the code other than monitoring the port pins?

Please clear all this doubts. May be we can find solutions to get done your task.

All the best
 

1. Will you press more than a switch at a time?
2. Are you going to assign more than 1 LED corresponding to the switch?
3. If you press more than a switch at a time, when the timer should start? I mean, consider you have 2 switch and pressed at a time. Now you released only one switch with in a sec. but the other switch is released after 3 sec. When the timer should start to calculate the delay? during the release of 1st switch or 2nd switch?
4. If you don't want to use interrupt, then are you not going to perform any other operations in the code other than monitoring the port pins?
at first I am so happy for your pursuing and your answers are
1- No, never two switches press down together
2- No just one LED to each switch
3- more than one switch is undefined in my program and logically it's not possible to press two switches at a same time , I mean in any way one of them is pressing faster than other
4- not yet
and I should say that eventually I could wrote the above codes. you know my teacher said this program can use to control the debounce of the Switches easily
 

3- more than one switch is undefined in my program and logically it's not possible to press two switches at a same time , I mean in any way one of them is pressing faster than other

I understand 2 switches can not be pressed at a time. but when a switch is pressed and before release the pressed switch, if you press another switch, do you want the corresponding LED should glow? or both LED should glow? I think in this case, the timer will start after 2nd switch is released. Am i right?

4- not yet
and I should say that eventually I could wrote the above codes. you know my teacher said this program can use to control the debounce of the Switches easily

If you are not having any other operation then you can monitor the port pins continuously and make toggle the LED. But if you wanted to do some other operation during the period of idle condition of switch, then you can better some other way to perform this task. I will also think about it

All the best.
 

Hai i have u a suggestion. Use portB for the micro switch. There is a option in pic to detect the portB changes. And it gives u interrupt. In this interupt routine u can toggle the led or start timer to count a min. I hope u can use 4 micro switch or if u use like a keyboard then u may able to add 16 switch
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top