mf1364
Full Member level 3
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:
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
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: