Electronic – Digital input ports of the dsPIC30F4011/4013 not working on the dsPICDEM 2 Development Board

digital-logicinputmicrochippicport

I have a Microchip dsPICDEM 2 Development Board with two microcontrollers on it: a dsPIC30F4011 (Motor Control Family) and a dsPIC30F4013 (General Purpose Family). I'm programming in C using the MPLAB IDE.

What I want to do is read the a digital input signal with TTL levels (0-5 V), but the reading of the ports is always 0.

If I set the port to be an output using the corresponding TRIS register and then write to the corresponding LAT register, it works fine. For example:

TRISBbits.TRISB0 = 0; // set RB0 to be an output
LATBbits.LATB0 = 1;

works perfectly.

However, when I set the port to be an input, I keep reading 0 both from the LAT and the PORT register. So, neither of the following code excerpts work:

// This doesn't work:

TRISBbits.TRISB0 = 1; // set RB0 to be an input
value = LATBbits.LATB0;

// This doesn't work either:

TRISBbits.TRISB0 = 1; // set RB0 to be an input
value = PORTBbits.RB0;

My program is as simple as this:

int main(void)
{
    TRISBbits.TRISB0 = 1;
    TRISBbits.TRISB1 = 0;

    while (1)
    {
        int rb0 = LATBbits.LATB0;
        LATBbits.LATB1 = rb0;
    }

    return 0;
}

so there's no other possible error source in the program.

These are the documents I've used for reference:

  1. dsPIC30F Family Reference Manual
  2. dsPIC30F4011/4012 Data Sheet
  3. dsPIC30F3014/4013 Data Sheet

What am I doing wrong?

EDIT: After the corrections pointed out by Tut (see accepted answer), the code of the program looks like this:

int main(void)
{
    TRISBbits.TRISB0 = 1; // set RB1 as input
    TRISBbits.TRISB1 = 0; // set RB1 as output
    ADPCFGbits.PCFG0 = 1; // set RB0 as digital input (i.e. disable analog input)

    while (1)
    {
        LATBbits.LATB1 = PORTBbits.RB0;
    }

    return 0;
}

Best Answer

The PORTB pins have alternate functions as analog inputs. You need to configure the ADPCFG register for digital pins (bits set to 1) as the reset value is all zeros (analog pins). Refer to the dsPIC30F Family Reference Manual page 18-9.

Note also that to use the pins as digital inputs, set the desired TRISBbits to 1 (inputs). Then you need to read from PORTBbits, not from LATBbits which is a latch for output operations.