I am working on a simple (to get more complex later) program to read buttons from a pic24h in a microstick debugger device. I think I am initializing or reading the ports wrong, because some of the bits on the portb register, just seem to randomly come high. This code will eventually make errScreenPage = 1, even when I am not pressing the buttons. Why is this, and what can I do to make my code work?
My code is as follows:
#include "p24HJ64GP502.h"
int numErrors = 8;
int moreErrorsPage = 0;
int errScreenPage = 0;
int maxErrScreenPage = 1;
void setup();
void checkBtn();
int main(void)
{
setup(); //setup the pic
wait(65535);
while(1){
checkBtn();
}
return 1;
}
void checkBtn(){
if(PORTBbits.RB5 == 1 && errScreenPage != maxErrScreenPage){
if(errScreenPage != 0 || numErrors == 0){
errScreenPage += 1;
}
}
if(PORTBbits.RB6 == 1 && errScreenPage == 0){
errScreenPage = 1;
}
}
void setup()
{
RPOR7 = 0x0708;
SPI1CON1 = 0x007E;
SPI1STAT = 0x8000;
TRISBbits.TRISB4 = 0;
TRISBbits.TRISB12 = 0;
TRISBbits.TRISB13 = 0;
LATBbits.LATB13 = 1;
LATBbits.LATB12 = 1;
LATBbits.LATB4 = 1;
wait(1000);
}
I figured it out, i needed to pull the pin low when it was not being pulled high.