Hey guys, i've been working on my project which requires me to turn on/off four switches. Below is my code for doing it.
PORTA are all connected to pushbutton switch.When i press the switch of RA3, RB 4 will be turned on(giving a high state), another push will turn off RB 4(low state)
When i press the switch of RA4, RB 5 will be turned on(giving a high state), another push will turn off RB 5(low state). The same goes for the other two pins.
Now here's my problem, my i push push RA3, RB4 will goes into a high, then when i push RA4, RB 5 will be high as well. But to turn off RB4, i need to push the switch of RA3 twice.
Does anyone has any idea how to solve this. Many thanks.
Code:
#include <pic.h>
#include <htc.h>
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BORDIS);
void main(void)
{
unsigned int A,B,ch=0,d;; //variable A & B
unsigned char segment[] ={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
ANSEL = 0x00; //select all PORTs digital i/o
ANSELH = 0x00;
TRISA = 0xFF; //select PORTA as input
TRISB = 0x00; //select PORTB as output
TRISC= 0x00; //PORTC SET AS OUTPUT
A=0; //pre-define A=0
B=0; //pre-define B=0
PORTB=0x00;
PORTC= segment[ch];
PORTA= 0x00;
while(1)
{
//on/off funcion
if(PORTA==0x04)
{
if(A==0)
{ B=1;
A=1;
}
else
{ B=2;
A=0;
}
}
if(B==1)
{ RB4=1;
B=0;
for(d=0;d<20000;d++){}
}
if(B==2)
{ RB4=0;
B=0;
for(d=0;d<20000;d++){}
}
if(PORTA==0x08)
{
if(A==0)
{ B=1;
A=1;
}
else
{ B=2;
A=0;
}
}
if(B==1)
{ RB5=1;
B=0;
for(d=0;d<20000;d++){}
}
if(B==2)
{ RB5=0;
B=0;
for(d=0;d<20000;d++){}
}
if(PORTA==0x10)
{
if(A==0)
{ B=1;
A=1;
}
else
{ B=2;
A=0;
}
}
if(B==1)
{ RB6=1;
B=0;
for(d=0;d<20000;d++){}
}
if(B==2)
{ RB6=0;
B=0;
for(d=0;d<20000;d++){}
}
PORTA are all connected to pushbutton switch.When i press the switch of RA3, RB 4 will be turned on(giving a high state), another push will turn off RB 4(low state)
When i press the switch of RA4, RB 5 will be turned on(giving a high state), another push will turn off RB 5(low state). The same goes for the other two pins.
Now here's my problem, my i push push RA3, RB4 will goes into a high, then when i push RA4, RB 5 will be high as well. But to turn off RB4, i need to push the switch of RA3 twice.
Does anyone has any idea how to solve this. Many thanks.