I have written a small PIC program in mikroC for turning on and off an led with same push button. That is pressing the switch will turn on the led and if the same switch is pressed again the led will turn off. Here is the program that I wrote
void main()
{
TRISB=0X00;
TRISC=OXFF;
PORTB=0X00;
PORTC=0XFF;
while(1)
{
if (portc.f3==0) /*checking if the switch is pressed*/
portb = ~portb; /*if the switch is pressed, the led will go off if it is already on or the led will go on if the it is already off*/
portc = 0xff;
}
}
}
another version of the same program that I have written is
void main()
{
int flag=0;
TRISB=0X00;
TRISC=OXFF;
PORTB=0X00;
PORTC=0XFF;
while(1)
{
if (portc.f3==0 && flag==0)
{
portb = 0xff;
flag=1;
portc=0xff
}
if (portc.f3==0 && flag==1)
{
porb=0x00;
flag=0;
portc=0xff;
}
}
}
in both cases it doesn't works as expected. when i press switch the led turns on but after some time it turns off itself (without pressing the switch again).
please help me and make me know whats wrong with my programs if any.
No comments:
Post a Comment