3
\$\begingroup\$

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;
}
\$\endgroup\$
  • \$\begingroup\$ Most likely the pins in question are multiplexed with another peripherals or there are some pull-down resistors active on the pins.. \$\endgroup\$ – m.Alin Oct 14 '13 at 13:22
  • \$\begingroup\$ Yes, indeed, they are multiplexed, but the datasheets specify that, after a reset, the pins are configured as digital inputs by default and the pins are only used for the other peripherals if the peripherals are enabled (which is not the case). In addition, I can use them as outputs, but not as inputs. \$\endgroup\$ – Genba Oct 14 '13 at 14:28
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Many thanks! I've just realized that this is actually mentioned on page 11-4 of the dsPIC30F Family Reference Manual. I somehow skipped that bit when reading. My bad :( I've also seen that the reset value of ADPCFG is specified on pages 95 and 149 of the dsPIC30F4011 Datasheet, in case anyone wonders. \$\endgroup\$ – Genba Oct 16 '13 at 12:22

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.