Strange PortA behavior on 18F PIC


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    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

  2. #2
    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

  3. #3
    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, 18:24
  2. PIC stop responding - Strange Problem!
    By financecatalyst in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 31st August 2009, 15:06
  3. Strange pic 16f877a memory loss
    By DavyJones in forum General
    Replies: 23
    Last Post: - 6th July 2009, 21:27
  4. First time with 18F pic...
    By gabrielg74 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 9th December 2005, 08:04
  5. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th March 2005, 00:14

Members who have read this thread : 0

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