Electronic – How to read from PIC18F4680 port D

picport

On my project, I plan to have a user configurable setting set using a switch. Unfortunately, it looks like I don't know how to read from a port.

I'm using PIC18F4680 and C18 compiler.

Here's the picture of the circuit:

switch

schematic

Circuit description:

The pin 19 is set as high and resistor R3 is set between pin 19 of the PIC and pin 3 of the switch. Pin 20 of the PIC is connected to the pin pin 2 of the switch. Pin one of the switch is connected via R37 to the circuit ground. Using a multimeter, I can see that when the switch is set so that the switch's pins 2 and 3 are connected on the PIC pin 20, there is at logical high voltage (around 4.96 V). When the switch is set so that switch's pins 1 and 2 are connected, I can see that the PIC's pin 20 is at logical low voltage (around 40 mV). The pin settings are exactly as shown below. As far as I know, on this PIC there are no internal pull-up resistors on port D.

Pin 19 is RD0 on this PIC and pin 20 is RD1 on this PIC.

I've set the RD0 like this:

TRISDbits.RD0=0;//Set as output 
LATDbits.LATD0=1;

and RD1 like this:

 TRISDbits.RD1=1;//set as input

When the switch is set so that the RD0 is connected to RD1, I can read 4.96 V on the RD1 pin, so after reading, I should get high value. Unfortunately, when I read it using this code:

PortD1=PORTDbits.RD1;

where PortD1 is a char.

I always get PortD1 value of zero, no matter in which position the switch is.

I'm not using any of the devices which are multiplexed to pins RD0 and RD1.

So what am I doing wrong here?

UPDATE 1:

I added

CMCON = 0;
TRISEbits.PSPMODE=0;

to the start of my program, so comparator and parallel slave port should be disabled and shouldn't interfere with port D. Also I'm not using hardware PWM, so it shouldn't be causing problems.

UPDATE 2

On this chip, I'm using port E to drive an 8 bit multiplexer, which is working correctly. The strange thing is that when I try to read status of port E, using

PortD1=PORTE;
PortD1=LATE;

I get zeroes both times although I know that the value isn't zero. So it looks like the problem isn't specific to port D.

UPDATE 3

After some more testing, I was unable to repeat the problems with reading LAT registers. It seems that they are read fine now.

UPDATE 5

After doing some more testing, it appears that ports A, B and C provide results same as LAT A, B and C registers. Ports D and E always read zero, no matter what is written into their LAT registers. It turns out that port E was zero because it was set as analog input. After setting ADCON1bits.PCFG=255;, port E works now.

UPDATE 6

I've also disabled the ECCP module using ECCP1CONbits.ECCP1M=0; and it didn't make any difference. I've accounted for all multiplexed modules on port D and they are all disabled.

Best Answer

I think Wouter had the right idea, but you need to put CMCON = 0x07; to turn the comparators off (see p.258 of datasheet, top right)