Electronic – can’t read push button value in dsPIC33

cmplabpic

This is the first time I work with a PIC microcontroller. I am using the Clicker 2 for dsPIC33 board (dsPIC33EP512MU810), and so far I am trying to implement a basic programme for testing the board. In this case, I want to turn on the 2 LEDs based on the value of a push button. I am using MPLAB X with the XC16 compiler

According to the schematics, the LEDs are connected to ports RA0 and RG9, whereas the push buttons are connected to RE8 and RE9.

Right now I am able to turn ON and OFF the LEDs, but the programme does not recognize when the buttons are pressed.

It was tested both RE8 and RE9 buttons, but none of them worked. Afterwards, a wire was used for shorting directly different pins of the board, doing the same function as the buttons. It was tested with RA1, RB3, and RG8. It only worked with RA1.

The code is:

int main(void) {
    TRISA = 0xFFFE;
    TRISB = 0xFFFF;
    TRISE = 0xFFFF;
    TRISG = 0x0009;
    LATA = 0x0002;
    LATG = 0x0200;

    char pushed = 0;

    while(1)
    {

        if (PORTEbits.RE8) {
            pushed = 1;
        }
        else {
            pushed = 0;
        }

        if (pushed) {
            LATG = 0x0200;
            LATA = 0x0000;
        }
        else {
            LATG = 0x0000;
            LATA = 0x0001;
        }
    }
}

Best Answer

RE8 and RE9 are dual analog/digital, and by default ANSEL bits are set to function as analog, so you need to set ANSELE to 0x0000. Per the datasheet -

When the PORT register is read, all pins configured as analog input channels are read as cleared (a low level).

Also, the buttons are connected from pin to ground with a pull-up so they'll be low when pushed.