0
\$\begingroup\$

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
        Nop();
        Nop();
        Nop();

    }

}

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ I didn't read your datasheet but it's common for PIC devices to have some pins that are analog inputs by default and you need to clear a bit in a register to allow digital input. Look for a register named ANSEL or similar. \$\endgroup\$ – Roger Rowland Apr 15 '16 at 4:16
  • \$\begingroup\$ you were correct, thank you! only difference is that the register is called AD1PCFGL in my particular device's case \$\endgroup\$ – ChateauDu Apr 15 '16 at 18:45
1
\$\begingroup\$

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!

Thanks

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.