cyteh
Newbie level 6
there are 4 switches where i named it as button1, button2, button3, button4
and they are originally connected to 2.5v.
once i press the button, it'll go to LOW.
When PIC detect LOW from the input, output=1.
However, the program i did was not functioning well...it execute "LATBbits.LATB2=1" even when i did not press the button( input is still high).
anyone know what mistake had i did?
this is my program:
#include "p18f24k20.h"
#define button1 LATAbits.LATA1
#define button2 LATAbits.LATA2
#define button3 LATAbits.LATA3
#define button4 LATAbits.LATA4
#define ON 1
#define OFF 0
void main (void)
{
LATA= 0x00;
TRISA=0b00011110;/* initialize RA<4:1> as input*/
LATB= 0x00;
TRISB=0b11100011;/* initialize RB<4:2> as output*/
LATC=0x00;
TRISC=0b11001011;/* initialize RC<5:4> and RA2 as output*/
while(1)
{
if(!button1)/*PIC detect LOW*/
{
LATBbits.LATB2=1;
}
else if(!button2)
{
LATBbits.LATB3=1;
}
else if(!button3)
{
LATBbits.LATB4=1;
}
else if (!button4)
{
LATCbits.LATC2=1;
}
else
{
LATBbits.LATB2=0;
LATBbits.LATB3=0;
LATBbits.LATB4=0;
LATCbits.LATC2=0;
}
}
} //main
and they are originally connected to 2.5v.
once i press the button, it'll go to LOW.
When PIC detect LOW from the input, output=1.
However, the program i did was not functioning well...it execute "LATBbits.LATB2=1" even when i did not press the button( input is still high).
anyone know what mistake had i did?
this is my program:
#include "p18f24k20.h"
#define button1 LATAbits.LATA1
#define button2 LATAbits.LATA2
#define button3 LATAbits.LATA3
#define button4 LATAbits.LATA4
#define ON 1
#define OFF 0
void main (void)
{
LATA= 0x00;
TRISA=0b00011110;/* initialize RA<4:1> as input*/
LATB= 0x00;
TRISB=0b11100011;/* initialize RB<4:2> as output*/
LATC=0x00;
TRISC=0b11001011;/* initialize RC<5:4> and RA2 as output*/
while(1)
{
if(!button1)/*PIC detect LOW*/
{
LATBbits.LATB2=1;
}
else if(!button2)
{
LATBbits.LATB3=1;
}
else if(!button3)
{
LATBbits.LATB4=1;
}
else if (!button4)
{
LATCbits.LATC2=1;
}
else
{
LATBbits.LATB2=0;
LATBbits.LATB3=0;
LATBbits.LATB4=0;
LATCbits.LATC2=0;
}
}
} //main