LCD Stuff2

The second line display problem actually wasn’t that simple to fix, when all is said and done.

I thought it was a matter of updating the letter count in the PIC when cursoring down to the second line. I modified the PIC code by adding the fragment:

if (byteToSend = 13) then                           
charLocation = 16
goto getNextChar
endif

I thought this would work, because the goto leads to a section that tests if charLocation = 16 and then outputs the LCD escape sequence to move the cursor to the second line. But the display was skipping a chunk of the message after the carriage return.

Of course it didn’t help that for about 20 minutes or more, I was compiling the HEX files into one directory and burning HEX files from a completely different directory and then wondering why none of my code changes were making any difference.

Anyway, once I’d got the PIC actually responding to the carriage return I was stuck with the problem of the missing letters.

So, it turns out that changing the charLocation variable to 16 leads to the program skipping all the letters in the message between the \r character and whatever is at position 16. That’s not what you want. You want the cursor to be at position 16 but the charLocation to point to the next character in the message.

I solved the problem by creating a new variable called charPos, to refer to the LCD physical cursor position and updating it separately from charLocation, which is the index to the current character in the message.

Something screwy with my keyboard rn.

At last I was getting somewhere. The final PicBasic code for displaying the messages looks a bit like:

showMessage:
gosub clearDisplay
charLocation = 0
charPos = 0

getNextChar:
if (charPos = 16) then
LCDOUT $fe, $c0
endif

byteToSend=messageBuffer[charLocation]
if (byteToSend = 0) then check2

if (byteToSend = 13) then
charPos = 16
charLocation = charLocation + 1
goto getNextChar
endif

LCDOUT byteToSend

charLocation = charLocation + 1
charPos = charPos + 1

if (charPos < 32) then
goto getNextChar
else
goto check2
endif

Bear in mind this is just a fragment of the code. First it clears the display and resets the index variables, then it enters the display loop. It checks if the character position is or has been set to 16 and moves the cursor to the second LCD line.

Then it gets a byte from the message buffer with charLocation as the index variable. If the byte is 0, that’s the end of the message so return to the main program loop. If it’s 13 (\r) set charPos to 16, and increment the charLocation variable to skip the carriage return character, otherwise the LCD will try to display it. Then return to the start of the loop. Since charPos has just been set to 16, the cursor will be moved to the second line.

Otherwise, byteToSend is a normal character, so LCDOUT it so it’s displayed. Once the character is displayed, increment the indices.

Then it checks if charPos is less than 32 (the maximum number of displayable characters on the 16 x 2 LCD. If it’s less than 32, it keeps looking for characters in the message buffer. Otherwise it returns to the main program loop.

This is where I tried putting a pause to allow a super long stream to be displayed over multiple screens, but all that meant was that the Pico sent a burst of data which the PIC mostly ignored because it was sitting in a pause loop. If I want to be able to display multi-screen messages, that will need to be handled by the Pico.

After doing all that I soldered together another LCD module with the new PCB. One thing I noticed on the first of the modules is that using vertical header pins for connecting the data cable meant that the connector pokes out 2+ cm from the back of the board. I’d made allowance for a modular connector to take up the space next to the header pins but I wasn’t installing a modular connector and since there was plenty of space, for the second module I elected to use 90° header pins, meaning that the plug sits snugly, parallel to the PCB instead of poking out.

Leave a Reply

Your email address will not be published. Required fields are marked *