Strange PortA behavior on 18F PIC


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2005
    Posts
    24

    Default Strange PortA behavior on 18F PIC

    I have a strange problem when setting a pin high on a PIC18F2525 on the A port. Below is the code simplified to almost nothing, but demonstrates the problem. PortA.3 will not go high to 5v, it stays at 0v when I also set PortA.4 pin high, but if I add a 1 ms delay between the commands, then it works or reverse the order in setting the pins high then PortA.3 can be set high to 5v. Below is the code. Does anybody have any clues as to what is going on, or have I missed something in the data sheet on configuring Port A. Even though I have a simple solution, it would be nice to understand what is going on, because it usually will come back to bite me.
    Terry

    Define OSC 20

    ADCON1 = %00001110 ' Sets port A to digital except A0
    TRISA=%00000001 ' Set Port A to outputs, except A0

    PORTA.3 = 1 ' This does not work, but if either a delay is added after this
    'pause 1 ' line or the PortA.4=1 line is in front of the PortA.3=1 line it works.
    PortA.4 = 1
    END

  2. #2
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    Terry, You need to configure INTCON1 for digital use and CMCON for digital use. It's in the data sheet for the part....

    Dave Purola,
    N8NTA

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Try this. Change PORTA.3 and PORTA.4 to LATA.3 and LATA.4.

    Does it work as expected?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Dec 2005
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Thanks for the feedback.
    I added the line
    CMCON = %00000111 to make port A digital rather than a comparator input, this made no difference. the default state according to the data sheet at POR is digital, so this is why I didn't originally put it in and would also explain why it made no difference. There is not a INTCON1 register for the 18F2525, but ADCON1 is used to make the port digital as originally listed.

    Changing to using LATA.x did make it work as expected!! Any theories as to why this worked and the other did not? also using "High PortA.x" did not work.
    Terry

  5. #5
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    Terry, I am sorry but I did mean ADCON1, not INTCON1.....

    Dave Purola,
    N8NTA

  6. #6
    Join Date
    Dec 2005
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Dave,
    I appreciate the help, I usually spend more time reading the PIC data sheets that I do writing the actual code, there always seems to be gotcha's if the registers are not set right.
    Terry

  7. #7
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Hi Terry,

    It's caused by read-modify-write. What happens is this;

    PORTA.3=1
    PORTA.4=1

    Generates code BSF PORTA,3 immediately followed by BSF PORTA,4. A slight amount of
    capacitance on PORTA.3 causes the pin to slowly transition from 0 to 1. The next instruction
    BSF PORTA,4 causes the whole port to be read, PORTA,4 bit to be modified, and the whole
    value is again placed back on the port.

    If PORTA,3 (the physical pin) is still low during this period, then a 0 is placed back on the pin.

    Note that it may work just fine if you switch these around and set A.4 first, then A.3 second
    because there may be less capacitance on A.4 than A.3. So it works.

    Writing to the LAT registers with BSF and BCF doesn't cause the read-modify-write problem.

    If you want to see it in action, try this experiment. Wire LEDs to RA3 and RA4. Place a small
    capacitor on one pin at the junction where the LED connects to the pin. Hook the free
    end of the cap to ground or +5V.

    Run this and watch the LEDs.
    Code:
    DEFINE OSC 20
    
    X VAR BYTE
    
    PORTA=0
    TRISA=%00000001
    ANSEL0=%00000001 ' or whatever you need for your particular PIC
    
    MAIN:
    ' Causing read-modify-write on any PIC
      FOR X = 0 TO 49
        PORTA.3=1
        'PAUSE 10 ' enable this pause to see the difference
        PORTA.4=1
        PAUSE 100
        PORTA.3=0
        'PAUSE 10  ' enable this pause to see the difference
        PORTA.4=0
        PAUSE 100
      NEXT X
    
      FOR X = 0 TO 49
         PORTA=%00011000
         PAUSE 100
         PORTA=%00000000
         PAUSE 100
      NEXT X
     
      FOR X = 0 TO 49
        LATA.3=1
        LATA.4=1
        PAUSE 100
        LATA.3=0
        LATA.4=0
        PAUSE 100
      NEXT X
        
      GOTO MAIN
      END
    If you remove the comments from each PAUSE in the 1st section, the LED with the cap
    blinks fine. Comment out the pause, and the LED with the cap doesn't work as expected.

    The larger the capacitance, the longer the PAUSE needs to be.

    Writing to the whole port at once or using the LAT registers, the LED blinks as expected.
    Even with the cap.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  8. #8
    Join Date
    Dec 2005
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Wow Bruce!!!
    What a great explanation, can this same phenomenon also happen on other ports, or is this just more unique to port A, being the typical Analog A/D port where the input capacitances are slightly different from the other ports.
    Thanks again for the great explanation.
    Terry

  9. #9
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    It can happen on any port. Read-modify-write isn't prejudice at all..;o}
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. PIC Interrupt and PORTA input
    By Jerade in forum mel PIC BASIC
    Replies: 5
    Last Post: - 29th November 2009, 17:24
  2. PIC stop responding - Strange Problem!
    By financecatalyst in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 31st August 2009, 14:06
  3. Strange pic 16f877a memory loss
    By DavyJones in forum General
    Replies: 23
    Last Post: - 6th July 2009, 20:27
  4. First time with 18F pic...
    By gabrielg74 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 9th December 2005, 07:04
  5. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th March 2005, 23:14

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts