Many thanks to reader Michael McLaren for giving me a heads-up about a better way to move the cursor on the LCD display.
In an earlier post, I described how I was using the ‘shift cursor right’ command multiple times to put the cursor in the desired position – mainly so that I could use the second line of the display.
Michael pointed out that it’s possible to talk directly to the display’s memory – which is known as DDRAM (display data random access memory). It has two sets of 40 bytes with addresses $00–$27 and $40–$67. Setting the address of the byte you want to talk to next is a simple matter of sending a byte to the command (aka instruction) register. You set the top bit – bit 7 – of this byte to 1 and the other seven bits contain the address of the byte.
I created another constant in the code:
LCD_SET_DDRAM = %10000000
And now, the code to set the cursor position is:
.lcd_set_cursor ; assumes X & Y co-ords have been put in X and Y ; X should contain the X param in range 0-15. ; Y should be 0 or 1. txa ; A now contains X position cpy #1 bcc lcd_move_curs ; Y is less than 1 adc #$3F ; if we want second line, add $40 .lcd_move_curs ora #LCD_SET_DDRAM ; OR with LCD_SET_DDRAM command byte jsr lcd_cmd rts
This is so much more compact and efficient than my original code. It’s seven lines of actual code (ie, not including labels and comments) compared to 14. And it doesn’t need a time-wasting loop. Basically, if the Y register is set to 1, the code just adds $3F to the value from the X register (which has been shifted into A for the purpose). This gives us the DDRAM address we need, so that gets ORed with LCD_SET_DDRAM and the command is sent to set the address.
BTW, the datasheet says you can use the memory addresses of bytes that aren’t going to be displayed just as memory. We’re using 16 bytes out of 40 for each row, leaving a total of 48 bytes going spare. I’m sure I could make use of that somehow. You know, if I were clever…
But, hmm. When we want to start at the beginning of the second line, we’re adding $3F to a value that is 0. But the first address of the second line is $40. There’s still that discrepancy. I’m not sure what I make of that just yet. But as it’s working, I’m not going to fix it.