Electrical – can’t figure out why dsPIC digital input is not working


Having trouble getting a basic digital input to work correctly. Just trying to use it as an additional condition for an LED flasher to work. Right now their example code works just fine but when I try to add my additional condition on PORTA pin RA1 is just always reads it as 0 no matter what input I apply to the the pin. Total noob to microcontrollers so I'm sure the mistake is basic, but here is the code:

int main ( void )

    /*  Initialize ports */
    //LATAbits.LATA0  = 0;          // set latch levels

    TRISA = 0x0002;                 // set pin RA1 as an input
    LATB  = 0x0000;                 // set latch levels
    TRISB = 0x0000;                 // set IO as outputs
    InitTimer1();                   // start the timer

    /*  endless loop*/
    while (FOREVER)

        if (timer_expired && Counter == FLASH_RATE && (PORTAbits.RA1 == 0))
            LATAbits.LATA0 = ~LATAbits.LATA0;
            LATBbits.LATB0 = ~LATBbits.LATB0;
            Counter = 0;
            timer_expired = 0;

        // or do something else



I didn't include the timer code because that is all working fine. The problem is with trying to read the RA1 pin using … PORTAbits.RA1. I know I have the tri-state register initialization correct because I put a pull-up resistor on the pin, initialized the whole LATA to 0's and then switched TRISA from 0x0000 to 0x0002; in the first case, the pin was driven low, in the second case, it was pulled up by the resistance. I looked at dsPIC's documentation here and couldn't find anything which explained why I can't seem to read a value from it.

Best Answer

I finally figured out the issue. The DSPIC has something called the AD1PCFGL register which by default disables the buffer whose input is the pin voltage and output goes to the "read port" hardware. It must be explicitly enabled in software for any pins whose voltage you'd like to read.

Roger Rowland you were right!