PRINT command for LCD changing port pins

  • Chuck Hellebuyck

    I'm driving a DFRobot Arduino LCD shield with a PIC16F886 based CHIPINO module in 4-bit mode.
    I have:
    RW pin grounded
    RS pin on PORTB.0
    E pin PORTB.1
    DB4 pin on PORTC.3
    DB5 pin on PORTC.2
    DB6 pin on PORTC.1
    DB7 pin on PORTC.0

    PORTB.2 controls the backlighting of the LCD (high on, low off)

    Here's the problem:
    When I use the PRINT command the message displays just fine but the backlight turns off.
    It appears that the LCD commands are changing the whole PORTB state.
    PORTB.2 keeps getting set low by the LCD commands.
    I have to set the PORTB.2 pin high after every PRINT command or the backlight will shut off.

    Shouldn't the LCD commands leave the other pins on the PORT alone?
    Is this a bug with the LCD commands?

  • Hugh Considine

    Hugh Considine - 2012-12-12

    This is probably caused by a read-modify-write error on the chip.

    On PIC chips, the bsf/bcf instructions work by reading a variable, changing the appropriate bit, and then writing the value back to the variable. The problem is that on the 16Fs, the output latch isn't accessible, so everything has to be done with the PORTA/B/C variable. When the PIC reads the PORTB variable, it reads the actual level on the pin, not what state it is set to. So if you have turned a bit on, but there is something pulling the voltage on the pin below about 3V, it will read as a zero. When the bsf/bcf instruction writes the value back to the port, it will turn the bit off. The same can happen if a pin is set off but pulled up by something - it will read as being on, and then the bcf/bsf instructions will turn it on.

    There are two ways to fix this. One option is to add Set commands in the right places to turn the backlight back on, as you've done. The other way is to switch to an 18F or enhanced 16F (16F1xxx) - these chips have a latch register that can be read instead of having to read values off of the pins, so read-modify-write errors are much less of a problem.


Log in to post a comment.