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.
Bookmarks