Electronic – PIC16F628 PORTA Behaving oddly

pic

This is a real odd thing I have here.

I picked up a PIC16F628 this morning to play with, and I am having a very strange issue with it.

I have written a very simple program which should light 2 LEDs in this sequence: (* = on, o = off)

RA0 RA1
 *   o
 *   *
 o   *
 o   o

But what actually happens is:

RA0 RA1
 *   o
 o   *
 o   o
 o   o

When RA1 turns on, RA0 turns off.

The exact same code run on an '84a works perfectly. Snippet:

loop
    BSF     PORTA,0
    CALL    delay
    BSF     PORTA,1
    CALL    delay
    BCF     PORTA,0
    CALL    delay
    BCF     PORTA,1
    CALL    delay
    GOTO    loop

My only thoughts are that it has something to do with the comparators that share the pins (should be disabled by default) which I have tried manually disabling but it has had no effect.

Now, one more thing to add confusion… If I send absolute values to PORTA (0x01, 0x03, 0x02, 0x00) it works correctly. Why should the bitwise operations work so strangely?

Oh, and I don't have any issue at all working with PORTB – only PORTA.

Any clues or ideas? Because I have run out.

Best Answer

Your problem is probably caused by the Read-Modify-Write effect, see page 31 of the data sheet. You should use a shadow register, set and reset the bits in that, and write it to the port. Delays usually help, so I find it surprising that it caused you problems. The fact that you could write bytes to the port and it works as expected indicates that it probably is an R-M-W effect.

Here is a good description of the problem.

The 18F PICs have latch registers for the ports, and don't suffer from the R-M-W problem.