3
\$\begingroup\$

I am experiencing a strange problem with reading back the value of PORTD while it is configured as an output. I am writing to LATD to set output on the port. The actual output is as I expect (verified with LEDs), but when I then read the value of PORTD, bits 0:3 always come back as zero...

My first thought was conflict with the ADC, but it is switched off, as are CCP1, ECCP and PSP which also share pins with port D. The code works fine when running in MPLAB SIM but the problem shows up both running normally and debugging with a PICkit3...

Here's the code:

void main (void)
{
   char test;

    ADCON0bits.ADON=0;          //Turn adc off
    ADCON1 = 0x0F;              //Turn individual ADC channels off.
    ECCP1CONbits.ECCP1M = 0;    //Turn off ECCP module
    CCP1CON = 0;                //Disable CCP1
    TRISEbits.PSPMODE = 0;      //Turn off PSP module
    TRISD = 0;                  //PORTD to output
    LATD = 0xFF;                //Set all bits high
    Nop();                      //Wait for output to propagate back to PORTD
    test = PORTD;               //test gets 0xF0???
}

This is really frustrating, all the more so because the hardware actually functions but my self tests keep giving false positives...

\$\endgroup\$
5
  • \$\begingroup\$ For a start I'd try using more than one NOP if you haven't tried already. Been a while since I've used PICs but I thought it took a cycle to raise an output, given a bit of capacitance it might be taking a bit longer to go high. I'd probably stick in a dozen or call a delay function to see if it makes a difference for a start. \$\endgroup\$
    – PeterJ
    Commented Mar 14, 2013 at 11:19
  • \$\begingroup\$ Reading LATD instead of PORTD (the difference) would probably solve the problem, but this is strange and needs clarification. Can you check the voltage on the different pins with respect to ground? \$\endgroup\$
    – user17592
    Commented Mar 14, 2013 at 11:48
  • \$\begingroup\$ Peter - I have indeed tried adding extra delay... Watching the register in the debugger, you can see the top bits update on the next cycle after the write, but the bottom bits don't... \$\endgroup\$
    – tsiflana
    Commented Mar 14, 2013 at 12:24
  • \$\begingroup\$ Camil - LATD is no good unfortunately... It does indeed show up correctly there, but I need to read PORTD to check the hardware is working correctly too. The output pins do get set correctly... \$\endgroup\$
    – tsiflana
    Commented Mar 14, 2013 at 12:26
  • 1
    \$\begingroup\$ You might check the header file and linker script to make sure they mapped those bits correctly as those outputs aren't available on the most common of that series. Or check the memory location in the debugger where you know the port is. \$\endgroup\$
    – Matt
    Commented Mar 14, 2013 at 13:31

1 Answer 1

2
\$\begingroup\$

I think it's the comparators still being attached - try adding:

CMCON = 0x07;

If you notice in the table CM2:CM0 have to be 111 (rather than 000) to have the pins disconnected and digital. Plus they correspond to pins RD0:RD3:

Comparators Off

\$\endgroup\$
3
  • \$\begingroup\$ Thanks!!!! I feel slightly ashamed that I clearly didn't read the datasheet carefully enough, but in my defense, two of us went through it, line by line, and still didn't spot it... That figure seems to be the only place the function of those bits is explained. Anyway, thanks again :) \$\endgroup\$
    – tsiflana
    Commented Mar 14, 2013 at 16:18
  • \$\begingroup\$ @tsiflana - no problem. Don't feel silly - I have seen this mistake made a few times, probably made it myself too at first. The datasheet doesn't make it nice and clear - the OFF being on the right of the table (and on the left if you glance you see "Comparators Reset"), the default being the comparators connected and all bits set to 1 to disconnect is not what you would usually expect, so it's hardly surprising it's quite a common trap. \$\endgroup\$
    – Oli Glaser
    Commented Mar 14, 2013 at 16:24
  • 1
    \$\begingroup\$ It's more that it stays connected even if you switch off the comparator... If I had written that datasheet, I would have put that in bold. Twice. ;) \$\endgroup\$
    – tsiflana
    Commented Mar 14, 2013 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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