PDA

View Full Version : Strange PortA behavior on 18F PIC



Terry
- 16th August 2007, 23:49
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

Dave
- 17th August 2007, 00:52
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

Bruce
- 17th August 2007, 02:59
Try this. Change PORTA.3 and PORTA.4 to LATA.3 and LATA.4.

Does it work as expected?

Terry
- 17th August 2007, 19:46
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

Dave
- 17th August 2007, 20:10
Terry, I am sorry but I did mean ADCON1, not INTCON1.....

Dave Purola,
N8NTA

Terry
- 17th August 2007, 21:23
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

Bruce
- 17th August 2007, 21:39
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.


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.

Terry
- 17th August 2007, 22:22
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

Bruce
- 17th August 2007, 22:31
It can happen on any port. Read-modify-write isn't prejudice at all..;o}